Skip to content

Commit f06ab3a

Browse files
Edits
1 parent bce242b commit f06ab3a

File tree

1 file changed

+86
-9
lines changed

1 file changed

+86
-9
lines changed

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

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ For general information about these approaches, see [Overview of application dev
4242
This article focuses on working with Azure Files resources using the following approaches:
4343

4444
- [Work with Azure Files using System.IO](#work-with-azure-files-using-systemio): Mount a file share using SMB or NFS and use the `System.IO` namespace to work with files and directories in the share.
45-
- [Work with Azure Files using the File Shares client library for .NET](#work-with-azure-files-using-the-file-shares-client-library-for-net): Use the Azure Storage File Shares client library for .NET to work with files and directories in a file share. This client library builds on the FileREST API.
46-
47-
To learn about using the Storage resource provider REST API and management libraries, see [Libraries for resource management](storage-files-developer-overview.md#libraries-for-resource-management).
45+
- [Work with Azure Files using the File Shares client library for .NET](#work-with-azure-files-data-using-the-file-shares-client-library-for-net): Use the Azure Storage File Shares client library for .NET to work with files and directories in a file share. This client library builds on the FileREST API.
46+
- [Manage Azure Files resources using the Azure Storage management libraries](#manage-azure-files-resources-using-the-azure-storage-management-libraries): Use the Azure Storage management libraries to manage file shares and other resources in your storage account. The management libraries build on the Azure Storage resource provider REST API.
4847

4948
## Prerequisites
5049

@@ -93,21 +92,23 @@ If you don't already have a .NET app, create one using Visual Studio or the .NET
9392

9493
### Install the package
9594

96-
If you plan to interact with Azure Files using the `System.IO` namespace, you don't need to install any additional packages. The `System.IO` namespace is included with the .NET SDK. If you plan to use the File Shares client library for .NET, install the package using NuGet.
95+
If you plan to interact with Azure Files using the `System.IO` namespace, you don't need to install any additional packages. The `System.IO` namespace is included with the .NET SDK. If you plan to use the File Shares client library for .NET or the Azure Storage management library for .NET, install the package using NuGet.
9796
9897
### [Visual Studio](#tab/visual-studio)
9998
10099
1. In **Solution Explorer**, right-click your project and choose **Manage NuGet Packages**.
101-
1. In **NuGet Package Manager**, select **Browse**. Then search for and choose **Azure.Storage.Files.Shares**. Select **Install**.
100+
1. In **NuGet Package Manager**, select **Browse**. Then search for and choose the appropriate package and select **Install**. For the File Shares client library, choose **Azure.Storage.Files.Shares**. For the Azure Storage management library, choose **Azure.ResourceManager.Storage**. For the Azure Identity library, which is needed for passwordless choose **Azure.Identity**.
102101
103102
This step installs the package and its dependencies.
104103
105104
### [.NET CLI](#tab/dotnet-cli)
106105
107-
1. In a console window, run the following command to install the `Azure.Storage.Files.Shares` package.
106+
1. In a console window, run the following command to install the `Azure.Storage.Files.Shares` or the `Azure.ResourceManager.Storage` package. You can also install the `Azure.Identity` package to use the `DefaultAzureCredential` class for authentication.
108107
109108
```dotnetcli
110109
dotnet add package Azure.Storage.Files.Shares
110+
dotnet add package Azure.ResourceManager.Storage
111+
dotnet add package Azure.Identity
111112
```
112113
113114
---
@@ -126,6 +127,18 @@ If you plan to use the File Shares client library for .NET, add the following us
126127
using Azure.Storage.Files.Shares;
127128
```
128129
130+
If you plan to use the Azure Storage management library for .NET, add the following using directive to the top of your *Program.cs* file:
131+
132+
```csharp
133+
using Azure.ResourceManager;
134+
```
135+
136+
To use the Azure Identity library for passwordless connections to Azure services, add the following using directive to the top of your *Program.cs* file:
137+
138+
```csharp
139+
using Azure.Identity;
140+
```
141+
129142
## Work with Azure Files using System.IO
130143
131144
Standard file I/O libraries are the most common way to access and work with Azure Files resources. When you mount a file share using SMB or NFS, your operating system redirects API requests for the local file system. This approach allows you to use standard file I/O libraries, such as `System.IO`, to interact with files and directories in the share.
@@ -279,7 +292,7 @@ static void EnumerateFileACLs(string filePath)
279292
}
280293
```
281294
282-
## Work with Azure Files using the File Shares client library for .NET
295+
## Work with Azure Files data using the File Shares client library for .NET
283296
284297
The FileREST API provides programmatic access to Azure Files. It allows you to call HTTPS endpoints to perform operations on file shares, directories, and files. The FileREST API is designed for high scalability and advanced features that might not be available through native protocols. The Azure SDK provides client libraries, such as the File Shares client library for .NET, that build on the FileREST API.
285298
@@ -289,7 +302,7 @@ Consider using the FileREST API and the File Share client library if your applic
289302
- **Custom cloud integrations:** Build custom value-added services, such as backup, antivirus, or data management, that interact directly with Azure Files.
290303
- **Performance optimization:** Benefit from performance advantages in high-scale scenarios using data plane operations.
291304
292-
The FileREST API models Azure Files as a hierarchy of resources, and is recommended for operations that are performed at the *directory* or *file* level. You should prefer the Storage resource provider REST API for operations that are performed at the *file service* or *file share* level.
305+
The FileREST API models Azure Files as a hierarchy of resources, and is recommended for operations that are performed at the *directory* or *file* level. You should prefer the [Storage resource provider REST API](#manage-azure-files-resources-using-the-azure-storage-management-libraries) for operations that are performed at the *file service* or *file share* level.
293306
294307
In this section, you learn how to use the File Shares client library to work with Azure Files resources.
295308
@@ -382,7 +395,71 @@ For information about how to obtain account keys and best practice guidelines fo
382395
383396
To learn more about each of these authorization mechanisms, see [Choose how to authorize access to file data](authorize-data-operations-portal.md).
384397
385-
### Example: TODO:Add examples here
398+
### Example: Copy files using the File Shares client library
399+
400+
The following code example shows how to copy a file to another file:
401+
402+
```csharp
403+
```
404+
405+
The following code example shows how to copy a file to a blob:
406+
407+
```csharp
408+
```
409+
410+
## Manage Azure Files resources using the Azure Storage management libraries
411+
412+
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.
413+
414+
The management libraries are recommended for operations that are performed at the *file service* or *file share* level. In this section, you learn how to use the Azure Storage management libraries to manage Azure Files resources.
415+
416+
### Example: Create a file share using the Azure Storage management library
417+
418+
The following code example shows how to create a top-level `ArmClient` object, register the Storage resource provider with a subscription, and create a file share using the Azure Storage management library:
419+
420+
```csharp
421+
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
422+
423+
// Create a resource identifier, then get the subscription resource
424+
ResourceIdentifier resourceIdentifier = new($"/subscriptions/<subscription-id>");
425+
SubscriptionResource subscription = armClient.GetSubscriptionResource(resourceIdentifier);
426+
427+
ResourceProviderResource resourceProvider =
428+
await subscription.GetResourceProviderAsync("Microsoft.Storage");
429+
430+
// Check the registration state of the resource provider and register, if needed
431+
if (resourceProvider.Data.RegistrationState == "NotRegistered")
432+
resourceProvider.Register();
433+
434+
// Get a resource group
435+
ResourceGroupResource resourceGroup = await subscription.GetResourceGroupAsync("<resource-group-name>");
436+
437+
// Get a collection of storage account resources
438+
StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts();
439+
440+
// Get a specific storage account resource
441+
StorageAccountResource storageAccount = await accountCollection.GetAsync("<storage-account-name>");
442+
443+
// Get a file service resource for the storage account
444+
FileServiceResource fileService = storageAccount.GetFileService();
445+
446+
// Create a new file share (or update if it already exists)
447+
ArmOperation <FileShareResource> fileShareOperation = await fileService
448+
.GetFileShares()
449+
.CreateOrUpdateAsync(WaitUntil.Completed, "sample-file-share", new FileShareData()
450+
{
451+
ShareQuota = 1024,
452+
// Add file share properties here
453+
});
454+
455+
// Get the file share resource
456+
FileShareResource fileShare = fileShareOperation.Value;
457+
```
458+
459+
You can configure the file share properties using the [FileShareData](/dotnet/api/azure.resourcemanager.storage.filesharedata) class. The previous example shows how to set the `ShareQuota` property.
460+
461+
>[!NOTE]
462+
> To perform the register operation, you need permissions for the following Azure RBAC action: Microsoft.Storage/register/action. This permission is included in the Contributor and Owner built-in roles.
386463
387464
## Related content
388465

0 commit comments

Comments
 (0)