使用 .NET 管理 blob 属性和元数据

除 Blob 包含的数据外,它们还支持系统属性和用户定义的元数据。 本文介绍如何使用用于 .NET 的 Azure 存储客户端库管理系统属性和用户定义元数据。

先决条件

关于属性和元数据

  • 系统属性:系统属性存在于每个 Blob 存储资源上。 其中一些属性是可以读取或设置的,而另一些属性是只读的。 事实上,有些系统属性与某些标准 HTTP 标头对应。 用于 .NET 的 Azure 存储客户端库将维护这些属性。

  • 用户定义的元数据:用户定义元数据包含一个或多个你为 Blob 存储资源指定的名称/值对对。 可以使用元数据存储资源的其他值。 元数据值仅用于你自己的目的,不会影响资源的行为方式。

    元数据名称/值对是有效的 HTTP 标头,因此应当遵循所有控制 HTTP 标头的限制。 有关元数据命名要求的详细信息,请参阅元数据名称

注意

使用 Blob 索引标记,还可以将任意用户定义的键/值属性与 Azure Blob 存储资源一起存储。 虽然与元数据类似,但只会自动为 Blob 索引标记编制索引,并由本机的 blob 服务进行搜索。 除非使用了单独的服务(如 Azure 搜索),否则无法对元数据进行索引和查询。

若要详细了解此功能,请参阅通过 Blob 索引管理和查找 Azure Blob 存储上的数据

设置和检索属性

以下代码示例设置 blob 的 ContentTypeContentLanguage 系统属性。

若要在 Blob 上设置属性,请调用 SetHttpHeadersSetHttpHeadersAsync。 清除未显式设置的任何属性。 下面的代码示例首先获取 Blob 上的现有属性,然后使用它们填充未更新的标头。

public static async Task SetBlobPropertiesAsync(BlobClient blob)
{
    Console.WriteLine("Setting blob properties...");

    try
    {
        // Get the existing properties
        BlobProperties properties = await blob.GetPropertiesAsync();

        BlobHttpHeaders headers = new BlobHttpHeaders
        {
            // Set the MIME ContentType every time the properties 
            // are updated or the field will be cleared
            ContentType = "text/plain",
            ContentLanguage = "en-us",

            // Populate remaining headers with 
            // the pre-existing properties
            CacheControl = properties.CacheControl,
            ContentDisposition = properties.ContentDisposition,
            ContentEncoding = properties.ContentEncoding,
            ContentHash = properties.ContentHash
        };

        // Set the blob's properties.
        await blob.SetHttpHeadersAsync(headers);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

以下代码示例获取 blob 的系统属性并显示一些值。

private static async Task GetBlobPropertiesAsync(BlobClient blob)
{
    try
    {
        // Get the blob properties
        BlobProperties properties = await blob.GetPropertiesAsync();

        // Display some of the blob's property values
        Console.WriteLine($" ContentLanguage: {properties.ContentLanguage}");
        Console.WriteLine($" ContentType: {properties.ContentType}");
        Console.WriteLine($" CreatedOn: {properties.CreatedOn}");
        Console.WriteLine($" LastModified: {properties.LastModified}");
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

设置和检索元数据

可将元数据指定为 Blob 或容器资源上的一个或多个名称/值对。 若要设置元数据,请将名称/值对添加到资源上的 Metadata 集合。 然后,调用以下方法之一来写入值:

以下代码示例在 blob 上设置元数据。 一个值是使用集合的 Add 方法设置的。 另一个值是使用隐式键/值语法设置的。

public static async Task AddBlobMetadataAsync(BlobClient blob)
{
    Console.WriteLine("Adding blob metadata...");

    try
    {
        IDictionary<string, string> metadata =
           new Dictionary<string, string>();

        // Add metadata to the dictionary by calling the Add method
        metadata.Add("docType", "textDocuments");

        // Add metadata to the dictionary by using key/value syntax
        metadata["category"] = "guidance";

        // Set the blob's metadata.
        await blob.SetMetadataAsync(metadata);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

以下代码示例在 Blob 上读取元数据。

要检索元数据,请对 blob 或容器调用 GetPropertiesGetPropertiesAsync 方法以填充 Metadata 集合,然后读取值,如下面的示例所示。 GetProperties 方法通过调用获取 Blob 属性操作和获取 Blob 元数据操作来检索 blob 属性和元数据。

public static async Task ReadBlobMetadataAsync(BlobClient blob)
{
    try
    {
        // Get the blob's properties and metadata.
        BlobProperties properties = await blob.GetPropertiesAsync();

        Console.WriteLine("Blob metadata:");

        // Enumerate the blob's metadata.
        foreach (var metadataItem in properties.Metadata)
        {
            Console.WriteLine($"\tKey: {metadataItem.Key}");
            Console.WriteLine($"\tValue: {metadataItem.Value}");
        }
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

资源

若要详细了解如何使用适用于 .NET 的 Azure Blob 存储客户端库来管理系统属性和用户定义的元数据,请参阅以下资源。

REST API 操作

Azure SDK for .NET 包含基于 Azure REST API 而生成的库,允许你通过熟悉的 .NET 范例与 REST API 操作进行交互。 用于管理系统属性和用户定义的元数据的客户端库方法使用以下 REST API 操作:

客户端库资源