protected async Task <bool> UploadSignature() { UploadOptions options = new UploadOptions(); SingleTransferContext context = new SingleTransferContext(); bool transferred = false; context.LogLevel = LogLevel.Informational; context.ProgressHandler = new SingleFileTransferProgressReporter(SignatureFile); context.SetAttributesCallback = (destination) => { SignatureBlob = destination as CloudBlockBlob; SignatureBlob.Properties.ContentType = "application/octet-stream"; }; context.ShouldOverwriteCallback = (source, destination) => { return(true); }; context.FileTransferred += (sender, e) => { Context_FileTransferred(sender, e); transferred = true; }; context.FileFailed += (sender, e) => { Context_FileFailed(sender, e); transferred = false; }; context.FileSkipped += (sender, e) => { Context_FileSkipped(sender, e); transferred = false; }; try { SignatureBlob = await DestinationStorage.GetorCreateCloudBlobAsync(DestinationContainerName, DestinationBlob.Name + ".sig", BlobType.BlockBlob) as CloudBlockBlob; if (await SignatureBlob.ExistsAsync()) { L.Warn("The existing signature blob {blob} will be overwritten.", SignatureBlob.Name); } await TransferManager.UploadAsync(SignatureFile.FullName, SignatureBlob, options, context, CT); transferred = true; } catch (Exception e) { LogTransferException(e, $"upload signature to cloud blob {SignatureBlob.Name}"); if (e is OperationCanceledException || e is TaskCanceledException || (e is TransferException && CT.IsCancellationRequested)) { transferred = true; } else { transferred = false; } } return(transferred); }
protected async Task <bool> UploadSingleFile(string fileName, bool writeSignature = false) { FileInfo file = new FileInfo(fileName); L.Info("{file} has size {size}", file.FullName, PrintBytes(file.Length, "")); if (string.IsNullOrEmpty(JournalFilePath)) { JournalFilePath = file.FullName + ".azsj"; } if (writeSignature && string.IsNullOrEmpty(SignatureFilePath)) { SignatureFilePath = file.FullName + ".sig"; } OpenTransferJournal(); UploadOptions options = new UploadOptions(); SingleTransferContext context = new SingleTransferContext(JournalStream); context.LogLevel = LogLevel.Informational; context.ProgressHandler = new SingleFileTransferProgressReporter(file); context.SetAttributesCallback = (destination) => { DestinationBlob = destination as CloudBlob; DestinationBlob.Properties.ContentType = this.ContentType; }; context.ShouldOverwriteCallback = (source, destination) => { CloudBlob b = (CloudBlob)destination; bool o = this.Overwrite; if (o) { L.Info("Overwriting existing blob {container}/{blob}.", DestinationContainerName, b.Name); } else { L.Warn("Not overwriting existing blob {container}/{blob}.", DestinationContainerName, b.Name); } return(o); }; context.FileTransferred += Context_FileTransferred; context.FileFailed += Context_FileFailed; context.FileSkipped += Context_FileSkipped; try { DestinationBlob = await DestinationStorage.GetorCreateCloudBlobAsync(DestinationContainerName, file.Name, BlobType.BlockBlob); if (writeSignature) { Signature = new SingleFileSignature(file, DestinationBlob); ComputeSignatureTask = Task.Factory.StartNew(() => Signature.Compute(), CT, TaskCreationOptions.None, TaskScheduler.Default); } else { ComputeSignatureTask = Task.CompletedTask; } using (Operation azOp = L.Begin("Upload file")) { TransferTask = TransferManager.UploadAsync(file.FullName, DestinationBlob, options, context, CT); Task.WaitAll(TransferTask, ComputeSignatureTask); if (TransferTask.Status == TaskStatus.RanToCompletion) { if (!writeSignature) { azOp.Complete(); return(true); } else { azOp.Complete(); } } else { return(false); } if (writeSignature && ComputeSignatureTask.Status == TaskStatus.RanToCompletion) { L.Info("Compute file signature has length {0} bytes.", Signature.ComputedSignature.Length); } else if (writeSignature) { L.Error("Could not compute signature for file {file}.", file.FullName); return(false); } } } catch (Exception e) { LogTransferException(e, "upload file"); if (e is AggregateException && e.InnerException != null && (e.InnerException is TaskCanceledException || e.InnerException is OperationCanceledException || e.InnerException is TransferSkippedException)) { return(true); } else if (e is TaskCanceledException || e is OperationCanceledException || e is TransferSkippedException) { return(true); } else { return(false); } } finally { CloseTransferJournal(); } using (Operation azOp = L.Begin("Upload and write file signature to disk")) { WriteSignatureTask = Task.Run(() => WriteSignatureToFile(), CT); try { WriteSignatureTask.Wait(); if (WriteSignatureTask.Result) { UploadSignatureTask = UploadSignature(); if (await UploadSignatureTask) { azOp.Complete(); return(true); } else { return(false); } } else { return(false); } } catch (Exception e) { LogTransferException(e, "upload and write file signature to disk"); azOp.Cancel(); if (e is TaskCanceledException || e is OperationCanceledException) { return(true); } else { return(false); } } } }