Exemple #1
0
        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();
            }
        }
Exemple #2
0
        private async Task <bool> ItemEqualsExistingBlobAsync(
            ITaskItem item,
            string relativeBlobPath,
            UploadClient client,
            SemaphoreSlim clientThrottle)
        {
            await clientThrottle.WaitAsync();

            try
            {
                return(await client.FileEqualsExistingBlobAsync(
                           AccountName,
                           AccountKey,
                           ContainerName,
                           item.ItemSpec,
                           relativeBlobPath,
                           UploadTimeoutInMinutes));
            }
            finally
            {
                clientThrottle.Release();
            }
        }