private async Task <bool> ExecuteAsync()
        {
            try
            {
                string accessId          = IsInternal ? "int" : "pub";
                string baseFeedName      = $"darc-{accessId}-{RepositoryName}-{CommitSha}";
                string versionedFeedName = baseFeedName;
                bool   needsUniqueName   = false;
                int    subVersion        = 0;
                var    containerName     = string.Empty;

                Log.LogMessage(MessageImportance.High, $"Creating a new Azure Storage internal feed ...");

                Match m = Regex.Match(AzureStorageFeedsBaseUrl, baseUrlRegex);
                if (m.Success)
                {
                    containerName = m.Groups["containername"].Value;
                }
                else
                {
                    Log.LogError($"Could not parse {nameof(AzureStorageFeedsBaseUrl)} to extract the container name: '{AzureStorageFeedsBaseUrl}'");
                    return(false);
                }

                AzureStorageUtils azUtils = new AzureStorageUtils(AzureStorageAccountName, AzureStorageAccountKey, containerName);

                // Create container if it doesn't already exist
                if (!await azUtils.CheckIfContainerExistsAsync())
                {
                    BlobContainerPermissions permissions = new BlobContainerPermissions
                    {
                        PublicAccess = IsInternal ? BlobContainerPublicAccessType.Off : BlobContainerPublicAccessType.Container
                    };

                    await azUtils.CreateContainerAsync(permissions);
                }

                // Create folder inside the container. Note that AzureStorage requires a folder
                // to have at least one file.
                do
                {
                    if (await azUtils.CheckIfBlobExistsAsync($"{versionedFeedName}/index.json"))
                    {
                        versionedFeedName = $"{baseFeedName}-{++subVersion}";
                        needsUniqueName   = true;
                    }
                    else
                    {
                        baseFeedName    = versionedFeedName;
                        needsUniqueName = false;
                    }
                } while (needsUniqueName);

                // Initialize the feed using sleet
                SleetSource sleetSource = new SleetSource()
                {
                    Name             = baseFeedName,
                    Type             = "azure",
                    BaseUri          = $"{AzureStorageFeedsBaseUrl}{baseFeedName}",
                    AccountName      = AzureStorageAccountName,
                    Container        = containerName,
                    FeedSubPath      = $"{baseFeedName}",
                    ConnectionString = $"DefaultEndpointsProtocol=https;AccountName={AzureStorageAccountName};AccountKey={AzureStorageAccountKey};EndpointSuffix=core.windows.net"
                };

                BlobFeedAction bfAction = new BlobFeedAction(sleetSource, AzureStorageAccountKey, Log);
                await bfAction.InitAsync();

                TargetFeedURL = $"{AzureStorageFeedsBaseUrl}{baseFeedName}";

                Log.LogMessage(MessageImportance.High, $"Feed '{TargetFeedURL}' created successfully!");
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e, true);
            }

            return(!Log.HasLoggedErrors);
        }
示例#2
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.");
            }
        }