Beispiel #1
0
        public async Task UploadAssetAsync(
            ITaskItem item,
            PushOptions options,
            SemaphoreSlim clientThrottle = null)
        {
            string relativeBlobPath = item.GetMetadata("RelativeBlobPath");

            if (string.IsNullOrEmpty(relativeBlobPath))
            {
                string fileName     = Path.GetFileName(item.ItemSpec);
                string recursiveDir = item.GetMetadata("RecursiveDir");
                relativeBlobPath = $"{recursiveDir}{fileName}";
            }

            if (!string.IsNullOrEmpty(relativeBlobPath))
            {
                relativeBlobPath = $"{RelativePath}{relativeBlobPath}".Replace("\\", "/");

                if (relativeBlobPath.StartsWith("//"))
                {
                    Log.LogError(
                        $"Item '{item.ItemSpec}' RelativeBlobPath contains virtual directory " +
                        $"without name (double forward slash): '{relativeBlobPath}'");
                    return;
                }

                Log.LogMessage($"Uploading {relativeBlobPath}");

                if (clientThrottle != null)
                {
                    await clientThrottle.WaitAsync();
                }

                try
                {
                    AzureStorageUtils blobUtils = new AzureStorageUtils(AccountName, AccountKey, ContainerName);

                    if (!options.AllowOverwrite && await blobUtils.CheckIfBlobExistsAsync(relativeBlobPath))
                    {
                        if (options.PassIfExistingItemIdentical)
                        {
                            if (!await blobUtils.IsFileIdenticalToBlobAsync(item.ItemSpec, relativeBlobPath))
                            {
                                Log.LogError(
                                    $"Item '{item}' already exists with different contents " +
                                    $"at '{relativeBlobPath}'");
                            }
                        }
                        else
                        {
                            Log.LogError($"Item '{item}' already exists at '{relativeBlobPath}'");
                        }
                    }
                    else
                    {
                        using (FileStream stream =
                                   new FileStream(item.ItemSpec, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            Log.LogMessage($"Uploading {item} to {relativeBlobPath}.");
                            await blobUtils.UploadBlockBlobAsync(item.ItemSpec, relativeBlobPath, stream);
                        }
                    }
                }
                catch (Exception exc)
                {
                    Log.LogError(
                        $"Unable to upload to {relativeBlobPath} in Azure Storage account {AccountName}/{ContainerName} due to {exc}.");
                    throw;
                }
                finally
                {
                    if (clientThrottle != null)
                    {
                        clientThrottle.Release();
                    }
                }
            }
            else
            {
                Log.LogError($"Relative blob path is empty.");
            }
        }