private CloudBlockBlob GetBlockBlobReference(CloudBlobContainer container, string blobName) { try { return(BackupRestoreUtility.PerformWithRetries <string, CloudBlockBlob>( (string name) => { return container.GetBlockBlobReference(name); }, blobName, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount)); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to get cloud block blob {0} from Azure storage blob container {1}.", blobName, container.Name); throw; } }
private CloudBlobContainer GetContainer(string containerName) { try { return(BackupRestoreUtility.PerformWithRetries <string, CloudBlobContainer>( (string name) => { CloudBlobContainer container = this.GetContainerRef(name); // Create the container if (!container.Exists()) { throw new ArgumentException(string.Format("Given container {0} doesn't exist.", name), "containerName"); } return container; }, containerName, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount)); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to get container {0} from Azure storage.", containerName); throw; } }
private CloudBlobContainer GetOrCreateContainer(string containerName) { try { return(BackupRestoreUtility.PerformWithRetries <string, CloudBlobContainer>( (string name) => { CloudBlobContainer blobContainer = this.GetContainerRef(containerName); // Create the container blobContainer.CreateIfNotExists(null, null); return blobContainer; }, containerName, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount)); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to GetOrCreate container {0} with Azure storage.", containerName); throw; } }
private bool CopyFileToDestination(CloudBlockBlob blob, string destinationFullFileName, int retryCount) { CloudFileCopyInfo cloudFileCopyInfo = new CloudFileCopyInfo() { SourceBlob = blob, FullFileName = destinationFullFileName, RetryCount = retryCount }; try { BackupRestoreUtility.PerformWithRetries( this.CopyBlobToDestinationFile, cloudFileCopyInfo, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to copy blob: {0} to file: {1}.", blob.Name, destinationFullFileName); throw; } return(true); }
public void Upload(string sourceFileOrFolderPath, string destinationFolderName, bool sourceIsFile = false) { var containerName = this.GetContainerName(); var destinationFolder = Path.Combine(this.GetFolderPath(), destinationFolderName == null ? string.Empty : destinationFolderName); this.TraceSource.WriteInfo(TraceType, "Moving backup from {0} to {1}/{2}", sourceFileOrFolderPath, containerName, destinationFolder); CloudBlobContainer container = null; // Create container try { container = BackupRestoreUtility.PerformWithRetries <string, CloudBlobContainer>( this.GetOrCreateContainer, containerName, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to create container {0} on Azure storage.", containerName); throw; } string sourceFolder; if (!sourceIsFile) { sourceFolder = sourceFileOrFolderPath; } else { sourceFolder = Path.GetDirectoryName(sourceFileOrFolderPath); } if (sourceIsFile) { CopyFileToDestination(container, sourceFileOrFolderPath, destinationFolder, this.retryCount); } else { var sourceDirInfo = new DirectoryInfo(sourceFolder); // TODO: Add following files in concurrent bag and run loop with scheduled number of threads to upload these in parallel. foreach (var file in sourceDirInfo.GetFiles("*", SearchOption.AllDirectories)) { string sourceRelative = file.DirectoryName.Substring(sourceFolder.Trim('\\').Length).Trim('\\'); string effectiveDestinationPath = Path.Combine(destinationFolder, sourceRelative); this.TraceSource.WriteInfo(TraceType, "Full file name: {0} \t destinationFolder: {1} ", file.FullName, effectiveDestinationPath); CopyFileToDestination(container, file.FullName, effectiveDestinationPath, this.retryCount); } } }
private IEnumerable <IListBlobItem> GetBlobList(CloudBlobDirectory directory, bool useFlatBlobListing) { try { return(BackupRestoreUtility.PerformWithRetries <bool, IEnumerable <IListBlobItem> >( (bool flatBlobListing) => { return directory.ListBlobs(flatBlobListing); }, useFlatBlobListing, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount)); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to list blobs under directory {0} from Azure storage.", directory.Prefix); throw; } }
private CloudBlobDirectory GetCloudBlobDirectoryRef(CloudBlobContainer container, string directoryName) { try { return(BackupRestoreUtility.PerformWithRetries <string, CloudBlobDirectory>( (string relDirName) => { return container.GetDirectoryReference(relDirName); }, directoryName, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), this.retryCount)); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to get directory reference {0} from Azure storage.", directoryName); throw; } }
private bool CopyFileToDestination(CloudBlobContainer container, string source, string destinationPath, int retryCount) { // Get the name of the source file string sourceFileName = Path.GetFileName(source); // Create a list to hold the directory hierarchy of the source List <string> sourceRelativeParts = new List <string>(); string relativeDirectoryName = string.Empty; if (!string.IsNullOrWhiteSpace(destinationPath)) { // Add each directory in the directory hierarchy to the list string sourceRelativePartialPath = destinationPath; while (false == string.IsNullOrEmpty(sourceRelativePartialPath)) { string sourceRelativePart = Path.GetFileName(sourceRelativePartialPath); sourceRelativeParts.Add(sourceRelativePart); sourceRelativePartialPath = Path.GetDirectoryName(sourceRelativePartialPath); } // Reverse the list, so that top-level directories appear first sourceRelativeParts.Reverse(); // Compute the directory name under the container relativeDirectoryName = string.Join( "/", sourceRelativeParts.ToArray()); } // Copy the file to the Azure blob FileCopyInfo fileCopyInfo = new FileCopyInfo() { SourceFullPath = source, SourceFileName = sourceFileName, RelativeDirectoryName = relativeDirectoryName, RetryCount = retryCount, Container = container }; try { BackupRestoreUtility.PerformWithRetries( this.CopyFileToDestinationBlob, fileCopyInfo, new RetriableOperationExceptionHandler(this.AzureStorageExceptionHandler), retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to copy file {0} to Azure blob storage container {1}.", source, container.Name); throw; } return(true); }