private async ThreadingTask UploadAsync(CancellationToken ct, ITaskItem item, HashSet <string> blobsPresent, SemaphoreSlim clientThrottle) { if (ct.IsCancellationRequested) { Log.LogError("Task UploadToAzure cancelled"); ct.ThrowIfCancellationRequested(); } string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) { throw new Exception(string.Format("Metadata 'RelativeBlobPath' is missing for item '{0}'.", item.ItemSpec)); } if (!File.Exists(item.ItemSpec)) { throw new Exception(string.Format("The file '{0}' does not exist.", item.ItemSpec)); } UploadClient uploadClient = new UploadClient(Log); if (!Overwrite && blobsPresent.Contains(relativeBlobPath)) { if (PassIfExistingItemIdentical && await ItemEqualsExistingBlobAsync(item, relativeBlobPath, uploadClient, clientThrottle)) { return; } throw new Exception(string.Format("The blob '{0}' already exists.", relativeBlobPath)); } string contentType = item.GetMetadata("ContentType"); await clientThrottle.WaitAsync(); try { Log.LogMessage("Uploading {0} to {1}.", item.ItemSpec, ContainerName); await uploadClient.UploadBlockBlobAsync( ct, AccountName, AccountKey, ContainerName, item.ItemSpec, relativeBlobPath, contentType, UploadTimeoutInMinutes); } finally { clientThrottle.Release(); } }
private async ThreadingTask UploadAsync(CancellationToken ct, ITaskItem item, HashSet <string> blobsPresent) { bool result = true; if (ct.IsCancellationRequested) { Log.LogError("Task UploadToAzure cancelled"); ct.ThrowIfCancellationRequested(); } string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) { Log.LogError(string.Format("Metadata 'RelativeBlobPath' is missing for item '{0}'.", item.ItemSpec)); result = false; } if (!File.Exists(item.ItemSpec)) { Log.LogError(string.Format("The file '{0}' does not exist.", item.ItemSpec)); result = false; } if (!Overwrite && blobsPresent.Contains(relativeBlobPath)) { Log.LogError(string.Format("The blob '{0}' already exists.", relativeBlobPath)); result = false; } if (result) { Log.LogMessage("Uploading {0} to {1}.", item.ItemSpec, ContainerName); UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( ct, AccountName, AccountKey, ContainerName, item.ItemSpec, relativeBlobPath); } }
private async ThreadingTask UploadAsync(CancellationToken ct, ITaskItem item, HashSet<string> blobsPresent, SemaphoreSlim clientThrottle) { if (ct.IsCancellationRequested) { Log.LogError("Task UploadToAzure cancelled"); ct.ThrowIfCancellationRequested(); } string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) throw new Exception(string.Format("Metadata 'RelativeBlobPath' is missing for item '{0}'.", item.ItemSpec)); if (!File.Exists(item.ItemSpec)) throw new Exception(string.Format("The file '{0}' does not exist.", item.ItemSpec)); if (!Overwrite && blobsPresent.Contains(relativeBlobPath)) throw new Exception(string.Format("The blob '{0}' already exists.", relativeBlobPath)); await clientThrottle.WaitAsync(); try { Log.LogMessage("Uploading {0} to {1}.", item.ItemSpec, ContainerName); UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( ct, AccountName, AccountKey, ContainerName, item.ItemSpec, relativeBlobPath); } finally { clientThrottle.Release(); } }