public async Task <bool> CreateFeedContainer() { ValidateContainerName(ContainerName); string url = $"{FeedContainerUrl}?restype=container"; using (HttpClient client = new HttpClient()) { List <Tuple <string, string> > additionalHeaders = null; if (IsPublic) { Tuple <string, string> headerBlobType = new Tuple <string, string>("x-ms-blob-public-access", "blob"); additionalHeaders = new List <Tuple <string, string> >() { headerBlobType }; } var createRequest = AzureHelper.RequestMessage("PUT", url, AccountName, AccountKey, additionalHeaders); using (HttpResponseMessage response = await AzureHelper.RequestWithRetry(Log, client, createRequest)) { try { Log.LogMessage( MessageImportance.Low, "Received response to create Container {0}: Status Code: {1} {2}", ContainerName, response.StatusCode, response.Content.ToString()); } catch (Exception e) { Log.LogErrorFromException(e, true); } } } string item = GenerateRootServiceIndex(RelativePath); string uploadPath = CalculateBlobPath(item, RelativePath); Log.LogMessage($"Uploading root index.json to {uploadPath}."); try { UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( CancellationToken, AccountName, AccountKey, ContainerName, item, uploadPath); } catch (Exception e) { Log.LogErrorFromException(e); } return(!Log.HasLoggedErrors); }
public async Task UploadAssets(ITaskItem item, SemaphoreSlim clientThrottle, bool allowOverwrite = false) { string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) { string fileName = Path.GetFileName(item.ItemSpec); string recursiveDir = item.GetMetadata("RecursiveDir"); relativeBlobPath = $"{feed.RelativePath}{recursiveDir}{fileName}"; } relativeBlobPath = relativeBlobPath.Replace("\\", "/"); Log.LogMessage($"Uploading {relativeBlobPath}"); await clientThrottle.WaitAsync(); try { bool blobExists = false; if (!allowOverwrite) { blobExists = await feed.CheckIfBlobExists(relativeBlobPath); } if (allowOverwrite || !blobExists) { Log.LogMessage($"Uploading {item} to {relativeBlobPath}."); UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( CancellationToken, feed.AccountName, feed.AccountKey, feed.ContainerName, item.ItemSpec, relativeBlobPath); } else { Log.LogError($"Item '{item}' already exists in {relativeBlobPath}."); } } catch (Exception exc) { Log.LogError($"Unable to upload to {relativeBlobPath} due to {exc}."); throw; } finally { clientThrottle.Release(); } }
private async Task UploadAsync(CancellationToken ct, string item, string uploadPath, SemaphoreSlim clientThrottle, bool isLeaseRequired) { if (!File.Exists(item)) { throw new Exception(string.Format("The file '{0}' does not exist.", item)); } await clientThrottle.WaitAsync(); string leaseId = string.Empty; AzureBlobLease blobLease = new AzureBlobLease(feed.AccountName, feed.AccountKey, string.Empty, feed.ContainerName, uploadPath, Log, "60", "10"); if (isLeaseRequired) { try { leaseId = blobLease.Acquire(); } catch (Exception) { Log.LogError($"Unable to obtain lease on {uploadPath}"); } } try { Log.LogMessage($"Uploading {item} to {uploadPath}."); UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( ct, feed.AccountName, feed.AccountKey, feed.ContainerName, item, uploadPath, leaseId); } catch (Exception) { Log.LogError($"Unable to upload to {uploadPath}"); } finally { if (isLeaseRequired) { blobLease.Release(); } clientThrottle.Release(); } }
public async Task UploadAssetAsync( ITaskItem item, SemaphoreSlim clientThrottle, int uploadTimeout, PushOptions options) { string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) { string fileName = Path.GetFileName(item.ItemSpec); string recursiveDir = item.GetMetadata("RecursiveDir"); relativeBlobPath = $"{recursiveDir}{fileName}"; } string contentType = item.GetMetadata("ContentType"); relativeBlobPath = $"{feed.RelativePath}{relativeBlobPath}".Replace("\\", "/"); if (relativeBlobPath.Contains("//")) { Log.LogError( $"Item '{item.ItemSpec}' RelativeBlobPath contains virtual directory " + $"without name (double forward slash): '{relativeBlobPath}'"); return; } Log.LogMessage($"Uploading {relativeBlobPath}"); await clientThrottle.WaitAsync(); try { UploadClient uploadClient = new UploadClient(Log); if (!options.AllowOverwrite && await feed.CheckIfBlobExistsAsync(relativeBlobPath)) { if (options.PassIfExistingItemIdentical) { if (!await uploadClient.FileEqualsExistingBlobAsync( feed.AccountName, feed.AccountKey, feed.ContainerName, item.ItemSpec, relativeBlobPath, uploadTimeout)) { Log.LogError( $"Item '{item}' already exists with different contents " + $"at '{relativeBlobPath}'"); } } else { Log.LogError($"Item '{item}' already exists at '{relativeBlobPath}'"); } } else { Log.LogMessage($"Uploading {item} to {relativeBlobPath}."); await uploadClient.UploadBlockBlobAsync( CancellationToken, feed.AccountName, feed.AccountKey, feed.ContainerName, item.ItemSpec, relativeBlobPath, contentType, uploadTimeout); } } catch (Exception exc) { Log.LogError($"Unable to upload to {relativeBlobPath} due to {exc}."); throw; } finally { clientThrottle.Release(); } }