From e53fdf0981dab11c34798f4b9a7f1be016bf272a Mon Sep 17 00:00:00 2001 From: tg-msft Date: Sun, 20 Oct 2019 20:11:22 -0700 Subject: [PATCH] Storage: Rename to DownloadTo and use string instead of FileInfo Fixes #8241 --- .../Azure.Storage.Blobs/src/BlobBaseClient.cs | 244 +++++++----------- .../tests/BlobBaseClientTests.cs | 16 +- 2 files changed, 99 insertions(+), 161 deletions(-) diff --git a/sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs b/sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs index 3c8dacab12c2..d7fda262fedb 100644 --- a/sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs +++ b/sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs @@ -778,7 +778,7 @@ await BlobRestClient.Blob.DownloadAsync( #region Parallel Download /// - /// The operation downloads a blob using parallel requests, + /// The operation downloads a blob using parallel requests, /// and writes the content to . /// /// @@ -792,17 +792,17 @@ await BlobRestClient.Blob.DownloadAsync( /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Response Download(Stream destination) => - Download(destination, CancellationToken.None); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual Response DownloadTo(Stream destination) => + DownloadTo(destination, CancellationToken.None); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The operation downloads a blob using parallel requests, - /// and writes the content to . + /// The operation downloads a blob using parallel requests, + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// A describing the @@ -812,13 +812,13 @@ public virtual Response Download(Stream destination) => /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Response Download(FileInfo destination) => - Download(destination, CancellationToken.None); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual Response DownloadTo(string path) => + DownloadTo(path, CancellationToken.None); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The downloads a blob using parallel requests, + /// The downloads a blob using parallel requests, /// and writes the content to . /// /// @@ -832,17 +832,17 @@ public virtual Response Download(FileInfo destination) => /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Task> DownloadAsync(Stream destination) => - DownloadAsync(destination, CancellationToken.None); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual async Task> DownloadToAsync(Stream destination) => + await DownloadToAsync(destination, CancellationToken.None).ConfigureAwait(false); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The downloads a blob using parallel requests, - /// and writes the content to . + /// The downloads a blob using parallel requests, + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// A describing the @@ -852,13 +852,13 @@ public virtual Task> DownloadAsync(Stream destination) /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Task> DownloadAsync(FileInfo destination) => - DownloadAsync(destination, CancellationToken.None); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual async Task> DownloadToAsync(string path) => + await DownloadToAsync(path, CancellationToken.None).ConfigureAwait(false); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The operation + /// The operation /// downloads a blob using parallel requests, /// and writes the content to . /// @@ -877,23 +877,23 @@ public virtual Task> DownloadAsync(FileInfo destination /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Response Download( + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual Response DownloadTo( Stream destination, CancellationToken cancellationToken) => - Download( + DownloadTo( destination, accessConditions: default, // Pass anything else so we don't recurse on this overload cancellationToken: cancellationToken); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The operation + /// The operation /// downloads a blob using parallel requests, - /// and writes the content to . + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// Optional to propagate @@ -907,18 +907,18 @@ public virtual Response Download( /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Response Download( - FileInfo destination, + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual Response DownloadTo( + string path, CancellationToken cancellationToken) => - Download( - destination, + DownloadTo( + path, accessConditions: default, // Pass anything else so we don't recurse on this overload cancellationToken: cancellationToken); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The operation + /// The operation /// downloads a blob using parallel requests, /// and writes the content to . /// @@ -937,23 +937,24 @@ public virtual Response Download( /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Task> DownloadAsync( + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual async Task> DownloadToAsync( Stream destination, CancellationToken cancellationToken) => - DownloadAsync( + await DownloadToAsync( destination, accessConditions: default, // Pass anything else so we don't recurse on this overload - cancellationToken: cancellationToken); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + cancellationToken: cancellationToken) + .ConfigureAwait(false); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The operation + /// The operation /// downloads a blob using parallel requests, - /// and writes the content to . + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// Optional to propagate @@ -967,18 +968,19 @@ public virtual Task> DownloadAsync( /// A will be thrown if /// a failure occurs. /// -#pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter - public virtual Task> DownloadAsync( - FileInfo destination, + #pragma warning disable AZC0002 // Client method should have cancellationToken as the last optional parameter + public virtual async Task> DownloadToAsync( + string path, CancellationToken cancellationToken) => - DownloadAsync( - destination, + await DownloadToAsync( + path, accessConditions: default, // Pass anything else so we don't recurse on this overload - cancellationToken: cancellationToken); -#pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter + cancellationToken: cancellationToken) + .ConfigureAwait(false); + #pragma warning restore AZC0002 // Client method should have cancellationToken as the last optional parameter /// - /// The + /// The /// operation downloads a blob using parallel requests, /// and writes the content to . /// @@ -1005,7 +1007,7 @@ public virtual Task> DownloadAsync( /// A will be thrown if /// a failure occurs. /// - public virtual Response Download( + public virtual Response DownloadTo( Stream destination, BlobAccessConditions? accessConditions = default, ///// @@ -1025,12 +1027,12 @@ public virtual Response Download( .EnsureCompleted(); /// - /// The + /// The /// operation downloads a blob using parallel requests, - /// and writes the content to . + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// Optional to add conditions on @@ -1052,8 +1054,8 @@ public virtual Response Download( /// A will be thrown if /// a failure occurs. /// - public virtual Response Download( - FileInfo destination, + public virtual Response DownloadTo( + string path, BlobAccessConditions? accessConditions = default, ///// ///// Optional to provide @@ -1061,8 +1063,10 @@ public virtual Response Download( ///// //IProgress progressHandler = default, StorageTransferOptions transferOptions = default, - CancellationToken cancellationToken = default) => - StagedDownloadAsync( + CancellationToken cancellationToken = default) + { + using Stream destination = File.Create(path); + return StagedDownloadAsync( destination, accessConditions, //progressHandler, @@ -1070,9 +1074,10 @@ public virtual Response Download( async: false, cancellationToken: cancellationToken) .EnsureCompleted(); + } /// - /// The + /// The /// operation downloads a blob using parallel requests, /// and writes the content to . /// @@ -1099,7 +1104,7 @@ public virtual Response Download( /// A will be thrown if /// a failure occurs. /// - public virtual Task> DownloadAsync( + public virtual async Task> DownloadToAsync( Stream destination, BlobAccessConditions? accessConditions = default, ///// @@ -1109,21 +1114,22 @@ public virtual Task> DownloadAsync( //IProgress progressHandler = default, StorageTransferOptions transferOptions = default, CancellationToken cancellationToken = default) => - StagedDownloadAsync( + await StagedDownloadAsync( destination, accessConditions, //progressHandler, transferOptions: transferOptions, async: true, - cancellationToken: cancellationToken); + cancellationToken: cancellationToken) + .ConfigureAwait(false); /// - /// The + /// The /// operation downloads a blob using parallel requests, - /// and writes the content to . + /// and writes the content to . /// - /// - /// A representing a file to write the downloaded content to. + /// + /// A file path to write the downloaded content to. /// /// /// Optional to add conditions on @@ -1145,8 +1151,8 @@ public virtual Task> DownloadAsync( /// A will be thrown if /// a failure occurs. /// - public virtual Task> DownloadAsync( - FileInfo destination, + public virtual async Task> DownloadToAsync( + string path, BlobAccessConditions? accessConditions = default, ///// ///// Optional to provide @@ -1154,14 +1160,18 @@ public virtual Task> DownloadAsync( ///// //IProgress progressHandler = default, StorageTransferOptions transferOptions = default, - CancellationToken cancellationToken = default) => - StagedDownloadAsync( + CancellationToken cancellationToken = default) + { + using Stream destination = File.Create(path); + return await StagedDownloadAsync( destination, accessConditions, //progressHandler, transferOptions: transferOptions, async: true, - cancellationToken: cancellationToken); + cancellationToken: cancellationToken) + .ConfigureAwait(false); + } /// /// This operation will download a blob of arbitrary size by downloading it as indiviually staged @@ -1280,78 +1290,6 @@ Task> DownloadPartitionAsync(ETag eTag, HttpRange htt static Task WritePartitionAsync(Response response, Stream destination, bool async, CancellationToken ct) => response.Value.Content.CopyToAsync(destination); } - - /// - /// This operation will download a blob of arbitrary size by downloading it as indiviually staged - /// partitions if it's larger than the - /// . - /// - /// - /// A representing a file to write the downloaded content to. - /// - /// - /// Optional to add conditions on - /// the creation of this new block blob. - /// - /// - /// The maximum size stream that we'll download as a single block. The - /// default value is 256MB. - /// - /// - /// Optional to configure - /// parallel transfer behavior. - /// - /// - /// Whether to invoke the operation asynchronously. - /// - /// - /// Optional to propagate - /// notifications that the operation should be cancelled. - /// - /// - /// A describing the - /// blob's properties. - /// - /// - /// A will be thrown if - /// a failure occurs. - /// - internal Task> StagedDownloadAsync( - FileInfo destination, - BlobAccessConditions? accessConditions = default, - ///// - ///// Optional to provide - ///// progress updates about data transfers. - ///// - //IProgress progressHandler, - long singleBlockThreshold = Constants.Blob.Block.MaxDownloadBytes, - StorageTransferOptions transferOptions = default, - bool async = true, - CancellationToken cancellationToken = default) - { - FileStream stream = destination.OpenWrite(); - - return StagedDownloadAsync( - stream, - accessConditions, - //progressHandler, - singleBlockThreshold, - transferOptions, - async, - cancellationToken - ).ContinueWith( - t => - { - stream.Flush(); - stream.Dispose(); - - return t.Result; - }, - CancellationToken.None, - TaskContinuationOptions.RunContinuationsAsynchronously, - TaskScheduler.Default - ); - } #endregion Parallel Download #region StartCopyFromUri diff --git a/sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs b/sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs index 2ea7ae87d60b..5a4eed029eff 100644 --- a/sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs +++ b/sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs @@ -416,8 +416,6 @@ private async Task ParallelDownloadFileAndVerify( await blob.UploadAsync(stream); } - var destination = new FileInfo(path); - // Create a special blob client for downloading that will // assign client request IDs based on the range so that out // of order operations still get predictable IDs and the @@ -425,13 +423,15 @@ private async Task ParallelDownloadFileAndVerify( var credential = new StorageSharedKeyCredential(this.TestConfigDefault.AccountName, this.TestConfigDefault.AccountKey); var downloadingBlob = this.InstrumentClient(new BlobClient(blob.Uri, credential, GetOptions(true))); - await downloadingBlob.StagedDownloadAsync( - destination, - singleBlockThreshold: singleBlockThreshold, - transferOptions: transferOptions - ); + using (FileStream file = File.OpenWrite(path)) + { + await downloadingBlob.StagedDownloadAsync( + file, + singleBlockThreshold: singleBlockThreshold, + transferOptions: transferOptions); + } - using (FileStream resultStream = destination.OpenRead()) + using (FileStream resultStream = File.OpenRead(path)) { TestHelper.AssertSequenceEqual(data, resultStream.AsBytes()); }