/* * BlobRoot = blob root name * BlobDirectoryReference = "directory name" * BlockBlobRef = "File name" * */ private void DownloadFileFromBlob(string BlobRoot, string BlobDirectoryReference, string BlockBlobRef, string LocalFileName) { try { if (!AZStorage.CloudStorageAccount.TryParse(ConfigurationManager.ConnectionStrings["AzureBlobStorageConfigConnection"].ConnectionString, out AZStorage.CloudStorageAccount StorageAccountAZ)) { throw new ConnectionException("Can not connect to CloudStorage Account. Verify connection string."); } AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ); var container = ClientBlob.GetContainerReference(BlobRoot); container.CreateIfNotExists(); AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobDirectoryReference); AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlockBlobRef); BlobBlock.DownloadToFile(LocalFileName, FileMode.OpenOrCreate); } catch (Exception ex) { throw new AzureTableBackupException(String.Format("Error downloading file '{0}'.", LocalFileName), ex); } finally { } }
/// <summary> /// Restore file created in blob storage by BackupAzureTables to the destination table name specified. /// Blob file will be downloaded to local storage before reading. If compressed, it will be decompressed on local storage before reading. /// </summary> /// <param name="DestinationTableName">Name of the Azure Table to restore to - may be different than name backed up originally.</param> /// <param name="OriginalTableName">Name of the Azure Table originally backed (required for determining blob directory to use)</param> /// <param name="BlobRoot">Name to use as blob root folder.</param> /// <param name="WorkingDirectory">Local directory (path) with authority to create/write a file.</param> /// <param name="BlobFileName">Name of the blob file to restore.</param> /// <param name="TimeoutSeconds">Set timeout for table client.</param> /// <returns>A string indicating the table restored and record count.</returns> public string RestoreTableFromBlob(string DestinationTableName, string OriginalTableName, string BlobRoot, string WorkingDirectory, string BlobFileName, int TimeoutSeconds = 30) { string result = "Error"; if (String.IsNullOrWhiteSpace(DestinationTableName)) { throw new ParameterSpecException("DestinationTableName is missing."); } if (String.IsNullOrWhiteSpace(OriginalTableName)) { throw new ParameterSpecException("OriginalTableName is missing."); } if (String.IsNullOrWhiteSpace(WorkingDirectory)) { throw new ParameterSpecException("WorkingDirectory is missing."); } if (!Directory.Exists(WorkingDirectory)) { throw new ParameterSpecException("WorkingDirectory does not exist."); } if (String.IsNullOrWhiteSpace(BlobFileName)) { throw new ParameterSpecException(String.Format("Invalid BlobFileName '{0}' specified.", BlobFileName)); } bool Decompress = BlobFileName.EndsWith(".7z"); if (String.IsNullOrWhiteSpace(BlobRoot)) { throw new ParameterSpecException(String.Format("Invalid BlobRoot '{0}' specified.", BlobRoot)); } if (Path.GetFullPath(WorkingDirectory) != WorkingDirectory) { throw new ParameterSpecException(String.Format("Invalid WorkingDirectory '{0}' specified.", WorkingDirectory)); } if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ)) { throw new ConnectionException("Can not connect to CloudStorage Account. Verify connection string."); } try { AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ); var container = ClientBlob.GetContainerReference(BlobRoot); container.CreateIfNotExists(); AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + OriginalTableName.ToLower()); string WorkingFileNamePath = Path.Combine(WorkingDirectory, BlobFileName); string WorkingFileNamePathCompressed = Path.Combine(WorkingDirectory, BlobFileName); /* * If file is compressed, WorkingFileNamePath will be set to .txt * If file is not compressed WorkingFileNamePathCompressed will be left as .txt */ if (Decompress) { WorkingFileNamePath = WorkingFileNamePath.Replace(".7z", ".txt"); } else { //WorkingFileNamePathCompressed = WorkingFileNamePathCompressed.Replace(".txt", ".7z"); } AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlobFileName); BlobBlock.DownloadToFile(WorkingFileNamePathCompressed, FileMode.Create); //https://www.tutorialspoint.com/compressing-and-decompressing-files-using-gzip-format-in-chash if (Decompress) { FileStream FileToDeCompress = File.OpenRead(WorkingFileNamePathCompressed); using (FileStream OutFileDecompressed = new FileStream(WorkingFileNamePath, FileMode.Create)) { using (var zip = new GZipStream(FileToDeCompress, CompressionMode.Decompress, true)) { byte[] buffer = new byte[FileToDeCompress.Length]; while (true) { int count = zip.Read(buffer, 0, buffer.Length); if (count != 0) { OutFileDecompressed.Write(buffer, 0, count); } if (count != buffer.Length) { break; } } } FileToDeCompress.Close(); OutFileDecompressed.Close(); } } result = RestoreTableFromFile(DestinationTableName, WorkingFileNamePath, TimeoutSeconds); // Cleanup files if (File.Exists(WorkingFileNamePath)) { File.Delete(WorkingFileNamePath); } if (File.Exists(WorkingFileNamePathCompressed)) { File.Delete(WorkingFileNamePathCompressed); } } catch (ConnectionException cex) { throw cex; } catch (Exception ex) { throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex); } finally { } return(result); }