diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/CHANGELOG.md b/sdk/storage/Azure.Storage.DataMovement.Blobs/CHANGELOG.md index 8e7359f5f4bc8..1021b06b0acc7 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/CHANGELOG.md +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/CHANGELOG.md @@ -16,8 +16,13 @@ - Renamed `BlobStorageResourceContainerOptions.BlobDirectoryPrefix` to `BlobPrefix` - Changed `BlobContainerClient.StartUploadDirectoryAsync` to `BlobContainerClient.UploadDirectoryAsync` and added a required `waitUntil` parameter. - Changed `BlobContainerClient.StartDownloadToDirectoryAsync` to `BlobContainerClient.DownloadToDirectoryAsync` and added a required `waitUntil` parameter. -- Removed `BlobsStorageResourceProvider()` empty default constructor -- Changed `BlobsStorageResourceProvider.FromClient` methods to `static` method +- Several refactors to `BlobsStorageResourceProvider`: + - Removed nested delegates `GetStorageSharedKeyCredential`, `GetTokenCredential`, and `GetAzureSasCredential`. + - Removed default constructor. + - Removed constructor overload for `GetTokenCredential` entirely. + - Changed constructor overloads for `GetStorageSharedKeyCredential` and `GetAzureSasCredential` to use `Func`. These callbacks are also now async, returning a `ValueTask`, and the `readOnly` parameter was removed. + - Changed `FromBlob` and `FromContainer` to async, returning a `ValueTask`, and renamed to `FromBlobAsync` and `FromContainerAsync` respectively. + - Changed `FromClient` methods to `static` methods. ### Bugs Fixed diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/README.md b/sdk/storage/Azure.Storage.DataMovement.Blobs/README.md index ffbef6378ac1c..64cdb90807a66 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/README.md +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/README.md @@ -95,17 +95,17 @@ BlobsStorageResourceProvider blobs = new(tokenCredential); To create a blob `StorageResource`, use the methods `FromBlob` or `FromContainer`. ```C# Snippet:ResourceConstruction_Blobs -StorageResource container = blobs.FromContainer( +StorageResource container = await blobs.FromContainerAsync( new Uri("https://myaccount.blob.core.windows.net/container")); // Block blobs are the default if no options are specified -StorageResource blockBlob = blobs.FromBlob( +StorageResource blockBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-block"), new BlockBlobStorageResourceOptions()); -StorageResource pageBlob = blobs.FromBlob( +StorageResource pageBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-page"), new PageBlobStorageResourceOptions()); -StorageResource appendBlob = blobs.FromBlob( +StorageResource appendBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-append"), new AppendBlobStorageResourceOptions()); ``` @@ -154,7 +154,7 @@ Upload a block blob. ```C# Snippet:SimpleBlobUpload TransferOperation transferOperation = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalPath), - destinationResource: blobs.FromBlob(destinationBlobUri)); + destinationResource: await blobs.FromBlobAsync(destinationBlobUri)); await transferOperation.WaitForCompletionAsync(); ``` @@ -163,7 +163,7 @@ Upload a directory as a specific blob type. ```C# Snippet:SimpleDirectoryUpload TransferOperation transferOperation = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromDirectory(sourcePath), - destinationResource: blobs.FromContainer( + destinationResource: await blobs.FromContainerAsync( blobContainerUri, new BlobStorageResourceContainerOptions() { @@ -181,7 +181,7 @@ Download a blob. ```C# Snippet:SimpleBlockBlobDownload TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromBlob(sourceBlobUri), + sourceResource: await blobs.FromBlobAsync(sourceBlobUri), destinationResource: LocalFilesStorageResourceProvider.FromFile(downloadPath)); await transferOperation.WaitForCompletionAsync(); ``` @@ -190,7 +190,7 @@ Download a container which may contain a mix of blob types. ```C# Snippet:SimpleDirectoryDownload_Blob TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromContainer( + sourceResource: await blobs.FromContainerAsync( blobContainerUri, new BlobStorageResourceContainerOptions() { @@ -208,8 +208,8 @@ Copy a single blob. Note the destination blob is an append blob, regardless of t ```C# Snippet:s2sCopyBlob TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromBlob(sourceBlobUri), - destinationResource: blobs.FromBlob(destinationBlobUri, new AppendBlobStorageResourceOptions())); + sourceResource: await blobs.FromBlobAsync(sourceBlobUri), + destinationResource: await blobs.FromBlobAsync(destinationBlobUri, new AppendBlobStorageResourceOptions())); await transferOperation.WaitForCompletionAsync(); ``` @@ -217,13 +217,13 @@ Copy a blob container. ```C# Snippet:s2sCopyBlobContainer TransferOperation transferOperation = await transferManager.StartTransferAsync( -sourceResource: blobs.FromContainer( +sourceResource: await blobs.FromContainerAsync( sourceContainerUri, new BlobStorageResourceContainerOptions() { BlobPrefix = sourceDirectoryName }), -destinationResource: blobs.FromContainer( +destinationResource: await blobs.FromContainerAsync( destinationContainerUri, new BlobStorageResourceContainerOptions() { diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net6.0.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net6.0.cs index 49f09a298f0d1..5a91558bcd5fd 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net6.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net6.0.cs @@ -26,22 +26,18 @@ public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.S { public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { } public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public BlobsStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public BlobsStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } - public Azure.Storage.DataMovement.StorageResource FromBlob(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null) { throw null; } + public System.Threading.Tasks.ValueTask FromBlobAsync(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.BlobContainerClient client, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.AppendBlobClient client, Azure.Storage.DataMovement.Blobs.AppendBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.BlockBlobClient client, Azure.Storage.DataMovement.Blobs.BlockBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.PageBlobClient client, Azure.Storage.DataMovement.Blobs.PageBlobStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromContainer(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + public System.Threading.Tasks.ValueTask FromContainerAsync(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class BlobStorageResourceContainerOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net8.0.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net8.0.cs index 49f09a298f0d1..5a91558bcd5fd 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net8.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.net8.0.cs @@ -26,22 +26,18 @@ public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.S { public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { } public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public BlobsStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public BlobsStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } - public Azure.Storage.DataMovement.StorageResource FromBlob(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null) { throw null; } + public System.Threading.Tasks.ValueTask FromBlobAsync(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.BlobContainerClient client, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.AppendBlobClient client, Azure.Storage.DataMovement.Blobs.AppendBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.BlockBlobClient client, Azure.Storage.DataMovement.Blobs.BlockBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.PageBlobClient client, Azure.Storage.DataMovement.Blobs.PageBlobStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromContainer(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + public System.Threading.Tasks.ValueTask FromContainerAsync(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class BlobStorageResourceContainerOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.netstandard2.0.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.netstandard2.0.cs index 49f09a298f0d1..5a91558bcd5fd 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.netstandard2.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/api/Azure.Storage.DataMovement.Blobs.netstandard2.0.cs @@ -26,22 +26,18 @@ public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.S { public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { } public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public BlobsStorageResourceProvider(Azure.Storage.DataMovement.Blobs.BlobsStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public BlobsStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public BlobsStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } - public Azure.Storage.DataMovement.StorageResource FromBlob(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null) { throw null; } + public System.Threading.Tasks.ValueTask FromBlobAsync(System.Uri blobUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.BlobContainerClient client, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.AppendBlobClient client, Azure.Storage.DataMovement.Blobs.AppendBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.BlockBlobClient client, Azure.Storage.DataMovement.Blobs.BlockBlobStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Blobs.Specialized.PageBlobClient client, Azure.Storage.DataMovement.Blobs.PageBlobStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromContainer(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + public System.Threading.Tasks.ValueTask FromContainerAsync(System.Uri containerUri, Azure.Storage.DataMovement.Blobs.BlobStorageResourceContainerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class BlobStorageResourceContainerOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/CopyDirectory.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/CopyDirectory.cs index 0d232cc910074..74d9f10bfb7e6 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/CopyDirectory.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/CopyDirectory.cs @@ -44,8 +44,8 @@ public override void Run(CancellationToken cancellationToken) public override async Task RunAsync(CancellationToken cancellationToken) { - StorageResource source = BlobResourceProvider.FromContainer(_sourceContainer.Uri); - StorageResource destination = BlobResourceProvider.FromContainer(_destinationContainer.Uri); + StorageResource source = await BlobResourceProvider.FromContainerAsync(_sourceContainer.Uri); + StorageResource destination = await BlobResourceProvider.FromContainerAsync(_destinationContainer.Uri); await RunAndVerifyTransferAsync(source, destination, cancellationToken); } diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/DownloadDirectory.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/DownloadDirectory.cs index 89a77f1395338..97a048c1008db 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/DownloadDirectory.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/DownloadDirectory.cs @@ -45,7 +45,7 @@ public override void Run(CancellationToken cancellationToken) public override async Task RunAsync(CancellationToken cancellationToken) { - StorageResource source = BlobResourceProvider.FromContainer(_sourceContainer.Uri); + StorageResource source = await BlobResourceProvider.FromContainerAsync(_sourceContainer.Uri); StorageResource destination = LocalFilesStorageResourceProvider.FromDirectory(_destinationDirectory); await RunAndVerifyTransferAsync(source, destination, cancellationToken); diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/UploadDirectory.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/UploadDirectory.cs index 22bc3f6b88f67..715e47cfcc8f3 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/UploadDirectory.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/perf/Azure.Storage.DataMovement.Blobs.Perf/Scenarios/UploadDirectory.cs @@ -46,7 +46,7 @@ public override void Run(CancellationToken cancellationToken) public override async Task RunAsync(CancellationToken cancellationToken) { StorageResource source = LocalFilesStorageResourceProvider.FromDirectory(_sourceDirectory); - StorageResource destination = BlobResourceProvider.FromContainer(_destinationContainer.Uri); + StorageResource destination = await BlobResourceProvider.FromContainerAsync(_destinationContainer.Uri); await RunAndVerifyTransferAsync(source, destination, cancellationToken); } diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/samples/Sample01b_HelloWorldAsync.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/samples/Sample01b_HelloWorldAsync.cs index e0ccab26acb95..88fae2d485544 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/samples/Sample01b_HelloWorldAsync.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/samples/Sample01b_HelloWorldAsync.cs @@ -12,6 +12,7 @@ using Azure.Storage.Blobs.Models; using Azure.Storage.Blobs; using System.Collections.Generic; +using System.Threading; namespace Azure.Storage.DataMovement.Blobs.Samples { @@ -52,17 +53,17 @@ public async Task ResourceConstructionDemonstration() // Construct simple blob resources for data movement #region Snippet:ResourceConstruction_Blobs - StorageResource container = blobs.FromContainer( + StorageResource container = await blobs.FromContainerAsync( new Uri("https://myaccount.blob.core.windows.net/container")); // Block blobs are the default if no options are specified - StorageResource blockBlob = blobs.FromBlob( + StorageResource blockBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-block"), new BlockBlobStorageResourceOptions()); - StorageResource pageBlob = blobs.FromBlob( + StorageResource pageBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-page"), new PageBlobStorageResourceOptions()); - StorageResource appendBlob = blobs.FromBlob( + StorageResource appendBlob = await blobs.FromBlobAsync( new Uri("https://myaccount.blob.core.windows.net/container/sample-blob-append"), new AppendBlobStorageResourceOptions()); #endregion @@ -70,7 +71,7 @@ public async Task ResourceConstructionDemonstration() { StorageSharedKeyCredential sharedKeyCredential = new(StorageAccountName, StorageAccountKey); // Get blobs provider with credential - AzureSasCredential GenerateSas(Uri uri, bool readOnly) + ValueTask GenerateSas(Uri uri, CancellationToken cancellationToken) { // Construct your SAS according to your needs BlobUriBuilder blobUri = new(uri); @@ -79,7 +80,7 @@ AzureSasCredential GenerateSas(Uri uri, bool readOnly) BlobContainerName = blobUri.BlobContainerName, BlobName = blobUri.BlobName, }; - return new AzureSasCredential(sas.ToSasQueryParameters(sharedKeyCredential).ToString()); + return new ValueTask(new AzureSasCredential(sas.ToSasQueryParameters(sharedKeyCredential).ToString())); } BlobsStorageResourceProvider blobs = new(GenerateSas); } @@ -200,7 +201,7 @@ public async Task UploadSingle_ConnectionStringAsync() #region Snippet:SimpleBlobUpload TransferOperation transferOperation = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalPath), - destinationResource: blobs.FromBlob(destinationBlobUri)); + destinationResource: await blobs.FromBlobAsync(destinationBlobUri)); await transferOperation.WaitForCompletionAsync(); #endregion #endregion @@ -273,7 +274,7 @@ public async Task DownloadSingle_SharedKeyAuthAsync() // Simple Download Single Blob Job #region Snippet:SimpleBlockBlobDownload TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromBlob(sourceBlobUri), + sourceResource: await blobs.FromBlobAsync(sourceBlobUri), destinationResource: LocalFilesStorageResourceProvider.FromFile(downloadPath)); await transferOperation.WaitForCompletionAsync(); #endregion @@ -369,7 +370,7 @@ public async Task UploadDirectory_SasAsync() #region Snippet:SimpleDirectoryUpload TransferOperation transferOperation = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromDirectory(sourcePath), - destinationResource: blobs.FromContainer( + destinationResource: await blobs.FromContainerAsync( blobContainerUri, new BlobStorageResourceContainerOptions() { @@ -652,7 +653,7 @@ await transferManager.StartTransferAsync( BlobsStorageResourceProvider blobs = new(tokenCredential); #region Snippet:SimpleDirectoryDownload_Blob TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromContainer( + sourceResource: await blobs.FromContainerAsync( blobContainerUri, new BlobStorageResourceContainerOptions() { @@ -721,8 +722,8 @@ public async Task CopySingle_ConnectionStringAsync() // Create simple transfer single blob upload job #region Snippet:s2sCopyBlob TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromBlob(sourceBlobUri), - destinationResource: blobs.FromBlob(destinationBlobUri, new AppendBlobStorageResourceOptions())); + sourceResource: await blobs.FromBlobAsync(sourceBlobUri), + destinationResource: await blobs.FromBlobAsync(destinationBlobUri, new AppendBlobStorageResourceOptions())); await transferOperation.WaitForCompletionAsync(); #endregion @@ -815,13 +816,13 @@ public async Task CopyDirectory() BlobContainerClient destinationContainer = container; #region Snippet:s2sCopyBlobContainer TransferOperation transferOperation = await transferManager.StartTransferAsync( - sourceResource: blobs.FromContainer( + sourceResource: await blobs.FromContainerAsync( sourceContainerUri, new BlobStorageResourceContainerOptions() { BlobPrefix = sourceDirectoryName }), - destinationResource: blobs.FromContainer( + destinationResource: await blobs.FromContainerAsync( destinationContainerUri, new BlobStorageResourceContainerOptions() { diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/src/BlobsStorageResourceProvider.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/src/BlobsStorageResourceProvider.cs index fce92430a44df..6cf6339847be5 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/src/BlobsStorageResourceProvider.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/src/BlobsStorageResourceProvider.cs @@ -17,39 +17,6 @@ namespace Azure.Storage.DataMovement.Blobs /// public class BlobsStorageResourceProvider : StorageResourceProvider { - /// - /// Delegate for fetching a shared key credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate StorageSharedKeyCredential GetStorageSharedKeyCredential(Uri uri, bool readOnly); - - /// - /// Delegate for fetching a token credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate TokenCredential GetTokenCredential(Uri uri, bool readOnly); - - /// - /// Delegate for fetching a SAS credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate AzureSasCredential GetAzureSasCredential(Uri uri, bool readOnly); - private enum ResourceType { Unknown = 0, @@ -71,9 +38,9 @@ private enum CredentialType protected override string ProviderId => "blob"; private readonly CredentialType _credentialType; - private readonly GetStorageSharedKeyCredential _getStorageSharedKeyCredential; - private readonly GetTokenCredential _getTokenCredential; - private readonly GetAzureSasCredential _getAzureSasCredential; + private readonly TokenCredential _tokenCredential; + private readonly Func> _getStorageSharedKeyCredential; + private readonly Func> _getAzureSasCredential; /// /// @@ -94,7 +61,7 @@ private enum CredentialType public BlobsStorageResourceProvider(StorageSharedKeyCredential credential) { _credentialType = CredentialType.SharedKey; - _getStorageSharedKeyCredential = (_, _) => credential; + _getStorageSharedKeyCredential = (_, _) => new ValueTask(credential); } /// @@ -116,7 +83,7 @@ public BlobsStorageResourceProvider(StorageSharedKeyCredential credential) public BlobsStorageResourceProvider(TokenCredential credential) { _credentialType = CredentialType.Token; - _getTokenCredential = (_, _) => credential; + _tokenCredential = credential; } /// @@ -140,7 +107,7 @@ public BlobsStorageResourceProvider(TokenCredential credential) public BlobsStorageResourceProvider(AzureSasCredential credential) { _credentialType = CredentialType.Sas; - _getAzureSasCredential = (_, _) => credential; + _getAzureSasCredential = (_, _) => new ValueTask(credential); } /// @@ -149,18 +116,18 @@ public BlobsStorageResourceProvider(AzureSasCredential credential) /// Storage . /// /// - /// This instance will use the given to fetch a credential + /// This instance will use the given callback to fetch a credential /// when constructing the underlying Azure.Storage.Blobs client, e.g. /// . - /// The delegate will only be used when the provider needs to construct a client in the first place. It will + /// The callback will only be used when the provider needs to construct a client in the first place. It will /// not be used when creating a from a pre-existing client, e.g. /// . /// /// /// - /// Delegate for acquiring a credential. + /// Callback for acquiring a credential for the given Uri. /// - public BlobsStorageResourceProvider(GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) + public BlobsStorageResourceProvider(Func> getStorageSharedKeyCredentialAsync) { _credentialType = CredentialType.SharedKey; _getStorageSharedKeyCredential = getStorageSharedKeyCredentialAsync; @@ -168,34 +135,11 @@ public BlobsStorageResourceProvider(GetStorageSharedKeyCredential getStorageShar /// /// - /// Constructs this provider to use the given delegate for acquiring a credential when making a new Blob - /// Storage . - /// - /// - /// This instance will use the given to fetch a credential - /// when constructing the underlying Azure.Storage.Blobs client, e.g. - /// . - /// The delegate will only be used when the provider needs to construct a client in the first place. It will - /// not be used when creating a from a pre-existing client, e.g. - /// . - /// - /// - /// - /// Delegate for acquiring a credential. - /// - public BlobsStorageResourceProvider(GetTokenCredential getTokenCredentialAsync) - { - _credentialType = CredentialType.SharedKey; - _getTokenCredential = getTokenCredentialAsync; - } - - /// - /// - /// Constructs this provider to use the given delegate for acquiring a credential when making a new Blob + /// Constructs this provider to use the given callback for acquiring a credential when making a new Blob /// Storage . /// /// - /// This instance will use the given to fetch a credential + /// This instance will use the given callback to fetch a credential /// when constructing the underlying Azure.Storage.Blobs client, e.g. /// . /// The delegate will only be used when the provider needs to construct a client in the first place. It will @@ -206,28 +150,29 @@ public BlobsStorageResourceProvider(GetTokenCredential getTokenCredentialAsync) /// /// /// - /// Delegate for acquiring a credential. + /// Callback for acquiring a credential for the given Uri and required set of permissions. /// - public BlobsStorageResourceProvider(GetAzureSasCredential getAzureSasCredentialAsync) + public BlobsStorageResourceProvider(Func> getAzureSasCredentialAsync) { - _credentialType = CredentialType.SharedKey; + _credentialType = CredentialType.Sas; _getAzureSasCredential = getAzureSasCredentialAsync; } #region Abstract Class Implementation /// - protected override Task FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: true, cancellationToken)); + protected override async ValueTask FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) + => await FromTransferPropertiesAsync(properties, getSource: true, cancellationToken).ConfigureAwait(false); /// - protected override Task FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: false, cancellationToken)); + protected override async ValueTask FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) + => await FromTransferPropertiesAsync(properties, getSource: false, cancellationToken).ConfigureAwait(false); - private StorageResource FromTransferProperties( + private async ValueTask FromTransferPropertiesAsync( TransferProperties properties, bool getSource, CancellationToken cancellationToken) { + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); StorageResourceCheckpointDetails checkpointDetails = properties.GetCheckpointDetails(getSource); ResourceType type = GetType(checkpointDetails, properties.IsContainer); @@ -253,19 +198,19 @@ private StorageResource FromTransferProperties( properties, checkpointDetails as BlobDestinationCheckpointDetails, getSource, - _getStorageSharedKeyCredential(uri, getSource), + await _getStorageSharedKeyCredential(uri, cancellationToken).ConfigureAwait(false), cancellationToken), CredentialType.Token => rehydrator.Rehydrate( properties, checkpointDetails as BlobDestinationCheckpointDetails, getSource, - _getTokenCredential(uri, getSource), + _tokenCredential, cancellationToken), CredentialType.Sas => rehydrator.Rehydrate( properties, checkpointDetails as BlobDestinationCheckpointDetails, getSource, - _getAzureSasCredential(uri, getSource), + await _getAzureSasCredential(uri, cancellationToken).ConfigureAwait(false), cancellationToken), _ => throw BadCredentialTypeException(_credentialType), }; @@ -300,17 +245,25 @@ internal async Task FromDestinationInternalHookAsync( /// /// Options for creating the storage resource. /// + /// + /// Optional to propagate + /// notifications that the operation should be cancelled. + /// /// /// The configured storage resource. /// - public StorageResource FromContainer(Uri containerUri, BlobStorageResourceContainerOptions options = default) + public async ValueTask FromContainerAsync( + Uri containerUri, + BlobStorageResourceContainerOptions options = default, + CancellationToken cancellationToken = default) { + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); BlobContainerClient client = _credentialType switch { CredentialType.None => new BlobContainerClient(containerUri), - CredentialType.SharedKey => new BlobContainerClient(containerUri, _getStorageSharedKeyCredential(containerUri, false)), - CredentialType.Token => new BlobContainerClient(containerUri, _getTokenCredential(containerUri, false)), - CredentialType.Sas => new BlobContainerClient(containerUri, _getAzureSasCredential(containerUri, false)), + CredentialType.SharedKey => new BlobContainerClient(containerUri, await _getStorageSharedKeyCredential(containerUri, cancellationToken).ConfigureAwait(false)), + CredentialType.Token => new BlobContainerClient(containerUri, _tokenCredential), + CredentialType.Sas => new BlobContainerClient(containerUri, await _getAzureSasCredential(containerUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new BlobStorageResourceContainer(client, options); @@ -336,19 +289,27 @@ public StorageResource FromContainer(Uri containerUri, BlobStorageResourceContai /// blob type of the destination. If only the base options type is /// provided, block blob will be the default used. /// + /// + /// Optional to propagate + /// notifications that the operation should be cancelled. + /// /// /// The configured storage resource. /// - public StorageResource FromBlob(Uri blobUri, BlobStorageResourceOptions options = default) + public async ValueTask FromBlobAsync( + Uri blobUri, + BlobStorageResourceOptions options = default, + CancellationToken cancellationToken = default) { + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); if (options is BlockBlobStorageResourceOptions) { BlockBlobClient blockClient = _credentialType switch { CredentialType.None => new BlockBlobClient(blobUri), - CredentialType.SharedKey => new BlockBlobClient(blobUri, _getStorageSharedKeyCredential(blobUri, false)), - CredentialType.Token => new BlockBlobClient(blobUri, _getTokenCredential(blobUri, false)), - CredentialType.Sas => new BlockBlobClient(blobUri, _getAzureSasCredential(blobUri, false)), + CredentialType.SharedKey => new BlockBlobClient(blobUri, await _getStorageSharedKeyCredential(blobUri, cancellationToken).ConfigureAwait(false)), + CredentialType.Token => new BlockBlobClient(blobUri, _tokenCredential), + CredentialType.Sas => new BlockBlobClient(blobUri, await _getAzureSasCredential(blobUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new BlockBlobStorageResource(blockClient, options as BlockBlobStorageResourceOptions); @@ -358,9 +319,9 @@ public StorageResource FromBlob(Uri blobUri, BlobStorageResourceOptions options PageBlobClient pageClient = _credentialType switch { CredentialType.None => new PageBlobClient(blobUri), - CredentialType.SharedKey => new PageBlobClient(blobUri, _getStorageSharedKeyCredential(blobUri, false)), - CredentialType.Token => new PageBlobClient(blobUri, _getTokenCredential(blobUri, false)), - CredentialType.Sas => new PageBlobClient(blobUri, _getAzureSasCredential(blobUri, false)), + CredentialType.SharedKey => new PageBlobClient(blobUri, await _getStorageSharedKeyCredential(blobUri, cancellationToken).ConfigureAwait(false)), + CredentialType.Token => new PageBlobClient(blobUri, _tokenCredential), + CredentialType.Sas => new PageBlobClient(blobUri, await _getAzureSasCredential(blobUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new PageBlobStorageResource(pageClient, options as PageBlobStorageResourceOptions); @@ -370,9 +331,9 @@ public StorageResource FromBlob(Uri blobUri, BlobStorageResourceOptions options AppendBlobClient appendClient = _credentialType switch { CredentialType.None => new AppendBlobClient(blobUri), - CredentialType.SharedKey => new AppendBlobClient(blobUri, _getStorageSharedKeyCredential(blobUri, false)), - CredentialType.Token => new AppendBlobClient(blobUri, _getTokenCredential(blobUri, false)), - CredentialType.Sas => new AppendBlobClient(blobUri, _getAzureSasCredential(blobUri, false)), + CredentialType.SharedKey => new AppendBlobClient(blobUri, await _getStorageSharedKeyCredential(blobUri, cancellationToken).ConfigureAwait(false)), + CredentialType.Token => new AppendBlobClient(blobUri, _tokenCredential), + CredentialType.Sas => new AppendBlobClient(blobUri, await _getAzureSasCredential(blobUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new AppendBlobStorageResource(appendClient, options as AppendBlobStorageResourceOptions); @@ -380,9 +341,9 @@ public StorageResource FromBlob(Uri blobUri, BlobStorageResourceOptions options BlockBlobClient client = _credentialType switch { CredentialType.None => new BlockBlobClient(blobUri), - CredentialType.SharedKey => new BlockBlobClient(blobUri, _getStorageSharedKeyCredential(blobUri, false)), - CredentialType.Token => new BlockBlobClient(blobUri, _getTokenCredential(blobUri, false)), - CredentialType.Sas => new BlockBlobClient(blobUri, _getAzureSasCredential(blobUri, false)), + CredentialType.SharedKey => new BlockBlobClient(blobUri, await _getStorageSharedKeyCredential(blobUri, cancellationToken).ConfigureAwait(false)), + CredentialType.Token => new BlockBlobClient(blobUri, _tokenCredential), + CredentialType.Sas => new BlockBlobClient(blobUri, await _getAzureSasCredential(blobUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new BlockBlobStorageResource(client, options as BlockBlobStorageResourceOptions); diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/BlobsStorageResourceProviderTests.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/BlobsStorageResourceProviderTests.cs index 61b3a467ebd46..1471a649c716a 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/BlobsStorageResourceProviderTests.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/BlobsStorageResourceProviderTests.cs @@ -12,6 +12,8 @@ using DMBlobs::Azure.Storage.DataMovement.Blobs; using Moq; using NUnit.Framework; +using System.Threading.Tasks; +using System.Threading; namespace Azure.Storage.DataMovement.Blobs.Tests { @@ -98,7 +100,7 @@ private void AssertBlobStorageResourceType( [Test] [Combinatorial] - public void FromContainer( + public async Task FromContainer( [Values(true, false)] bool withPrefix, [Values(CredType.SharedKey, CredType.Token, CredType.Sas)] CredType credType) { @@ -114,7 +116,7 @@ public void FromContainer( CredType.Sas => new(mockCreds.Sas.Object), _ => throw new ArgumentException("Bad cred type"), }; - BlobStorageResourceContainer resource = provider.FromContainer(uri) as BlobStorageResourceContainer; + BlobStorageResourceContainer resource = await provider.FromContainerAsync(uri) as BlobStorageResourceContainer; Assert.IsNotNull(resource); Assert.AreEqual(uri, resource.Uri); @@ -124,7 +126,7 @@ public void FromContainer( [Test] [Combinatorial] - public void FromBlob( + public async Task FromBlob( [Values(BlobType.Unspecified, BlobType.Block, BlobType.Page, BlobType.Append)] BlobType blobType, [Values(CredType.SharedKey, CredType.Token, CredType.Sas)] CredType credType) { @@ -143,10 +145,10 @@ public void FromBlob( StorageResource resource = blobType switch { - BlobType.Unspecified => provider.FromBlob(uri), - BlobType.Block => provider.FromBlob(uri, new BlockBlobStorageResourceOptions()), - BlobType.Page => provider.FromBlob(uri, new PageBlobStorageResourceOptions()), - BlobType.Append => provider.FromBlob(uri, new AppendBlobStorageResourceOptions()), + BlobType.Unspecified => await provider.FromBlobAsync(uri), + BlobType.Block => await provider.FromBlobAsync(uri, new BlockBlobStorageResourceOptions()), + BlobType.Page => await provider.FromBlobAsync(uri, new PageBlobStorageResourceOptions()), + BlobType.Append => await provider.FromBlobAsync(uri, new AppendBlobStorageResourceOptions()), _ => throw new ArgumentException("Bad blob type") }; @@ -156,5 +158,38 @@ public void FromBlob( Assert.AreEqual(uri, underlyingClient.Uri); AssertCredPresent(underlyingClient.ClientConfiguration, credType); } + + [Test] + public async Task CredentialCallback( + [Values(CredType.SharedKey, CredType.Sas)] CredType credType) + { + const string containerName = "mycontainer"; + const string blobName = "my/blob.txt"; + Uri uri = new Uri($"https://myaccount.blob.core.windows.net/{containerName}/{blobName}"); + (Mock SharedKey, Mock Token, Mock Sas) mockCreds = GetMockCreds(); + + ValueTask GetSharedKeyCredential(Uri uri, CancellationToken cancellationToken) + { + return new ValueTask(mockCreds.SharedKey.Object); + } + ValueTask GetSasCredential(Uri _, CancellationToken cancellationToken) + { + return new ValueTask(mockCreds.Sas.Object); + } + + BlobsStorageResourceProvider provider = credType switch + { + CredType.SharedKey => new(GetSharedKeyCredential), + CredType.Sas => new(GetSasCredential), + _ => throw new ArgumentException("Bad cred type"), + }; + StorageResource resource = await provider.FromBlobAsync(uri); + + Assert.IsNotNull(resource); + AssertBlobStorageResourceType(resource, BlobType.Block, out BlobBaseClient underlyingClient); + Assert.AreEqual(uri, resource.Uri); + Assert.AreEqual(uri, underlyingClient.Uri); + AssertCredPresent(underlyingClient.ClientConfiguration, credType); + } } } diff --git a/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/ProgressHandlerTests.cs b/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/ProgressHandlerTests.cs index e5c84e63f888f..8ad6d12b2c54f 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/ProgressHandlerTests.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Blobs/tests/ProgressHandlerTests.cs @@ -337,7 +337,7 @@ public async Task ProgressHandler_PauseResume(int delayInMs) // Act - Start transfer TransferOperation transfer = await transferManager.StartTransferAsync( - blobProvider.FromContainer(source.Container.Uri), + await blobProvider.FromContainerAsync(source.Container.Uri), LocalFilesStorageResourceProvider.FromDirectory(destination.DirectoryPath), transferOptions); diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/CHANGELOG.md b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/CHANGELOG.md index 97a432bb46406..812ec986f4dde 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/CHANGELOG.md +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/CHANGELOG.md @@ -22,8 +22,13 @@ - `ShareFileStorageResourceOptions.FileChangedOn` - Changed `ShareDirectoryClient.StartUploadDirectoryAsync` to `ShareDirectoryClient.UploadDirectoryAsync` and added a required `waitUntil` parameter. - Changed `ShareDirectoryClient.StartDownloadToDirectoryAsync` to `ShareDirectoryClient.DownloadToDirectoryAsync` and added a required `waitUntil` parameter. -- Removed `ShareFilesStorageResourceProvider()` empty default constructor -- Changed `ShareFilesStorageResourceProvider.FromClient` methods to `static` method +- Several refactors to `ShareFilesStorageResourceProvider`: + - Removed nested delegates `GetStorageSharedKeyCredential`, `GetTokenCredential`, and `GetAzureSasCredential`. + - Removed default constructor. + - Removed constructor overload for `GetTokenCredential` entirely. + - Changed constructor overloads for `GetStorageSharedKeyCredential` and `GetAzureSasCredential` to use `Func`. These callbacks are also now async, returning a `ValueTask`, and the `readOnly` parameter was removed. + - Changed `FromFile` and `FromDirectory` to async, returning a `ValueTask`, and renamed to `FromFileAsync` and `FromDirectoryAsync` respectively. + - Changed `FromClient` methods to `static` methods. ### Bugs Fixed diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/README.md b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/README.md index 912c816e9d5b5..156d9c120ae66 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/README.md +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/README.md @@ -91,11 +91,11 @@ ShareFilesStorageResourceProvider shares = new(tokenCredential); To create a share `StorageResource`, use the methods `FromFile` or `FromDirectory`. ```C# Snippet:ResourceConstruction_Shares -StorageResource directory = shares.FromDirectory( +StorageResource directory = await shares.FromDirectoryAsync( new Uri("http://myaccount.files.core.windows.net/share/path/to/directory")); -StorageResource rootDirectory = shares.FromDirectory( +StorageResource rootDirectory = await shares.FromDirectoryAsync( new Uri("http://myaccount.files.core.windows.net/share")); -StorageResource file = shares.FromFile( +StorageResource file = await shares.FromFileAsync( new Uri("http://myaccount.files.core.windows.net/share/path/to/file.txt")); ``` @@ -115,7 +115,7 @@ Upload a file. ```C# Snippet:SimplefileUpload_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalFile), - destinationResource: shares.FromFile(destinationFileUri)); + destinationResource: await shares.FromFileAsync(destinationFileUri)); await fileTransfer.WaitForCompletionAsync(); ``` @@ -124,7 +124,7 @@ Upload a directory. ```C# Snippet:SimpleDirectoryUpload_Shares TransferOperation folderTransfer = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromDirectory(sourceLocalDirectory), - destinationResource: shares.FromDirectory(destinationFolderUri)); + destinationResource: await shares.FromDirectoryAsync(destinationFolderUri)); await folderTransfer.WaitForCompletionAsync(); ``` @@ -136,7 +136,7 @@ Download a file. ```C# Snippet:SimpleFileDownload_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromFile(sourceFileUri), + sourceResource: await shares.FromFileAsync(sourceFileUri), destinationResource: LocalFilesStorageResourceProvider.FromFile(destinationLocalFile)); await fileTransfer.WaitForCompletionAsync(); ``` @@ -145,7 +145,7 @@ Download a Directory. ```C# Snippet:SimpleDirectoryDownload_Shares TransferOperation directoryTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromDirectory(sourceDirectoryUri), + sourceResource: await shares.FromDirectoryAsync(sourceDirectoryUri), destinationResource: LocalFilesStorageResourceProvider.FromDirectory(destinationLocalDirectory)); await directoryTransfer.WaitForCompletionAsync(); ``` @@ -158,8 +158,8 @@ Copy a single file. ```C# Snippet:s2sCopyFile_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromFile(sourceFileUri), - destinationResource: shares.FromFile(destinationFileUri)); + sourceResource: await shares.FromFileAsync(sourceFileUri), + destinationResource: await shares.FromFileAsync(destinationFileUri)); await fileTransfer.WaitForCompletionAsync(); ``` @@ -167,8 +167,8 @@ Copy a directory. ```C# Snippet:s2sCopyDirectory_Shares TransferOperation directoryTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromDirectory(sourceDirectoryUri), - destinationResource: shares.FromDirectory(destinationDirectoryUri)); + sourceResource: await shares.FromDirectoryAsync(sourceDirectoryUri), + destinationResource: await shares.FromDirectoryAsync(destinationDirectoryUri)); await directoryTransfer.WaitForCompletionAsync(); ``` diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net6.0.cs b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net6.0.cs index d892a34956afa..66d13ae8b677b 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net6.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net6.0.cs @@ -10,20 +10,16 @@ public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovem { public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { } public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public ShareFilesStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public ShareFilesStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareDirectoryClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareFileClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromDirectory(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromFile(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + public System.Threading.Tasks.ValueTask FromDirectoryAsync(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Threading.Tasks.ValueTask FromFileAsync(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class ShareFileStorageResourceOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net8.0.cs b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net8.0.cs index d892a34956afa..66d13ae8b677b 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net8.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.net8.0.cs @@ -10,20 +10,16 @@ public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovem { public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { } public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public ShareFilesStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public ShareFilesStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareDirectoryClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareFileClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromDirectory(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromFile(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + public System.Threading.Tasks.ValueTask FromDirectoryAsync(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Threading.Tasks.ValueTask FromFileAsync(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class ShareFileStorageResourceOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.netstandard2.0.cs b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.netstandard2.0.cs index d892a34956afa..66d13ae8b677b 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.netstandard2.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/api/Azure.Storage.DataMovement.Files.Shares.netstandard2.0.cs @@ -10,20 +10,16 @@ public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovem { public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { } public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetAzureSasCredential getAzureSasCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) { } - public ShareFilesStorageResourceProvider(Azure.Storage.DataMovement.Files.Shares.ShareFilesStorageResourceProvider.GetTokenCredential getTokenCredentialAsync) { } public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { } + public ShareFilesStorageResourceProvider(System.Func> getAzureSasCredentialAsync) { } + public ShareFilesStorageResourceProvider(System.Func> getStorageSharedKeyCredentialAsync) { } protected override string ProviderId { get { throw null; } } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareDirectoryClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } public static Azure.Storage.DataMovement.StorageResource FromClient(Azure.Storage.Files.Shares.ShareFileClient client, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromDirectory(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - public Azure.Storage.DataMovement.StorageResource FromFile(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null) { throw null; } - protected override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } - public delegate Azure.AzureSasCredential GetAzureSasCredential(System.Uri uri, bool readOnly); - public delegate Azure.Storage.StorageSharedKeyCredential GetStorageSharedKeyCredential(System.Uri uri, bool readOnly); - public delegate Azure.Core.TokenCredential GetTokenCredential(System.Uri uri, bool readOnly); + protected override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + public System.Threading.Tasks.ValueTask FromDirectoryAsync(System.Uri directoryUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Threading.Tasks.ValueTask FromFileAsync(System.Uri fileUri, Azure.Storage.DataMovement.Files.Shares.ShareFileStorageResourceOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + protected override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public partial class ShareFileStorageResourceOptions { diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/samples/Sample01b_HelloWorldAsync.cs b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/samples/Sample01b_HelloWorldAsync.cs index 246298bfdf827..34b9a387e081c 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/samples/Sample01b_HelloWorldAsync.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/samples/Sample01b_HelloWorldAsync.cs @@ -9,6 +9,7 @@ using Azure.Identity; using Azure.Storage.Files.Shares; using Azure.Storage.Sas; +using System.Threading; namespace Azure.Storage.DataMovement.Files.Shares.Samples { @@ -44,11 +45,11 @@ public async Task ResourceConstructionDemonstration() // Construct simple share file resources for data movement #region Snippet:ResourceConstruction_Shares - StorageResource directory = shares.FromDirectory( + StorageResource directory = await shares.FromDirectoryAsync( new Uri("http://myaccount.files.core.windows.net/share/path/to/directory")); - StorageResource rootDirectory = shares.FromDirectory( + StorageResource rootDirectory = await shares.FromDirectoryAsync( new Uri("http://myaccount.files.core.windows.net/share")); - StorageResource file = shares.FromFile( + StorageResource file = await shares.FromFileAsync( new Uri("http://myaccount.files.core.windows.net/share/path/to/file.txt")); #endregion } @@ -63,7 +64,7 @@ public async Task ResourceConstructionDemonstration() { StorageSharedKeyCredential sharedKeyCredential = new(StorageAccountName, StorageAccountKey); // Get shares provider with credential - AzureSasCredential GenerateSas(Uri uri, bool readOnly) + ValueTask GenerateSas(Uri uri, CancellationToken cancellationToken) { // Quick sample demonstrating minimal steps // Construct your SAS according to your needs @@ -73,7 +74,7 @@ AzureSasCredential GenerateSas(Uri uri, bool readOnly) ShareName = pathUri.ShareName, FilePath = pathUri.DirectoryOrFilePath, }; - return new AzureSasCredential(sas.ToSasQueryParameters(sharedKeyCredential).ToString()); + return new ValueTask(new AzureSasCredential(sas.ToSasQueryParameters(sharedKeyCredential).ToString())); } ShareFilesStorageResourceProvider shares = new(GenerateSas); } @@ -109,7 +110,7 @@ public async Task Upload() #region Snippet:SimplefileUpload_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalFile), - destinationResource: shares.FromFile(destinationFileUri)); + destinationResource: await shares.FromFileAsync(destinationFileUri)); await fileTransfer.WaitForCompletionAsync(); #endregion @@ -117,7 +118,7 @@ public async Task Upload() #region Snippet:SimpleDirectoryUpload_Shares TransferOperation folderTransfer = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromDirectory(sourceLocalDirectory), - destinationResource: shares.FromDirectory(destinationFolderUri)); + destinationResource: await shares.FromDirectoryAsync(destinationFolderUri)); await folderTransfer.WaitForCompletionAsync(); #endregion } @@ -151,7 +152,7 @@ public async Task Download() // Create simple transfer single share file upload job #region Snippet:SimpleFileDownload_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromFile(sourceFileUri), + sourceResource: await shares.FromFileAsync(sourceFileUri), destinationResource: LocalFilesStorageResourceProvider.FromFile(destinationLocalFile)); await fileTransfer.WaitForCompletionAsync(); #endregion @@ -159,7 +160,7 @@ public async Task Download() // Create simple transfer single share directory upload job #region Snippet:SimpleDirectoryDownload_Shares TransferOperation directoryTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromDirectory(sourceDirectoryUri), + sourceResource: await shares.FromDirectoryAsync(sourceDirectoryUri), destinationResource: LocalFilesStorageResourceProvider.FromDirectory(destinationLocalDirectory)); await directoryTransfer.WaitForCompletionAsync(); #endregion @@ -193,16 +194,16 @@ public async Task Copy() // Create simple transfer single share file upload job #region Snippet:s2sCopyFile_Shares TransferOperation fileTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromFile(sourceFileUri), - destinationResource: shares.FromFile(destinationFileUri)); + sourceResource: await shares.FromFileAsync(sourceFileUri), + destinationResource: await shares.FromFileAsync(destinationFileUri)); await fileTransfer.WaitForCompletionAsync(); #endregion // Create simple transfer single share directory upload job #region Snippet:s2sCopyDirectory_Shares TransferOperation directoryTransfer = await transferManager.StartTransferAsync( - sourceResource: shares.FromDirectory(sourceDirectoryUri), - destinationResource: shares.FromDirectory(destinationDirectoryUri)); + sourceResource: await shares.FromDirectoryAsync(sourceDirectoryUri), + destinationResource: await shares.FromDirectoryAsync(destinationDirectoryUri)); await directoryTransfer.WaitForCompletionAsync(); #endregion } diff --git a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareFilesStorageResourceProvider.cs b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareFilesStorageResourceProvider.cs index 7f8527c4ca9c1..223c407fd6e7d 100644 --- a/sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareFilesStorageResourceProvider.cs +++ b/sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareFilesStorageResourceProvider.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using System; -using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -17,39 +16,6 @@ namespace Azure.Storage.DataMovement.Files.Shares /// public class ShareFilesStorageResourceProvider : StorageResourceProvider { - /// - /// Delegate for fetching a shared key credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate StorageSharedKeyCredential GetStorageSharedKeyCredential(Uri uri, bool readOnly); - - /// - /// Delegate for fetching a token credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate TokenCredential GetTokenCredential(Uri uri, bool readOnly); - - /// - /// Delegate for fetching a SAS credential for a given URI. - /// - /// - /// URI of resource to fetch credential for. - /// - /// - /// Whether the permission can be read-only. - /// - public delegate AzureSasCredential GetAzureSasCredential(Uri uri, bool readOnly); - private enum ResourceType { Unknown = 0, @@ -70,9 +36,9 @@ private enum CredentialType protected override string ProviderId => "share"; private readonly CredentialType _credentialType; - private readonly GetStorageSharedKeyCredential _getStorageSharedKeyCredential; - private readonly GetTokenCredential _getTokenCredential; - private readonly GetAzureSasCredential _getAzureSasCredential; + private readonly TokenCredential _tokenCredential; + private readonly Func> _getStorageSharedKeyCredential; + private readonly Func> _getAzureSasCredential; #region ctors /// @@ -94,7 +60,7 @@ private enum CredentialType public ShareFilesStorageResourceProvider(StorageSharedKeyCredential credential) { _credentialType = CredentialType.SharedKey; - _getStorageSharedKeyCredential = (_, _) => credential; + _getStorageSharedKeyCredential = (_, _) => new ValueTask(credential); } /// @@ -116,7 +82,7 @@ public ShareFilesStorageResourceProvider(StorageSharedKeyCredential credential) public ShareFilesStorageResourceProvider(TokenCredential credential) { _credentialType = CredentialType.Token; - _getTokenCredential = (_, _) => credential; + _tokenCredential = credential; } /// @@ -140,7 +106,7 @@ public ShareFilesStorageResourceProvider(TokenCredential credential) public ShareFilesStorageResourceProvider(AzureSasCredential credential) { _credentialType = CredentialType.Sas; - _getAzureSasCredential = (_, _) => credential; + _getAzureSasCredential = (_, _) => new ValueTask(credential); } /// @@ -149,7 +115,7 @@ public ShareFilesStorageResourceProvider(AzureSasCredential credential) /// see cref="StorageResource"/>. /// /// - /// This instance will use the given to fetch a credential + /// This instance will use the given callback to fetch a credential /// when constructing the underlying Azure.Storage.Files.Shares client, e.g. /// . /// The delegate will only be used when the provider needs to construct a client in the first place. It will @@ -158,9 +124,9 @@ public ShareFilesStorageResourceProvider(AzureSasCredential credential) /// /// /// - /// Delegate for acquiring a credential. + /// Callback for acquiring a credential for the given Uri. /// - public ShareFilesStorageResourceProvider(GetStorageSharedKeyCredential getStorageSharedKeyCredentialAsync) + public ShareFilesStorageResourceProvider(Func> getStorageSharedKeyCredentialAsync) { _credentialType = CredentialType.SharedKey; _getStorageSharedKeyCredential = getStorageSharedKeyCredentialAsync; @@ -172,30 +138,7 @@ public ShareFilesStorageResourceProvider(GetStorageSharedKeyCredential getStorag /// . /// /// - /// This instance will use the given to fetch a credential - /// when constructing the underlying Azure.Storage.Files.Shares client, e.g. - /// . - /// The delegate will only be used when the provider needs to construct a client in the first place. It will - /// not be used when creating a from a pre-existing client, e.g. - /// . - /// - /// - /// - /// Delegate for acquiring a credential. - /// - public ShareFilesStorageResourceProvider(GetTokenCredential getTokenCredentialAsync) - { - _credentialType = CredentialType.SharedKey; - _getTokenCredential = getTokenCredentialAsync; - } - - /// - /// - /// Constructs this provider to use the given delegate for acquiring a credential when making a new File Share - /// . - /// - /// - /// This instance will use the given to fetch a credential + /// This instance will use the given callback to fetch a credential /// when constructing the underlying Azure.Storage.Files.Shares client, e.g. /// . /// The delegate will only be used when the provider needs to construct a client in the first place. It will @@ -206,28 +149,27 @@ public ShareFilesStorageResourceProvider(GetTokenCredential getTokenCredentialAs /// /// /// - /// Delegate for acquiring a credential. + /// Callback for acquiring a credential for the given Uri. /// - public ShareFilesStorageResourceProvider(GetAzureSasCredential getAzureSasCredentialAsync) + public ShareFilesStorageResourceProvider(Func> getAzureSasCredentialAsync) { - _credentialType = CredentialType.SharedKey; + _credentialType = CredentialType.Sas; _getAzureSasCredential = getAzureSasCredentialAsync; } #endregion #region Abstract Class Implementation /// - protected override Task FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) + protected override async ValueTask FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) { // Source share file data currently empty, so no specific properties to grab - - return Task.FromResult(properties.IsContainer - ? FromDirectory(properties.SourceUri) - : FromFile(properties.SourceUri)); + return properties.IsContainer + ? await FromDirectoryAsync(properties.SourceUri).ConfigureAwait(false) + : await FromFileAsync(properties.SourceUri).ConfigureAwait(false); } /// - protected override Task FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) + protected override async ValueTask FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) { ShareFileDestinationCheckpointDetails checkpointDetails; using (MemoryStream stream = new(properties.DestinationCheckpointDetails)) @@ -261,9 +203,9 @@ protected override Task FromDestinationAsync(TransferProperties FileMetadata = checkpointDetails.FileMetadata, _isFileMetadataSet = checkpointDetails.IsFileMetadataSet, }; - return Task.FromResult(properties.IsContainer - ? FromDirectory(properties.DestinationUri, options) - : FromFile(properties.DestinationUri, options)); + return properties.IsContainer + ? await FromDirectoryAsync(properties.DestinationUri, options).ConfigureAwait(false) + : await FromFileAsync(properties.DestinationUri, options).ConfigureAwait(false); } /// @@ -295,20 +237,28 @@ internal async Task FromDestinationInternalHookAsync( /// /// Options for creating the storage resource. /// + /// + /// Optional to propagate + /// notifications that the operation should be cancelled. + /// /// /// The configured storage resource. /// - public StorageResource FromDirectory(Uri directoryUri, ShareFileStorageResourceOptions options = default) + public async ValueTask FromDirectoryAsync( + Uri directoryUri, + ShareFileStorageResourceOptions options = default, + CancellationToken cancellationToken = default) { + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); ShareDirectoryClient client = _credentialType switch { CredentialType.None => new ShareDirectoryClient(directoryUri), - CredentialType.SharedKey => new ShareDirectoryClient(directoryUri, _getStorageSharedKeyCredential(directoryUri, false)), + CredentialType.SharedKey => new ShareDirectoryClient(directoryUri, await _getStorageSharedKeyCredential(directoryUri, cancellationToken).ConfigureAwait(false)), CredentialType.Token => new ShareDirectoryClient( directoryUri, - _getTokenCredential(directoryUri, false), + _tokenCredential, new ShareClientOptions { ShareTokenIntent = ShareTokenIntent.Backup }), - CredentialType.Sas => new ShareDirectoryClient(directoryUri, _getAzureSasCredential(directoryUri, false)), + CredentialType.Sas => new ShareDirectoryClient(directoryUri, await _getAzureSasCredential(directoryUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new ShareDirectoryStorageResourceContainer(client, options); @@ -323,22 +273,28 @@ public StorageResource FromDirectory(Uri directoryUri, ShareFileStorageResourceO /// /// Options for creating the storage resource. /// + /// + /// Optional to propagate + /// notifications that the operation should be cancelled. + /// /// /// The configured storage resource. /// - public StorageResource FromFile( + public async ValueTask FromFileAsync( Uri fileUri, - ShareFileStorageResourceOptions options = default) + ShareFileStorageResourceOptions options = default, + CancellationToken cancellationToken = default) { + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); ShareFileClient client = _credentialType switch { CredentialType.None => new ShareFileClient(fileUri), - CredentialType.SharedKey => new ShareFileClient(fileUri, _getStorageSharedKeyCredential(fileUri, false)), + CredentialType.SharedKey => new ShareFileClient(fileUri, await _getStorageSharedKeyCredential(fileUri, cancellationToken).ConfigureAwait(false)), CredentialType.Token => new ShareFileClient( fileUri, - _getTokenCredential(fileUri, false), + _tokenCredential, new ShareClientOptions { ShareTokenIntent = ShareTokenIntent.Backup }), - CredentialType.Sas => new ShareFileClient(fileUri, _getAzureSasCredential(fileUri, false)), + CredentialType.Sas => new ShareFileClient(fileUri, await _getAzureSasCredential(fileUri, cancellationToken).ConfigureAwait(false)), _ => throw BadCredentialTypeException(_credentialType), }; return new ShareFileStorageResource(client, options); @@ -389,10 +345,6 @@ public static StorageResource FromClient( } #endregion - private static ArgumentException BadResourceTypeException(ResourceType resourceType) - => new ArgumentException( - $"No support for resource type {Enum.GetName(typeof(ResourceType), resourceType)}."); - private static ArgumentException BadCredentialTypeException(CredentialType credentialType) => new ArgumentException( $"No support for credential type {Enum.GetName(typeof(CredentialType), credentialType)}."); diff --git a/sdk/storage/Azure.Storage.DataMovement/README.md b/sdk/storage/Azure.Storage.DataMovement/README.md index 9260796673e9c..670c759ced4ed 100644 --- a/sdk/storage/Azure.Storage.DataMovement/README.md +++ b/sdk/storage/Azure.Storage.DataMovement/README.md @@ -97,7 +97,7 @@ BlobsStorageResourceProvider blobs = new(tokenCredential); // Create simple transfer single blob upload job TransferOperation transferOperation = await transferManager.StartTransferAsync( sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalPath), - destinationResource: blobs.FromBlob(destinationBlobUri)); + destinationResource: await blobs.FromBlobAsync(destinationBlobUri)); await transferOperation.WaitForCompletionAsync(); ``` diff --git a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net6.0.cs b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net6.0.cs index a5a7c2db64319..74cdbdb9f3104 100644 --- a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net6.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net6.0.cs @@ -4,10 +4,10 @@ public partial class LocalFilesStorageResourceProvider : Azure.Storage.DataMovem { internal LocalFilesStorageResourceProvider() { } protected internal override string ProviderId { get { throw null; } } - protected internal override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } public static Azure.Storage.DataMovement.StorageResourceContainer FromDirectory(string directoryPath) { throw null; } public static Azure.Storage.DataMovement.StorageResourceItem FromFile(string filePath) { throw null; } - protected internal override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public abstract partial class StorageResource { @@ -85,8 +85,8 @@ public abstract partial class StorageResourceProvider { protected StorageResourceProvider() { } protected internal abstract string ProviderId { get; } - protected internal abstract System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); - protected internal abstract System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); } public partial class StorageResourceReadStreamResult { diff --git a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net8.0.cs b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net8.0.cs index a5a7c2db64319..74cdbdb9f3104 100644 --- a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net8.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.net8.0.cs @@ -4,10 +4,10 @@ public partial class LocalFilesStorageResourceProvider : Azure.Storage.DataMovem { internal LocalFilesStorageResourceProvider() { } protected internal override string ProviderId { get { throw null; } } - protected internal override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } public static Azure.Storage.DataMovement.StorageResourceContainer FromDirectory(string directoryPath) { throw null; } public static Azure.Storage.DataMovement.StorageResourceItem FromFile(string filePath) { throw null; } - protected internal override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public abstract partial class StorageResource { @@ -85,8 +85,8 @@ public abstract partial class StorageResourceProvider { protected StorageResourceProvider() { } protected internal abstract string ProviderId { get; } - protected internal abstract System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); - protected internal abstract System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); } public partial class StorageResourceReadStreamResult { diff --git a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.netstandard2.0.cs b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.netstandard2.0.cs index a5a7c2db64319..74cdbdb9f3104 100644 --- a/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.netstandard2.0.cs +++ b/sdk/storage/Azure.Storage.DataMovement/api/Azure.Storage.DataMovement.netstandard2.0.cs @@ -4,10 +4,10 @@ public partial class LocalFilesStorageResourceProvider : Azure.Storage.DataMovem { internal LocalFilesStorageResourceProvider() { } protected internal override string ProviderId { get { throw null; } } - protected internal override System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } public static Azure.Storage.DataMovement.StorageResourceContainer FromDirectory(string directoryPath) { throw null; } public static Azure.Storage.DataMovement.StorageResourceItem FromFile(string filePath) { throw null; } - protected internal override System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } + protected internal override System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken) { throw null; } } public abstract partial class StorageResource { @@ -85,8 +85,8 @@ public abstract partial class StorageResourceProvider { protected StorageResourceProvider() { } protected internal abstract string ProviderId { get; } - protected internal abstract System.Threading.Tasks.Task FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); - protected internal abstract System.Threading.Tasks.Task FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromDestinationAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); + protected internal abstract System.Threading.Tasks.ValueTask FromSourceAsync(Azure.Storage.DataMovement.TransferProperties properties, System.Threading.CancellationToken cancellationToken); } public partial class StorageResourceReadStreamResult { diff --git a/sdk/storage/Azure.Storage.DataMovement/src/LocalFilesStorageResourceProvider.cs b/sdk/storage/Azure.Storage.DataMovement/src/LocalFilesStorageResourceProvider.cs index bd63f5fa88815..b51ae06847bc2 100644 --- a/sdk/storage/Azure.Storage.DataMovement/src/LocalFilesStorageResourceProvider.cs +++ b/sdk/storage/Azure.Storage.DataMovement/src/LocalFilesStorageResourceProvider.cs @@ -25,12 +25,12 @@ internal LocalFilesStorageResourceProvider() } /// - protected internal override Task FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: true)); + protected internal override ValueTask FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) + => new(FromTransferProperties(properties, getSource: true)); /// - protected internal override Task FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: false)); + protected internal override ValueTask FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) + => new(FromTransferProperties(properties, getSource: false)); private StorageResource FromTransferProperties(TransferProperties properties, bool getSource) { diff --git a/sdk/storage/Azure.Storage.DataMovement/src/StorageResourceProvider.cs b/sdk/storage/Azure.Storage.DataMovement/src/StorageResourceProvider.cs index 247232620e1bb..1db3e1d8bfc6e 100644 --- a/sdk/storage/Azure.Storage.DataMovement/src/StorageResourceProvider.cs +++ b/sdk/storage/Azure.Storage.DataMovement/src/StorageResourceProvider.cs @@ -19,11 +19,11 @@ public abstract class StorageResourceProvider /// /// Gets a source resource from the given transfer properties. /// - protected internal abstract Task FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken); + protected internal abstract ValueTask FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken); /// /// Gets a source resource from the given transfer properties. /// - protected internal abstract Task FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken); + protected internal abstract ValueTask FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken); } } diff --git a/sdk/storage/Azure.Storage.DataMovement/tests/MockStorageResourceProvider.cs b/sdk/storage/Azure.Storage.DataMovement/tests/MockStorageResourceProvider.cs index bc7de36d35a7d..6098939a5f53d 100644 --- a/sdk/storage/Azure.Storage.DataMovement/tests/MockStorageResourceProvider.cs +++ b/sdk/storage/Azure.Storage.DataMovement/tests/MockStorageResourceProvider.cs @@ -28,12 +28,12 @@ internal MockStorageResourceProvider( } /// - protected internal override Task FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: true)); + protected internal override ValueTask FromSourceAsync(TransferProperties properties, CancellationToken cancellationToken) + => new(FromTransferProperties(properties, getSource: true)); /// - protected internal override Task FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) - => Task.FromResult(FromTransferProperties(properties, getSource: false)); + protected internal override ValueTask FromDestinationAsync(TransferProperties properties, CancellationToken cancellationToken) + => new(FromTransferProperties(properties, getSource: false)); private StorageResource FromTransferProperties(TransferProperties properties, bool getSource) {