Skip to content

Commit 3462954

Browse files
Edits and more examples
1 parent 66e61c1 commit 3462954

File tree

2 files changed

+107
-16
lines changed

2 files changed

+107
-16
lines changed

articles/storage/files/storage-dotnet-how-to-use-files.md

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to develop .NET applications and services that use Azure
55
author: pauljewellmsft
66
ms.service: azure-file-storage
77
ms.topic: conceptual
8-
ms.date: 02/14/2025
8+
ms.date: 03/13/2025
99
ms.author: pauljewell
1010
ms.devlang: csharp
1111
ms.custom: devx-track-csharp, devx-track-dotnet
@@ -228,7 +228,9 @@ static void WriteToFile(string fileSharePath, string fileName)
228228
229229
### Example: Lock a file in a file share using System.IO
230230
231-
The following code example shows how to lock a file in a file share:
231+
SMB clients that mount file shares can use file system locking mechanisms to manage access to shared files.
232+
233+
The following code example shows how to lock a file in a file share with share mode set to `None`. This share mode declines sharing of the current file until the file is closed.
232234
233235
```csharp
234236
using System.IO;
@@ -259,6 +261,8 @@ static void LockFile(string filePath)
259261
}
260262
```
261263
264+
When using both SMB and the FileREST API, be aware that the FileREST API uses [leases](#example-lease-a-file-using-the-file-shares-client-library) to manage file locks, while SMB uses file system locks managed by the operating system. To learn more about managing file locking interactions between SMB and the FileREST API, see [Manage file locks](/rest/api/storageservices/managing-file-locks).
265+
262266
### Example: Enumerate file ACLs using System.IO
263267
264268
The following code example shows how to enumerate ACLs for a file:
@@ -416,15 +420,16 @@ string destFilePath = "dest/path/to/file";
416420
417421
TokenCredential tokenCredential = new DefaultAzureCredential();
418422
419-
ShareFileClient srcShareFileClient = new(
420-
new Uri($"https://{accountName}.file.core.windows.net/{srcShareName}/{srcFilePath}"),
421-
tokenCredential);
422-
423423
ShareClientOptions options = new()
424424
{
425425
ShareTokenIntent = ShareTokenIntent.Backup,
426426
};
427427
428+
ShareFileClient srcShareFileClient = new(
429+
new Uri($"https://{accountName}.file.core.windows.net/{srcShareName}/{srcFilePath}"),
430+
tokenCredential,
431+
options);
432+
428433
ShareFileClient destShareFileClient = new(
429434
new Uri($"https://{accountName}.file.core.windows.net/{destShareName}/{destFilePath}"),
430435
tokenCredential,
@@ -435,15 +440,99 @@ ShareFileClient destShareFileClient = new(
435440
await destShareFileClient.StartCopyAsync(srcShareFileClient.Uri);
436441
```
437442
438-
## Lease a file using the File Shares client library
443+
### Example: Lease a file using the File Shares client library
439444
440-
A lease creates a lock on a file that's managed by Azure via a lease ID. The lease provides a mechanism to coordinate access to files across multiple clients in a distributed system. A lease on a file provides exclusive write and delete access. To learn more about lease duration and actions, see [Lease File](/rest/api/storageservices/lease-file).
445+
A lease creates a lock on a file that's managed by Azure via a lease ID. The lease provides a mechanism to coordinate access to files across multiple clients in a distributed system. A lease on a file provides exclusive write and delete access. To learn more about lease states and actions, see [Lease File](/rest/api/storageservices/lease-file#remarks).
441446
442-
The following code example shows how to acquire a lease on a file:
447+
The following code example shows how to create a lease client, acquire a infinite duration lease on a file, and release the lease:
443448
444449
```csharp
450+
string accountName = "<account-name>";
451+
string shareName = "sample-file-share";
452+
string filePath = "path/to/file";
453+
454+
TokenCredential tokenCredential = new DefaultAzureCredential();
455+
456+
ShareClientOptions options = new()
457+
{
458+
ShareTokenIntent = ShareTokenIntent.Backup,
459+
};
460+
461+
ShareFileClient fileClient = new(
462+
new Uri($"https://{accountName}.file.core.windows.net/{shareName}/{filePath}"),
463+
tokenCredential,
464+
options);
465+
466+
ShareLeaseClient leaseClient = fileClient.GetShareLeaseClient();
467+
468+
// Acquire a lease on the source file
469+
await leaseClient.AcquireAsync(duration: ShareLeaseClient.InfiniteLeaseDuration);
470+
471+
// Do something with the file
472+
473+
// Release the lease
474+
await leaseClient.ReleaseAsync();
445475
```
446476
477+
When using both SMB and the FileREST API, be aware that the FileREST API uses [leases](#example-lease-a-file-using-the-file-shares-client-library) to manage file locks, while SMB uses file system locks managed by the operating system. To learn more about managing file locking interactions between SMB and the FileREST API, see [Manage file locks](/rest/api/storageservices/managing-file-locks).
478+
479+
### Example: Create and list share snapshots using the File Shares client library
480+
481+
Share snapshots are read-only copies of a file share at a point in time. You can create a snapshot of a file share, and then use the snapshot to access the data in the share at the time the snapshot was created. You can also list all snapshots in a file share, and delete share snapshots.
482+
483+
The following code example shows how to create a share snapshot, list the snapshots in a file share, and traverse the directory tree in a share snapshot:
484+
485+
```csharp
486+
string connectionString = "<connection-string>";
487+
488+
ShareServiceClient shareServiceClient = new ShareServiceClient(connectionString);
489+
ShareClient shareClient = shareServiceClient.GetShareClient("sample-file-share");
490+
491+
// Create a snapshot
492+
493+
ShareSnapshotInfo snapshotInfo = await shareClient.CreateSnapshotAsync();
494+
Console.WriteLine($"Snapshot created: {snapshotInfo.Snapshot}");
495+
496+
// List snapshots in a share
497+
498+
await foreach (ShareItem shareItem in shareServiceClient.GetSharesAsync(ShareTraits.All, ShareStates.Snapshots))
499+
{
500+
if (shareItem.Snapshot != null)
501+
{
502+
Console.WriteLine($"Share: {shareItem.Name} (Snapshot: {shareItem.Snapshot})");
503+
}
504+
}
505+
506+
// List directories and files in a share snapshot
507+
508+
string snapshotTimestamp = snapshotInfo.Snapshot.ToString();
509+
ShareClient shareSnapshot = shareClient.WithSnapshot(snapshotTimestamp);
510+
ShareDirectoryClient rootDir = shareSnapshot.GetRootDirectoryClient();
511+
512+
await ListDirectoryTreeAsync(rootDir);
513+
514+
static async Task ListDirectoryTreeAsync(ShareDirectoryClient directory)
515+
{
516+
await foreach (ShareFileItem fileItem in directory.GetFilesAndDirectoriesAsync())
517+
{
518+
if (fileItem.IsDirectory)
519+
{
520+
Console.WriteLine($"Directory: {fileItem.Name}");
521+
await ListDirectoryTreeAsync(directory.GetSubdirectoryClient(fileItem.Name));
522+
}
523+
else
524+
{
525+
Console.WriteLine($"File: {fileItem.Name}");
526+
}
527+
}
528+
}
529+
```
530+
531+
>[!NOTE]
532+
> OAuth tokens, such as those obtained when using `DefaultAzureCredential`, aren't allowed for data plane operations at the file share level. To work with share snapshots, the client object must be authorized using the account key. The `ShareClient` object created in this code example uses a connection string, which includes the account key.
533+
>
534+
> Storing account keys or connection strings presents a security risk. You should only use them when Microsoft Entra authentication isn't available. To learn more about securely storing account keys in Azure Key Vault, see [About Azure Key Vault managed storage account keys](/azure/key-vault/secrets/about-managed-storage-account-keys).
535+
447536
## Manage Azure Files resources using the Azure Storage management libraries
448537
449538
The Azure Storage management libraries are built on the Azure Storage resource provider REST API. The Azure Storage resource provider is a service based on [Azure Resource Manager](/azure/azure-resource-manager/management/overview), and supports both declarative (templates) and imperative (direct API call) methods. The Azure Storage resource provider REST API provides programmatic access to Azure Storage resources, including file shares. The Azure SDK provides management libraries that build on the Azure Storage resource provider REST API.
@@ -514,16 +603,18 @@ await foreach (FileShareResource shareResource in fileService.GetFileShares().Ge
514603
Console.WriteLine($"Resource name: {resourceData.Name}");
515604
if (resourceData.SnapshotOn.HasValue)
516605
{
517-
Console.WriteLine($"Snapshot created: {resourceData.SnapshotOn}");
606+
Console.WriteLine($"Snapshot: {resourceData.SnapshotOn}");
518607
}
519608
}
520609
```
521610
522611
## Related content
523612
524-
For more information about Azure Files, see the following resources:
613+
For more information about developing with Azure Files, see the following resources:
614+
615+
- [Overview of application development with Azure Files](storage-files-developer-overview.md)
616+
- [Naming and referencing shares, directories, files, and metadata](/rest/api/storageservices/naming-and-referencing-shares--directories--files--and-metadata)
617+
- [Manage file locks](/rest/api/storageservices/managing-file-locks)
618+
- [Operations on directories](/rest/api/storageservices/operations-on-directories)
619+
- [Operations on files](/rest/api/storageservices/operations-on-files)
525620
526-
- [Get started with AzCopy](../common/storage-use-azcopy-v10.md?toc=/azure/storage/files/toc.json)
527-
- [Troubleshoot Azure Files](/troubleshoot/azure/azure-storage/files-troubleshoot?toc=/azure/storage/files/toc.json)
528-
- [Azure Storage APIs for .NET](/dotnet/api/overview/azure/storage)
529-
- [File Service REST API](/rest/api/storageservices/File-Service-REST-API)

articles/storage/files/storage-files-developer-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to develop applications and services that use Azure Files
55
author: pauljewellmsft
66
ms.service: azure-file-storage
77
ms.topic: conceptual
8-
ms.date: 02/13/2025
8+
ms.date: 03/13/2025
99
ms.author: pauljewell
1010
---
1111

0 commit comments

Comments
 (0)