forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ShareDirectoryClient extensions (Azure#38993)
* ShareDirectoryClient extensions * removed mention of blobs * exportapi * fix tests
- Loading branch information
1 parent
2f11d17
commit 085f2de
Showing
5 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareDirectoryClientExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Storage.DataMovement; | ||
using Azure.Storage.DataMovement.Files.Shares; | ||
|
||
namespace Azure.Storage.Files.Shares | ||
{ | ||
/// <summary> | ||
/// Data movement extension methods for the <see cref="ShareDirectoryClient"/>. | ||
/// </summary> | ||
public static class ShareDirectoryClientExtensions | ||
{ | ||
private static Lazy<TransferManager> s_defaultTransferManager = new(() => new TransferManager(default)); | ||
private static Lazy<LocalFilesStorageResourceProvider> s_localFilesProvider = new(); | ||
private static Lazy<ShareFilesStorageResourceProvider> s_shareFilesProvider = new(); | ||
|
||
/// <summary> | ||
/// Uploads the entire contents of local directory to the share directory. | ||
/// </summary> | ||
/// <param name="client"> | ||
/// The <see cref="ShareDirectoryClient"/> used for service operations. | ||
/// </param> | ||
/// <param name="localDirectoryPath"> | ||
/// The full path to the local directory to be uploaded. | ||
/// </param> | ||
/// <param name="options"> | ||
/// Options which control the directory upload. | ||
/// </param> | ||
/// <returns> | ||
/// A <see cref="DataTransfer"/> instance which can be used track progress and wait for | ||
/// completion with <see cref="DataTransfer.WaitForCompletionAsync"/>. | ||
/// </returns> | ||
public static async Task<DataTransfer> StartUploadDirectoryAsync( | ||
this ShareDirectoryClient client, | ||
string localDirectoryPath, | ||
ShareDirectoryClientTransferOptions options = default) | ||
{ | ||
StorageResource localDirectory = s_localFilesProvider.Value.FromPath(localDirectoryPath); | ||
StorageResource shareDirectory = s_shareFilesProvider.Value.FromClient(client, options?.ShareDirectoryOptions); | ||
|
||
return await s_defaultTransferManager.Value.StartTransferAsync( | ||
localDirectory, shareDirectory, options?.TransferOptions).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Downloads the contents of a share directory. | ||
/// </summary> | ||
/// <param name="client">The <see cref="ShareDirectoryClient"/> used for service operations.</param> | ||
/// <param name="localDirectoryPath">The full path to the local directory where files will be dowloaded.</param> | ||
/// <param name="options">Options which control the container download.</param> | ||
/// <returns></returns> | ||
public static async Task<DataTransfer> StartDownloadToDirectoryAsync( | ||
this ShareDirectoryClient client, | ||
string localDirectoryPath, | ||
ShareDirectoryClientTransferOptions options = default) | ||
{ | ||
StorageResource localDirectory = s_localFilesProvider.Value.FromPath(localDirectoryPath); | ||
StorageResource shareDirectory = s_shareFilesProvider.Value.FromClient(client, options?.ShareDirectoryOptions); | ||
|
||
return await s_defaultTransferManager.Value.StartTransferAsync( | ||
shareDirectory, localDirectory, options?.TransferOptions).ConfigureAwait(false); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...torage/Azure.Storage.DataMovement.Files.Shares/src/ShareDirectoryClientTransferOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Storage.Files.Shares; | ||
|
||
namespace Azure.Storage.DataMovement.Files.Shares | ||
{ | ||
/// <summary> | ||
/// Options applying to data transfer uploads and downloads using the <see cref="ShareDirectoryClient"/> extension methods | ||
/// <see cref="ShareDirectoryClientExtensions.StartDownloadToDirectoryAsync(ShareDirectoryClient, string, ShareDirectoryClientTransferOptions)"/> and | ||
/// <see cref="ShareDirectoryClientExtensions.StartUploadDirectoryAsync(ShareDirectoryClient, string, ShareDirectoryClientTransferOptions)"/>. | ||
/// </summary> | ||
public class ShareDirectoryClientTransferOptions | ||
{ | ||
/// <summary> | ||
/// Options pertaining to the share file directory used in the data transfer. | ||
/// </summary> | ||
public ShareFileStorageResourceOptions ShareDirectoryOptions { get; set; } | ||
|
||
/// <summary> | ||
/// Options pertaining to the data tranfer. | ||
/// </summary> | ||
public DataTransferOptions TransferOptions { get; set; } | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...rage/Azure.Storage.DataMovement.Files.Shares/tests/ShareDirectoryClientExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Storage.Files.Shares; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Storage.DataMovement.Files.Shares.Tests | ||
{ | ||
[NonParallelizable] | ||
public class ShareDirectoryClientExtensionsTests | ||
{ | ||
private Mock<TransferManager> ExtensionMockTransferManager { get; set; } | ||
|
||
// temporarily stores the static value that was in the extensions class | ||
private Lazy<TransferManager> _backupTransferManagerValue; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
ExtensionMockTransferManager = new(); | ||
ExtensionMockTransferManager.Setup(tm => tm.StartTransferAsync( | ||
It.IsAny<StorageResource>(), | ||
It.IsAny<StorageResource>(), | ||
It.IsAny<DataTransferOptions>(), | ||
It.IsAny<CancellationToken>())); | ||
|
||
_backupTransferManagerValue = (Lazy<TransferManager>)typeof(ShareDirectoryClientExtensions) | ||
.GetField("s_defaultTransferManager", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); | ||
typeof(ShareDirectoryClientExtensions) | ||
.GetField("s_defaultTransferManager", BindingFlags.NonPublic | BindingFlags.Static) | ||
.SetValue(null, new Lazy<TransferManager>(() => ExtensionMockTransferManager.Object)); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
typeof(ShareDirectoryClientExtensions) | ||
.GetField("s_defaultTransferManager", BindingFlags.NonPublic | BindingFlags.Static) | ||
.SetValue(null, _backupTransferManagerValue); | ||
} | ||
|
||
[Test] | ||
public async Task StartUploadDirectory([Values(true, false)] bool useOptions) | ||
{ | ||
ShareFileStorageResourceOptions storageResourceOptions = new(); | ||
DataTransferOptions dataTransferOptions = new(); | ||
ShareDirectoryClientTransferOptions transferOptions = new() | ||
{ | ||
ShareDirectoryOptions = storageResourceOptions, | ||
TransferOptions = dataTransferOptions, | ||
}; | ||
string localPath = Path.GetTempPath(); | ||
Mock<ShareDirectoryClient> clientMock = new(); | ||
|
||
await clientMock.Object.StartUploadDirectoryAsync(localPath, useOptions ? transferOptions : null); | ||
|
||
ExtensionMockTransferManager.Verify(tm => tm.StartTransferAsync( | ||
It.IsAny<StorageResource>(), | ||
It.Is<StorageResource>(res => res is ShareDirectoryStorageResourceContainer && | ||
(res as ShareDirectoryStorageResourceContainer).ShareDirectoryClient == clientMock.Object && | ||
(res as ShareDirectoryStorageResourceContainer).ResourceOptions == (useOptions ? storageResourceOptions : null)), | ||
useOptions ? dataTransferOptions : null, | ||
default), Times.Once); | ||
ExtensionMockTransferManager.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Test] | ||
public async Task StartDownloadDirectory([Values(true, false)] bool useOptions) | ||
{ | ||
ShareFileStorageResourceOptions storageResourceOptions = new(); | ||
DataTransferOptions dataTransferOptions = new(); | ||
ShareDirectoryClientTransferOptions transferOptions = new() | ||
{ | ||
ShareDirectoryOptions = storageResourceOptions, | ||
TransferOptions = dataTransferOptions, | ||
}; | ||
string localPath = Path.GetTempPath(); | ||
Mock<ShareDirectoryClient> clientMock = new(); | ||
|
||
await clientMock.Object.StartDownloadToDirectoryAsync(localPath, useOptions ? transferOptions : null); | ||
|
||
ExtensionMockTransferManager.Verify(tm => tm.StartTransferAsync( | ||
It.Is<StorageResource>(res => res is ShareDirectoryStorageResourceContainer && | ||
(res as ShareDirectoryStorageResourceContainer).ShareDirectoryClient == clientMock.Object && | ||
(res as ShareDirectoryStorageResourceContainer).ResourceOptions == (useOptions ? storageResourceOptions : null)), | ||
It.IsAny<StorageResource>(), | ||
useOptions ? dataTransferOptions : null, | ||
default), Times.Once); | ||
ExtensionMockTransferManager.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |