public async Task RunAsync(TransferReport transferReport) { var container = _blobCopier.GetSourceContainer(); BlobContinuationToken continuationToken = null; try { // // Enumerate the result segment for the source blobs in order to copy each blob. // When the continuation token is null, the last segment has been returned and exit the loop. // do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync( string.Empty, // prefix true, // useFlatBlobListing BlobListingDetails.Metadata, // blobListingDetails null, // maxResults continuationToken, // currentToken null, // options null); // operationContext foreach (var blobItem in resultSegment.Results) { // // Copy each blob // CloudBlob blob = (CloudBlob)blobItem; await CopyBlob(blob, transferReport); } // Get the continuation token and loop until it is null. continuationToken = resultSegment.ContinuationToken; } while (continuationToken != null); } catch (StorageException e) { _logger.LogWarning($"Failed to copy blobs. HTTP error code {e.RequestInformation.HttpStatusCode}: {e.RequestInformation.ErrorCode}"); throw; } _logger.LogInformation($"Total blobs successfully copied: {transferReport.CopyBlobs.Succeeded.Count}."); _logger.LogInformation($"Total blobs skipped: {transferReport.CopyBlobs.Skipped.Count}."); _logger.LogInformation($"Total blobs failed in copying: {transferReport.CopyBlobs.Failed.Count}."); }
private async Task CopyBlob(CloudBlob blob, TransferReport transferReport) { string blobName = blob.Name; string blobUri = blob.Uri.AbsoluteUri; const string metadataName = "mcrexport"; transferReport.ListSourceBlob.Succeeded.Add(blobName); try { Tuple <bool, string> tupleResult = await _blobCopier.GetContentMD5(blobName); bool succeededToGetContentMD5 = tupleResult.Item1; var contentMD5 = tupleResult.Item2; if (!succeededToGetContentMD5) { _logger.LogError($"Failed to copy: {blobUri}"); transferReport.CopyBlobs.Failed.Add(blobName); return; } if (String.IsNullOrEmpty(contentMD5)) { _logger.LogDebug($"Skip to copy: {blobUri}"); transferReport.CopyBlobs.Skipped.Add(blobName); return; } _logger.LogInformation($"Begin to copy: {blobUri}:{contentMD5}"); await _blobCopier.SetMetadata(blobName, metadataName, contentMD5); await _blobCopier.CopyAsync(blobName); _logger.LogInformation($"End to copy: {blobUri}:{contentMD5}"); transferReport.CopyBlobs.Succeeded.Add(blobName); } catch (Exception e) { _logger.LogError($"Blob {blobName}: failed to copy, exception: {e.ToString()}."); transferReport.CopyBlobs.Failed.Add(blobName); await _blobCopier.SetMetadata(blobName, metadataName, "failed"); } }
public static async Task Main(string[] args) { var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); var logger = loggerFactory.CreateLogger("Program"); if (!InitializeBlobTransfer(logger)) { return; } var config = GetConfig(); var configuration = config.GetSection("BlobTransfer").Get <BlobTransferConfiguration>(); var report = new TransferReport(); var worker = new BlobTransferWorker(configuration, loggerFactory); try { await worker.RunAsync(report); } catch (Exception e) { logger.LogError($"Unexpected exception: {e.ToString()}."); Environment.ExitCode = ERROR_FAIL; return; } await WriteFileAsync(TransferReportNamePrefix, report); Environment.ExitCode = ERROR_SUCCESS; logger.LogInformation("Done!"); }