Skip to content

Commit 3c13426

Browse files
authored
[Storage] [DataMovement] Getting Started Guide Updates / Advanced samples (Azure#48125)
* WIP * More updates to samples / getting started * Updated links, next steps, and adding creation mode samples * Fixed links * Fix links again * Fixed link part 4 * PR comments
1 parent ad40c90 commit 3c13426

File tree

10 files changed

+476
-175
lines changed

10 files changed

+476
-175
lines changed

sdk/storage/Azure.Storage.DataMovement.Blobs/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,30 @@ destinationResource: await blobs.FromContainerAsync(
235235
await transferOperation.WaitForCompletionAsync();
236236
```
237237

238+
### Resume using ShareFilesStorageResourceProvider
239+
240+
To resume a transfer with Blob(s), valid credentials must be provided. See the sample below.
241+
242+
```C# Snippet:TransferManagerResumeTransfers
243+
TokenCredential tokenCredential = new DefaultAzureCredential();
244+
BlobsStorageResourceProvider blobs = new(tokenCredential);
245+
TransferManager transferManager = new TransferManager(new TransferManagerOptions()
246+
{
247+
ProvidersForResuming = new List<StorageResourceProvider>() { blobs }
248+
});
249+
// Get resumable transfers from transfer manager
250+
await foreach (TransferProperties properties in transferManager.GetResumableTransfersAsync())
251+
{
252+
// Resume the transfer
253+
if (properties.SourceUri.AbsoluteUri == "https://storageaccount.blob.core.windows.net/containername/blobpath")
254+
{
255+
await transferManager.ResumeTransferAsync(properties.TransferId);
256+
}
257+
}
258+
```
259+
260+
For more information regarding pause, resume, and/or checkpointing, see [Pause and Resume Checkpointing](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.DataMovement/samples/PauseResumeCheckpointing.md).
261+
238262
### Extensions on `BlobContainerClient`
239263

240264
For applications with preexisting code using Azure.Storage.Blobs, this package provides extension methods for `BlobContainerClient` to get some of the benefits of the `TransferManager` with minimal extra code.

sdk/storage/Azure.Storage.DataMovement.Blobs/samples/Sample01b_HelloWorldAsync.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ public async Task UploadSingle_ConnectionStringAsync()
194194
// Get a reference to a destination blobs
195195
Uri destinationBlobUri = container.GetBlockBlobClient("sample-blob").Uri;
196196

197+
// Should be the same token credential as above, but for demonstration/sample purposes
198+
// we include how we would get the token credential.
197199
#region Snippet:SimpleBlobUpload_BasePackage
198-
BlobsStorageResourceProvider blobs = new(tokenCredential);
200+
TokenCredential defaultTokenCredential = new DefaultAzureCredential();
201+
BlobsStorageResourceProvider blobs = new BlobsStorageResourceProvider(defaultTokenCredential);
199202

200203
// Create simple transfer single blob upload job
201204
#region Snippet:SimpleBlobUpload
@@ -864,10 +867,10 @@ public async Task PauseAndResumeAsync_ManagerId()
864867

865868
// Create a token credential that can use our Azure Active
866869
// Directory application to authenticate with Azure Storage
867-
TokenCredential tokenCredential = new DefaultAzureCredential();
868870

869871
// Create transfer manager
870872
#region Snippet:SetupTransferManagerForResume
873+
TokenCredential tokenCredential = new DefaultAzureCredential();
871874
BlobsStorageResourceProvider blobs = new(tokenCredential);
872875
TransferManager transferManager = new(new TransferManagerOptions()
873876
{
@@ -890,8 +893,10 @@ public async Task PauseAndResumeAsync_ManagerId()
890893
await transferManager.PauseTransferAsync(transferId);
891894
#endregion
892895

896+
#region Snippet:ResumeAllTransfers
893897
// Resume all transfers
894898
List<TransferOperation> transfers = await transferManager.ResumeAllTransfersAsync();
899+
#endregion
895900

896901
// Resume a single transfer
897902
#region Snippet:DataMovement_ResumeSingle
@@ -951,7 +956,7 @@ public async Task PauseAndResumeAsync_DataTransferPause()
951956
#endregion
952957

953958
TransferOperation resumedTransfer = await transferManager.ResumeTransferAsync(
954-
transferId: transferOperation.Id);
959+
transferId: transferOperation.Id);
955960

956961
// Wait for download to finish
957962
await resumedTransfer.WaitForCompletionAsync();
@@ -1192,6 +1197,55 @@ async Task<TransferOperation> ListenToProgressAsync(TransferManager transferMana
11921197
}
11931198
}
11941199

1200+
public void CreateTransferOptionCreationMode()
1201+
{
1202+
#region Snippet:TransferOptionsOverwrite
1203+
TransferOptions optionsOverwriteIfExists = new TransferOptions()
1204+
{
1205+
CreationMode = StorageResourceCreationMode.OverwriteIfExists,
1206+
};
1207+
#endregion
1208+
#region Snippet:TransferOptionsSkipIfExists
1209+
TransferOptions optionsSkipIfExists = new TransferOptions()
1210+
{
1211+
CreationMode = StorageResourceCreationMode.SkipIfExists,
1212+
};
1213+
#endregion
1214+
}
1215+
1216+
public async Task ResumeTransfersStoredAsync()
1217+
{
1218+
#region Snippet:TransferManagerResumeTransfers
1219+
TokenCredential tokenCredential = new DefaultAzureCredential();
1220+
BlobsStorageResourceProvider blobs = new(tokenCredential);
1221+
TransferManager transferManager = new TransferManager(new TransferManagerOptions()
1222+
{
1223+
ProvidersForResuming = new List<StorageResourceProvider>() { blobs }
1224+
});
1225+
// Get resumable transfers from transfer manager
1226+
await foreach (TransferProperties properties in transferManager.GetResumableTransfersAsync())
1227+
{
1228+
// Resume the transfer
1229+
if (properties.SourceUri.AbsoluteUri == "https://storageaccount.blob.core.windows.net/containername/blobpath")
1230+
{
1231+
await transferManager.ResumeTransferAsync(properties.TransferId);
1232+
}
1233+
}
1234+
#endregion
1235+
}
1236+
1237+
#region Snippet:EnumerateTransfersStatus
1238+
public async Task CheckTransfersStatusAsync(TransferManager transferManager)
1239+
{
1240+
string logFile = CreateTempPath();
1241+
await foreach (TransferOperation transfer in transferManager.GetTransfersAsync())
1242+
{
1243+
using StreamWriter logStream = File.AppendText(logFile);
1244+
logStream.WriteLine(Enum.GetName(typeof(TransferStatus), transfer.Status));
1245+
}
1246+
}
1247+
#endregion
1248+
11951249
public async Task<string> CreateBlobContainerTestDirectory(BlobContainerClient client, int depth = 0, string basePath = default)
11961250
{
11971251
basePath = basePath ?? Path.GetTempFileName();

sdk/storage/Azure.Storage.DataMovement.Files.Shares/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ az storage account create --name MyStorageAccount --resource-group MyResourceGro
4545
```
4646

4747
### Authenticate the client
48-
The Azure.Storage.DataMovement.Files.Shares library uses clients from the Azure.Storage.Files.Shares package to communicate with the Azure File Storage service. For more information see the Azure.Storage.Files.Shares [authentication documentation](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.Files.Shares#authenticate-the-client).
48+
The Azure.Storage.DataMovement.Files.Shares library uses clients from the Azure.Storage.Files.Shares package to communicate with the Azure File Storage service. For more information see the Azure.Storage.Files.Shares [authentication documentation](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.Files.Shares/README.md#authenticate-the-client).
4949

5050
### Permissions
5151

@@ -113,6 +113,9 @@ An upload takes place between a local file `StorageResource` as source and file
113113
Upload a file.
114114

115115
```C# Snippet:SimplefileUpload_Shares
116+
TokenCredential tokenCredential = new DefaultAzureCredential();
117+
ShareFilesStorageResourceProvider shares = new(tokenCredential);
118+
TransferManager transferManager = new TransferManager(new TransferManagerOptions());
116119
TransferOperation fileTransfer = await transferManager.StartTransferAsync(
117120
sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalFile),
118121
destinationResource: await shares.FromFileAsync(destinationFileUri));
@@ -172,6 +175,30 @@ TransferOperation directoryTransfer = await transferManager.StartTransferAsync(
172175
await directoryTransfer.WaitForCompletionAsync();
173176
```
174177

178+
### Resume using ShareFilesStorageResourceProvider
179+
180+
To resume a transfer with Share File(s), valid credentials must be provided. See the sample below.
181+
182+
```C# Snippet:TransferManagerResumeTransfers_Shares
183+
TokenCredential tokenCredential = new DefaultAzureCredential();
184+
ShareFilesStorageResourceProvider shares = new(tokenCredential);
185+
TransferManager transferManager = new TransferManager(new TransferManagerOptions()
186+
{
187+
ProvidersForResuming = new List<StorageResourceProvider>() { shares }
188+
});
189+
// Get resumable transfers from transfer manager
190+
await foreach (TransferProperties properties in transferManager.GetResumableTransfersAsync())
191+
{
192+
// Resume the transfer
193+
if (properties.SourceUri.AbsoluteUri == "https://storageaccount.blob.core.windows.net/containername/blobpath")
194+
{
195+
await transferManager.ResumeTransferAsync(properties.TransferId);
196+
}
197+
}
198+
```
199+
200+
For more information regarding pause, resume, and/or checkpointing, see [Pause and Resume Checkpointing](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.DataMovement/samples/PauseResumeCheckpointing.md).
201+
175202
## Troubleshooting
176203

177204
See [Handling Failed Transfers](#handling-failed-transfers) and [Enabling Logging](https://learn.microsoft.com/dotnet/azure/sdk/logging) to assist with any troubleshooting.

sdk/storage/Azure.Storage.DataMovement.Files.Shares/samples/Sample01b_HelloWorldAsync.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Azure.Storage.Files.Shares;
1111
using Azure.Storage.Sas;
1212
using System.Threading;
13+
using System.Collections.Generic;
1314

1415
namespace Azure.Storage.DataMovement.Files.Shares.Samples
1516
{
@@ -99,15 +100,15 @@ public async Task Upload()
99100
await share.CreateIfNotExistsAsync();
100101
try
101102
{
102-
ShareFilesStorageResourceProvider shares = new(new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey));
103-
104103
// Get a reference to a destination share file/directory
105104
Uri destinationFolderUri = share.GetDirectoryClient("sample-directory").Uri;
106105
Uri destinationFileUri = share.GetRootDirectoryClient().GetFileClient("sample-file").Uri;
107-
TransferManager transferManager = new TransferManager(new TransferManagerOptions());
108106

109107
// Create simple transfer single share file upload job
110108
#region Snippet:SimplefileUpload_Shares
109+
TokenCredential tokenCredential = new DefaultAzureCredential();
110+
ShareFilesStorageResourceProvider shares = new(tokenCredential);
111+
TransferManager transferManager = new TransferManager(new TransferManagerOptions());
111112
TransferOperation fileTransfer = await transferManager.StartTransferAsync(
112113
sourceResource: LocalFilesStorageResourceProvider.FromFile(sourceLocalFile),
113114
destinationResource: await shares.FromFileAsync(destinationFileUri));
@@ -213,6 +214,27 @@ public async Task Copy()
213214
}
214215
}
215216

217+
public async Task ResumeTransfersStoredAsync()
218+
{
219+
#region Snippet:TransferManagerResumeTransfers_Shares
220+
TokenCredential tokenCredential = new DefaultAzureCredential();
221+
ShareFilesStorageResourceProvider shares = new(tokenCredential);
222+
TransferManager transferManager = new TransferManager(new TransferManagerOptions()
223+
{
224+
ProvidersForResuming = new List<StorageResourceProvider>() { shares }
225+
});
226+
// Get resumable transfers from transfer manager
227+
await foreach (TransferProperties properties in transferManager.GetResumableTransfersAsync())
228+
{
229+
// Resume the transfer
230+
if (properties.SourceUri.AbsoluteUri == "https://storageaccount.blob.core.windows.net/containername/blobpath")
231+
{
232+
await transferManager.ResumeTransferAsync(properties.TransferId);
233+
}
234+
}
235+
#endregion
236+
}
237+
216238
public async Task<string> CreateFileShareTestDirectory(ShareClient client, int depth = 0, string basePath = default)
217239
{
218240
basePath = basePath ?? Path.GetTempFileName();

0 commit comments

Comments
 (0)