使用 .NET 针对 Azure 文件进行开发Develop for Azure Files with .NET
了解开发使用 Azure 文件存储来存储数据的 .NET 应用程序的基础知识。Learn the basics of developing .NET applications that use Azure Files to store data. 本文介绍了如何创建一个简单的控制台应用程序,以便通过 .NET 和 Azure 文件存储执行以下操作:This article shows how to create a simple console application to do the following with .NET and Azure Files:
- 获取文件内容。Get the contents of a file.
- 为文件共享设置最大大小或配额。Set the maximum size, or quota, for a file share.
- 为文件创建共享访问签名 (SAS)。Create a shared access signature (SAS) for a file.
- 将文件复制到同一存储帐户中的另一个文件。Copy a file to another file in the same storage account.
- 将文件复制到同一存储帐户中的一个 Blob。Copy a file to a blob in the same storage account.
- 创建文件共享的快照。Create a snapshot of a file share.
- 从共享快照还原文件。Restore a file from a share snapshot.
- 使用 Azure 存储度量值进行故障排除。Use Azure Storage Metrics for troubleshooting.
若要了解有关 Azure 文件存储的详细信息,请参阅什么是 Azure 文件存储?To learn more about Azure Files, see What is Azure Files?
提示
查看 Azure 存储代码示例存储库Check out the Azure Storage code samples repository
如需易用且能够下载和运行的端到端 Azure 存储代码示例,请查看我们的 Azure 存储示例列表。For easy-to-use end-to-end Azure Storage code samples that you can download and run, please check out our list of Azure Storage Samples.
了解 .NET APIUnderstanding the .NET APIs
Azure 文件为客户端应用程序提供两个主要方法:服务器消息块 (SMB) 和 REST。Azure Files provides two broad approaches to client applications: Server Message Block (SMB) and REST. 在 .NET 中,System.IO
和 Azure.Storage.Files.Shares
API 将抽象化这些方法。Within .NET, the System.IO
and Azure.Storage.Files.Shares
APIs abstract these approaches.
APIAPI | 何时使用When to use | 说明Notes |
---|---|---|
System.IOSystem.IO | 应用程序:Your application:
|
一般情况下,通过 SMB 使用 Azure 文件存储实现的文件 I/O 与使用任何网络文件共享或本地存储设备实现的 I/O 相同。File I/O implemented with Azure Files over SMB is generally the same as I/O with any network file share or local storage device. 有关 .NET 中的许多功能(包括文件 I/O)的简介,请参阅控制台应用程序教程。For an introduction to a number of features in .NET, including file I/O, see the Console Application tutorial. |
Azure.Storage.Files.SharesAzure.Storage.Files.Shares | 应用程序:Your application:
|
本文演示如何通过 REST(而不是 SMB)将 Azure.Storage.Files.Shares 用于文件 I/O 以及如何管理文件共享。This article demonstrates the use of Azure.Storage.Files.Shares for file I/O using REST instead of SMB and management of the file share. |
创建控制台应用程序,并获取程序集Create the console application and obtain the assembly
可以在任意类型的 .NET 应用中使用 Azure 文件存储客户端库。You can use the Azure Files client library in any type of .NET app. 这些应用包括 Azure 云应用、Web 应用、桌面应用和移动应用。These apps include Azure cloud, web, desktop, and mobile apps. 为简单起见,我们在本指南中创建一个控制台应用程序。In this guide, we create a console application for simplicity.
在 Visual Studio 中创建新的 Windows 控制台应用程序。In Visual Studio, create a new Windows console application. 以下步骤演示了如何在 Visual Studio 2019 中创建控制台应用程序。The following steps show you how to create a console application in Visual Studio 2019. 在其他版本的 Visual Studio 中,这些步骤是类似的。The steps are similar in other versions of Visual Studio.
- 启动 Visual Studio 并选择“创建新项目”。 Start Visual Studio and select Create a new project.
- 在“创建新项目” 中,选择用于 C# 的“控制台应用(.NET Framework)” ,然后选择“下一步” 。In Create a new project, choose Console App (.NET Framework) for C#, and then select Next.
- 在“配置新项目”中输入应用的名称,然后选择“创建”。 In Configure your new project, enter a name for the app, and select Create.
将本文中的所有代码示例添加到 Program.cs 文件中的 Program
类。Add all the code examples in this article to the Program
class in the Program.cs file.
使用 NuGet 安装所需包Use NuGet to install the required packages
在你的项目中引用以下包:Refer to these packages in your project:
- 适用于 .NET 的 Azure Core 库:此包是 Azure 客户端管道的实现。Azure core library for .NET: This package is the implementation of the Azure client pipeline.
- 适用于 .NET 的 Azure 存储 Blob 客户端库:使用此包能够以编程方式访问存储帐户中的 Blob 资源。Azure Storage Blob client library for .NET: This package provides programmatic access to blob resources in your storage account.
- 适用于 .NET 的 Azure 存储文件客户端库:使用此包能够以编程方式访问存储帐户中的文件资源。Azure Storage Files client library for .NET: This package provides programmatic access to file resources in your storage account.
- 适用于 .NET 的系统配置管理器库:此包提供了一个类,用于在配置文件中存储和检索值。System Configuration Manager library for .NET: This package provides a class storing and retrieving values in a configuration file.
可以使用 NuGet 获取这些包。You can use NuGet to obtain the packages. 执行以下步骤:Follow these steps:
在“解决方案资源管理器”中,右键单击你的项目并选择“管理 NuGet 包” 。In Solution Explorer, right-click your project and choose Manage NuGet Packages.
在“NuGet 包管理器”中选择“浏览”。 In NuGet Package Manager, select Browse. 接着搜索并选择“Azure.Core”,然后选择“安装”。 Then search for and choose Azure.Core, and then select Install.
此步骤将安装该包及其依赖项。This step installs the package and its dependencies.
搜索并安装以下包:Search for and install these packages:
- Azure.Storage.BlobsAzure.Storage.Blobs
- Azure.Storage.Files.SharesAzure.Storage.Files.Shares
- System.Configuration.ConfigurationManagerSystem.Configuration.ConfigurationManager
将存储帐户凭据保存到 App.config 文件Save your storage account credentials to the App.config file
接下来,将凭据保存到项目的 App.config 文件中。Next, save your credentials in your project's App.config file. 在“解决方案资源管理器”中,双击 App.config
并编辑该文件,使其类似于以下示例。In Solution Explorer, double-click App.config
and edit the file so that it is similar to the following example.
请将 myaccount
替换为你的存储帐户名,将 mykey
替换为你的存储帐户密钥。Replace myaccount
with your storage account name and mykey
with your storage account key.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="StorageConnectionString"
value="DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey;EndpointSuffix=core.chinacloudapi.cn" />
<add key="StorageAccountName" value="myaccount" />
<add key="StorageAccountKey" value="mykey" />
</appSettings>
</configuration>
备注
Azurite 存储模拟器目前不支持 Azure 文件存储。The Azurite storage emulator does not currently support Azure Files. 连接字符串必须针对云中要使用 Azure 文件存储的 Azure 存储帐户。Your connection string must target an Azure storage account in the cloud to work with Azure Files.
添加 using 指令Add using directives
在“解决方案资源管理器”中打开 Program.cs 文件,并在该文件顶部添加以下 using 指令。In Solution Explorer, open the Program.cs file, and add the following using directives to the top of the file.
using System;
using System.Configuration;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using Azure.Storage.Sas;
以编程方式访问文件共享Access the file share programmatically
在 Program.cs 文件中添加以下代码,以便以编程方式访问文件共享。In the Program.cs file, add the following code to access the file share programmatically.
如果文件共享尚不存在,则以下方法将创建该文件共享。The following method creates a file share if it doesn't already exist. 该方法首先从连接字符串创建一个 ShareClient 对象。The method starts by creating a ShareClient object from a connection string. 然后,该示例会尝试下载我们先前创建的文件。The sample then attempts to download a file we created earlier. 从 Main()
调用此方法。Call this method from Main()
.
//-------------------------------------------------
// Create a file share
//-------------------------------------------------
public async Task CreateShareAsync(string shareName)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a ShareClient which will be used to create and manipulate the file share
ShareClient share = new ShareClient(connectionString, shareName);
// Create the share if it doesn't already exist
await share.CreateIfNotExistsAsync();
// Ensure that the share exists
if (await share.ExistsAsync())
{
Console.WriteLine($"Share created: {share.Name}");
// Get a reference to the sample directory
ShareDirectoryClient directory = share.GetDirectoryClient("CustomLogs");
// Create the directory if it doesn't already exist
await directory.CreateIfNotExistsAsync();
// Ensure that the directory exists
if (await directory.ExistsAsync())
{
// Get a reference to a file object
ShareFileClient file = directory.GetFileClient("Log1.txt");
// Ensure that the file exists
if (await file.ExistsAsync())
{
Console.WriteLine($"File exists: {file.Name}");
// Download the file
ShareFileDownloadInfo download = await file.DownloadAsync();
// Save the data to a local file, overwrite if the file already exists
using (FileStream stream = File.OpenWrite(@"downloadedLog1.txt"))
{
await download.Content.CopyToAsync(stream);
await stream.FlushAsync();
stream.Close();
// Display where the file was saved
Console.WriteLine($"File downloaded: {stream.Name}");
}
}
}
}
else
{
Console.WriteLine($"CreateShareAsync failed");
}
}
设置文件共享的最大大小Set the maximum size for a file share
从 Azure 文件存储客户端库的 5.x 版开始,可以设置文件共享的配额(最大大小)。Beginning with version 5.x of the Azure Files client library, you can set the quota (maximum size) for a file share. 还可以查看共享当前存储了多少数据。You can also check to see how much data is currently stored on the share.
设置共享配额可以限制在该共享上存储的文件的总大小。Setting the quota for a share limits the total size of the files stored on the share. 如果共享上的文件总大小超出了配额,则客户端无法增大现有文件的大小。If the total size of files on the share exceeds the quota, clients can't increase the size of existing files. 客户端也无法创建新文件,除非这些文件是空的。Clients also can't create new files, unless those files are empty.
下面的示例演示如何检查共享的当前使用情况,以及如何设置共享的配额。The example below shows how to check the current usage for a share and how to set the quota for the share.
//-------------------------------------------------
// Set the maximum size of a share
//-------------------------------------------------
public async Task SetMaxShareSizeAsync(string shareName, int increaseSizeInGiB)
{
const long ONE_GIBIBYTE = 10737420000; // Number of bytes in 1 gibibyte
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a ShareClient which will be used to access the file share
ShareClient share = new ShareClient(connectionString, shareName);
// Create the share if it doesn't already exist
await share.CreateIfNotExistsAsync();
// Ensure that the share exists
if (await share.ExistsAsync())
{
// Get and display current share quota
ShareProperties properties = await share.GetPropertiesAsync();
Console.WriteLine($"Current share quota: {properties.QuotaInGB} GiB");
// Get and display current usage stats for the share
ShareStatistics stats = await share.GetStatisticsAsync();
Console.WriteLine($"Current share usage: {stats.ShareUsageInBytes} bytes");
// Convert current usage from bytes into GiB
int currentGiB = (int)(stats.ShareUsageInBytes / ONE_GIBIBYTE);
// This line sets the quota to be the current
// usage of the share plus the increase amount
await share.SetQuotaAsync(currentGiB + increaseSizeInGiB);
// Get the new quota and display it
properties = await share.GetPropertiesAsync();
Console.WriteLine($"New share quota: {properties.QuotaInGB} GiB");
}
}
为文件或文件共享生成共享访问签名Generate a shared access signature for a file or file share
从 Azure 文件存储客户端库的 5.x 版开始,可以为文件共享或单个文件生成共享访问签名 (SAS)。Beginning with version 5.x of the Azure Files client library, you can generate a shared access signature (SAS) for a file share or for an individual file.
下面的示例方法返回指定共享中文件上的 SAS。The following example method returns a SAS on a file in the specified share.
//-------------------------------------------------
// Create a SAS URI for a file
//-------------------------------------------------
public Uri GetFileSasUri(string shareName, string filePath, DateTime expiration, ShareFileSasPermissions permissions)
{
// Get the account details from app settings
string accountName = ConfigurationManager.AppSettings["StorageAccountName"];
string accountKey = ConfigurationManager.AppSettings["StorageAccountKey"];
ShareSasBuilder fileSAS = new ShareSasBuilder()
{
ShareName = shareName,
FilePath = filePath,
// Specify an Azure file resource
Resource = "f",
// Expires in 24 hours
ExpiresOn = expiration
};
// Set the permissions for the SAS
fileSAS.SetPermissions(permissions);
// Create a SharedKeyCredential that we can use to sign the SAS token
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
// Build a SAS URI
UriBuilder fileSasUri = new UriBuilder($"https://{accountName}.file.core.chinacloudapi.cn/{fileSAS.ShareName}/{fileSAS.FilePath}");
fileSasUri.Query = fileSAS.ToSasQueryParameters(credential).ToString();
// Return the URI
return fileSasUri.Uri;
}
若要详细了解如何创建和使用共享访问签名,请参阅共享访问签名的工作原理。For more information about creating and using shared access signatures, see How a shared access signature works.
复制文件Copy files
从 Azure 文件存储客户端库的 5.x 版开始,可以将一个文件复制到另一个文件,将一个文件复制到一个 Blob,或将一个 Blob 复制到一个文件。Beginning with version 5.x of the Azure Files client library, you can copy a file to another file, a file to a blob, or a blob to a file.
还可以使用 AzCopy 将一个文件复制到另一个文件或将一个 Blob 复制到一个文件,反之亦然。You can also use AzCopy to copy one file to another or to copy a blob to a file or the other way around. 请参阅 AzCopy 入门。See Get started with AzCopy.
备注
如果将一个 Blob 复制到一个文件,或将一个文件复制到一个 Blob,必须使用共享访问签名 (SAS) 授予对源对象的访问权限,即使是在同一存储帐户内进行复制。If you are copying a blob to a file, or a file to a blob, you must use a shared access signature (SAS) to authorize access to the source object, even if you are copying within the same storage account.
将一个文件复制到另一个文件Copy a file to another file
以下示例将一个文件复制到同一共享中的另一个文件。The following example copies a file to another file in the same share. 可以使用共享密钥身份验证执行复制,因为此操作在同一存储帐户内复制文件。You can use Shared Key authentication to do the copy because this operation copies files within the same storage account.
//-------------------------------------------------
// Copy file within a directory
//-------------------------------------------------
public async Task CopyFileAsync(string shareName, string sourceFilePath, string destFilePath)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Get a reference to the file we created previously
ShareFileClient sourceFile = new ShareFileClient(connectionString, shareName, sourceFilePath);
// Ensure that the source file exists
if (await sourceFile.ExistsAsync())
{
// Get a reference to the destination file
ShareFileClient destFile = new ShareFileClient(connectionString, shareName, destFilePath);
// Start the copy operation
await destFile.StartCopyAsync(sourceFile.Uri);
if (await destFile.ExistsAsync())
{
Console.WriteLine($"{sourceFile.Uri} copied to {destFile.Uri}");
}
}
}
将文件复制到 BlobCopy a file to a blob
以下示例创建一个文件并将其复制到同一存储帐户中的某个 blob。The following example creates a file and copies it to a blob within the same storage account. 该示例为源文件创建一个 SAS,服务在复制操作期间使用该 SAS 授予对源文件的访问权限。The example creates a SAS for the source file, which the service uses to authorize access to the source file during the copy operation.
//-------------------------------------------------
// Copy a file from a share to a blob
//-------------------------------------------------
public async Task CopyFileToBlobAsync(string shareName, string sourceFilePath, string containerName, string blobName)
{
// Get a file SAS from the method created ealier
Uri fileSasUri = GetFileSasUri(shareName, sourceFilePath, DateTime.UtcNow.AddHours(24), ShareFileSasPermissions.Read);
// Get a reference to the file we created previously
ShareFileClient sourceFile = new ShareFileClient(fileSasUri);
// Ensure that the source file exists
if (await sourceFile.ExistsAsync())
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Get a reference to the destination container
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
// Create the container if it doesn't already exist
await container.CreateIfNotExistsAsync();
BlobClient destBlob = container.GetBlobClient(blobName);
await destBlob.StartCopyFromUriAsync(sourceFile.Uri);
if (await destBlob.ExistsAsync())
{
Console.WriteLine($"File {sourceFile.Name} copied to blob {destBlob.Name}");
}
}
}
可以用相同的方式将一个 Blob 复制到一个文件。You can copy a blob to a file in the same way. 如果源对象是一个 Blob,则创建一个 SAS,以便在复制操作期间授予对该 Blob 的访问权限。If the source object is a blob, then create a SAS to authorize access to that blob during the copy operation.
共享快照Share snapshots
从 Azure 文件存储客户端库的 8.5 版开始,可以创建共享快照。Beginning with version 8.5 of the Azure Files client library, you can create a share snapshot. 还可以列出或浏览共享快照,以及删除共享快照。You can also list or browse share snapshots and delete share snapshots. 创建后,共享快照是只读的。Once created, share snapshots are read-only.
创建共享快照Create share snapshots
下面的示例创建文件共享快照。The following example creates a file share snapshot.
//-------------------------------------------------
// Create a share snapshot
//-------------------------------------------------
public async Task CreateShareSnapshotAsync(string shareName)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareServiceClient = new ShareServiceClient(connectionString);
// Instantiate a ShareClient which will be used to access the file share
ShareClient share = shareServiceClient.GetShareClient(shareName);
// Ensure that the share exists
if (await share.ExistsAsync())
{
// Create a snapshot
ShareSnapshotInfo snapshotInfo = await share.CreateSnapshotAsync();
Console.WriteLine($"Snapshot created: {snapshotInfo.Snapshot}");
}
}
列出共享快照List share snapshots
以下示例列出共享上的快照。The following example lists the snapshots on a share.
//-------------------------------------------------
// List the snapshots on a share
//-------------------------------------------------
public void ListShareSnapshots()
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareServiceClient = new ShareServiceClient(connectionString);
// Display each share and the snapshots on each share
foreach (ShareItem item in shareServiceClient.GetShares(ShareTraits.All, ShareStates.Snapshots))
{
if (null != item.Snapshot)
{
Console.WriteLine($"Share: {item.Name}\tSnapshot: {item.Snapshot}");
}
}
}
列出共享快照中的文件和目录List files and directories within share snapshots
以下示例浏览共享快照中的文件和目录。The following example browses files and directories within share snapshots.
//-------------------------------------------------
// List the snapshots on a share
//-------------------------------------------------
public void ListSnapshotContents(string shareName, string snapshotTime)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareService = new ShareServiceClient(connectionString);
// Get a ShareClient
ShareClient share = shareService.GetShareClient(shareName);
Console.WriteLine($"Share: {share.Name}");
// Get as ShareClient that points to a snapshot
ShareClient snapshot = share.WithSnapshot(snapshotTime);
// Get the root directory in the snapshot share
ShareDirectoryClient rootDir = snapshot.GetRootDirectoryClient();
// Recursively list the directory tree
ListDirTree(rootDir);
}
//-------------------------------------------------
// Recursively list a directory tree
//-------------------------------------------------
public void ListDirTree(ShareDirectoryClient dir)
{
// List the files and directories in the snapshot
foreach (ShareFileItem item in dir.GetFilesAndDirectories())
{
if (item.IsDirectory)
{
Console.WriteLine($"Directory: {item.Name}");
ShareDirectoryClient subDir = dir.GetSubdirectoryClient(item.Name);
ListDirTree(subDir);
}
else
{
Console.WriteLine($"File: {dir.Name}\\{item.Name}");
}
}
}
从共享快照还原文件共享或文件Restore file shares or files from share snapshots
创建文件共享的快照即可恢复各个文件或整个文件共享。Taking a snapshot of a file share enables you to recover individual files or the entire file share.
查询文件共享的共享快照即可从文件共享快照还原文件。You can restore a file from a file share snapshot by querying the share snapshots of a file share. 然后,可以检索属于特定共享快照的文件。You can then retrieve a file that belongs to a particular share snapshot. 使用该版本直接读取或还原文件。Use that version to directly read or to restore the file.
//-------------------------------------------------
// Restore file from snapshot
//-------------------------------------------------
public async Task RestoreFileFromSnapshot(string shareName, string directoryName, string fileName, string snapshotTime)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareService = new ShareServiceClient(connectionString);
// Get a ShareClient
ShareClient share = shareService.GetShareClient(shareName);
// Get as ShareClient that points to a snapshot
ShareClient snapshot = share.WithSnapshot(snapshotTime);
// Get a ShareDirectoryClient, then a ShareFileClient to the snapshot file
ShareDirectoryClient snapshotDir = snapshot.GetDirectoryClient(directoryName);
ShareFileClient snapshotFile = snapshotDir.GetFileClient(fileName);
// Get a ShareDirectoryClient, then a ShareFileClient to the live file
ShareDirectoryClient liveDir = share.GetDirectoryClient(directoryName);
ShareFileClient liveFile = liveDir.GetFileClient(fileName);
// Restore the file from the snapshot
ShareFileCopyInfo copyInfo = await liveFile.StartCopyAsync(snapshotFile.Uri);
// Display the status of the operation
Console.WriteLine($"Restore status: {copyInfo.CopyStatus}");
}
删除共享快照Delete share snapshots
下面的示例删除文件共享快照。The following example deletes a file share snapshot.
//-------------------------------------------------
// Delete a snapshot
//-------------------------------------------------
public async Task DeleteSnapshotAsync(string shareName, string snapshotTime)
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareService = new ShareServiceClient(connectionString);
// Get a ShareClient
ShareClient share = shareService.GetShareClient(shareName);
// Get a ShareClient that points to a snapshot
ShareClient snapshotShare = share.WithSnapshot(snapshotTime);
try
{
// Delete the snapshot
await snapshotShare.DeleteIfExistsAsync();
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Exception: {ex.Message}");
Console.WriteLine($"Error code: {ex.Status}\t{ex.ErrorCode}");
}
}
使用指标排查 Azure 文件存储问题Troubleshoot Azure Files by using metrics
Azure 存储分析支持用于 Azure 文件存储的指标。Azure Storage Analytics supports metrics for Azure Files. 使用指标数据,可以跟踪请求和诊断问题。With metrics data, you can trace requests and diagnose issues.
可以通过 Azure 门户为 Azure 文件存储启用指标。You can enable metrics for Azure Files from the Azure portal. 还可以通过 REST API 或 Azure 文件存储客户端库中的类似功能之一调用设置文件服务属性操作,以编程方式启用指标。You can also enable metrics programmatically by calling the Set File Service Properties operation with the REST API or one of its analogs in the Azure Files client library.
以下代码示例演示了如何使用 .NET 客户端库启用 Azure 文件存储的指标。The following code example shows how to use the .NET client library to enable metrics for Azure Files.
//-------------------------------------------------
// Use metrics
//-------------------------------------------------
public async Task UseMetricsAsync()
{
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instatiate a ShareServiceClient
ShareServiceClient shareService = new ShareServiceClient(connectionString);
// Set metrics properties for File service
await shareService.SetPropertiesAsync(new ShareServiceProperties()
{
// Set hour metrics
HourMetrics = new ShareMetrics()
{
Enabled = true,
IncludeApis = true,
Version = "1.0",
RetentionPolicy = new ShareRetentionPolicy()
{
Enabled = true,
Days = 14
}
},
// Set minute metrics
MinuteMetrics = new ShareMetrics()
{
Enabled = true,
IncludeApis = true,
Version = "1.0",
RetentionPolicy = new ShareRetentionPolicy()
{
Enabled = true,
Days = 7
}
}
});
// Read the metrics properties we just set
ShareServiceProperties serviceProperties = await shareService.GetPropertiesAsync();
// Display the properties
Console.WriteLine();
Console.WriteLine($"HourMetrics.InludeApis: {serviceProperties.HourMetrics.IncludeApis}");
Console.WriteLine($"HourMetrics.RetentionPolicy.Days: {serviceProperties.HourMetrics.RetentionPolicy.Days}");
Console.WriteLine($"HourMetrics.Version: {serviceProperties.HourMetrics.Version}");
Console.WriteLine();
Console.WriteLine($"MinuteMetrics.InludeApis: {serviceProperties.MinuteMetrics.IncludeApis}");
Console.WriteLine($"MinuteMetrics.RetentionPolicy.Days: {serviceProperties.MinuteMetrics.RetentionPolicy.Days}");
Console.WriteLine($"MinuteMetrics.Version: {serviceProperties.MinuteMetrics.Version}");
Console.WriteLine();
}
如果遇到任何问题,可以参阅在 Windows 中排查 Azure 文件存储问题。If you encounter any problems, you can refer to Troubleshoot Azure Files problems in Windows.
后续步骤Next steps
有关 Azure 文件存储的详细信息,请参阅以下资源:For more information about Azure Files, see the following resources:
概念性文章Conceptual articles
文件存储的工具支持Tooling support for File storage
- AzCopy 入门Get started with AzCopy
- 在 Windows 中排查 Azure 文件问题Troubleshoot Azure Files problems in Windows