From 343374d7c761eab9b6bf9ae3f2b65dfe21307eda Mon Sep 17 00:00:00 2001 From: Nate Amundson Date: Thu, 22 Sep 2016 19:02:53 -0700 Subject: [PATCH] Support building with custom container name Currently, using a custom container name is supported for only some publish steps. This commit makes it supported for all publish steps. This commit also ensures that the Overwrite property is specified on all calls to the UploadToAzure task. --- build/Microsoft.DotNet.Cli.Publish.targets | 4 +- .../SetBlobPropertiesBasedOnFileTypeTask.cs | 11 ++-- .../Publishing/AzurePublisher.cs | 52 +++++-------------- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/build/Microsoft.DotNet.Cli.Publish.targets b/build/Microsoft.DotNet.Cli.Publish.targets index d791c9d3b..2a3feae93 100644 --- a/build/Microsoft.DotNet.Cli.Publish.targets +++ b/build/Microsoft.DotNet.Cli.Publish.targets @@ -97,11 +97,13 @@ AccountKey="$(CloudDropAccessToken)" AccountName="$(CloudDropAccountName)" ContainerName="$(ContainerName)" - Items="@(CliVersionBadgeToUpload)" /> + Items="@(CliVersionBadgeToUpload)" + Overwrite="$(OverwriteOnPublish)" /> \ No newline at end of file diff --git a/build_projects/dotnet-cli-build/SetBlobPropertiesBasedOnFileTypeTask.cs b/build_projects/dotnet-cli-build/SetBlobPropertiesBasedOnFileTypeTask.cs index dd20d82b7..2c4bc4648 100644 --- a/build_projects/dotnet-cli-build/SetBlobPropertiesBasedOnFileTypeTask.cs +++ b/build_projects/dotnet-cli-build/SetBlobPropertiesBasedOnFileTypeTask.cs @@ -18,6 +18,9 @@ namespace Microsoft.DotNet.Cli.Build [Required] public string AccountKey { get; set; } + [Required] + public string ContainerName { get; set; } + [Required] public ITaskItem[] Items { get; set; } @@ -25,9 +28,9 @@ namespace Microsoft.DotNet.Cli.Build { get { - if(_azurePublisher == null) + if (_azurePublisher == null) { - _azurePublisher = new AzurePublisher(AccountName, AccountKey); + _azurePublisher = new AzurePublisher(AccountName, AccountKey, ContainerName); } return _azurePublisher; @@ -42,7 +45,7 @@ namespace Microsoft.DotNet.Cli.Build return false; } - foreach(var item in Items) + foreach (var item in Items) { string relativeBlobPath = item.GetMetadata("RelativeBlobPath"); if (string.IsNullOrEmpty(relativeBlobPath)) @@ -53,7 +56,7 @@ namespace Microsoft.DotNet.Cli.Build } AzurePublisherTool.SetBlobPropertiesBasedOnFileType(relativeBlobPath); - } + } return true; } diff --git a/build_projects/shared-build-targets-utils/Publishing/AzurePublisher.cs b/build_projects/shared-build-targets-utils/Publishing/AzurePublisher.cs index 922fb7753..b9e17e09a 100644 --- a/build_projects/shared-build-targets-utils/Publishing/AzurePublisher.cs +++ b/build_projects/shared-build-targets-utils/Publishing/AzurePublisher.cs @@ -22,42 +22,44 @@ namespace Microsoft.DotNet.Cli.Build Sdk, } - private const string s_dotnetBlobRootUrl = "https://dotnetcli.blob.core.windows.net/" + s_dotnetBlobContainerName; private const string s_dotnetBlobContainerName = "dotnet"; private string _connectionString { get; set; } + private string _containerName { get; set; } private CloudBlobContainer _blobContainer { get; set; } - public AzurePublisher() + public AzurePublisher(string containerName = s_dotnetBlobContainerName) { _connectionString = EnvVars.EnsureVariable("CONNECTION_STRING").Trim('"'); - _blobContainer = GetDotnetBlobContainer(_connectionString); + _containerName = containerName; + _blobContainer = GetDotnetBlobContainer(_connectionString, containerName); } - public AzurePublisher(string accountName, string accountKey) + public AzurePublisher(string accountName, string accountKey, string containerName = s_dotnetBlobContainerName) { - _blobContainer = GetDotnetBlobContainer(accountName, accountKey); + _containerName = containerName; + _blobContainer = GetDotnetBlobContainer(accountName, accountKey, containerName); } - private CloudBlobContainer GetDotnetBlobContainer(string connectionString) + private CloudBlobContainer GetDotnetBlobContainer(string connectionString, string containerName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); - return GetDotnetBlobContainer(storageAccount); + return GetDotnetBlobContainer(storageAccount, containerName); } - private CloudBlobContainer GetDotnetBlobContainer(string accountName, string accountKey) + private CloudBlobContainer GetDotnetBlobContainer(string accountName, string accountKey, string containerName) { var storageCredentials = new StorageCredentials(accountName, accountKey); var storageAccount = new CloudStorageAccount(storageCredentials, true); - return GetDotnetBlobContainer(storageAccount); + return GetDotnetBlobContainer(storageAccount, containerName); } - private CloudBlobContainer GetDotnetBlobContainer(CloudStorageAccount storageAccount) + private CloudBlobContainer GetDotnetBlobContainer(CloudStorageAccount storageAccount, string containerName) { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); - return blobClient.GetContainerReference(s_dotnetBlobContainerName); + return blobClient.GetContainerReference(containerName); } public string UploadFile(string file, Product product, string version) @@ -126,7 +128,7 @@ namespace Microsoft.DotNet.Cli.Build BlobContinuationToken continuationToken = new BlobContinuationToken(); var blobFiles = blobDir.ListBlobsSegmentedAsync(continuationToken).Result; - return blobFiles.Results.Select(bf => bf.Uri.PathAndQuery.Replace($"/{s_dotnetBlobContainerName}/", string.Empty)); + return blobFiles.Results.Select(bf => bf.Uri.PathAndQuery.Replace($"/{_containerName}/", string.Empty)); } public string AcquireLeaseOnBlob( @@ -219,35 +221,9 @@ namespace Microsoft.DotNet.Cli.Build _blobContainer.GetBlockBlobReference(path).DeleteAsync().Wait(); } - public static string CalculateFullUrlForFile(string file, Product product, string version) - { - return $"{s_dotnetBlobRootUrl}/{CalculateRelativePathForFile(file, product, version)}"; - } - private static string CalculateRelativePathForFile(string file, Product product, string version) { return $"{product}/{version}/{Path.GetFileName(file)}"; } - - public static async Task DownloadFile(string blobFilePath, string localDownloadPath) - { - var blobUrl = $"{s_dotnetBlobRootUrl}/{blobFilePath}"; - - using (var client = new HttpClient()) - { - var request = new HttpRequestMessage(HttpMethod.Get, blobUrl); - var sendTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); - var response = sendTask.Result.EnsureSuccessStatusCode(); - - var httpStream = await response.Content.ReadAsStreamAsync(); - - using (var fileStream = File.Create(localDownloadPath)) - using (var reader = new StreamReader(httpStream)) - { - httpStream.CopyTo(fileStream); - fileStream.Flush(); - } - } - } } }