|
| 1 | +--- |
| 2 | +title: Manage properties and metadata for a blob with .NET - Azure Storage |
| 3 | +description: Learn how to set and retrieve system properties and store custom metadata on blobs in your Azure Storage account using the .NET client library. |
| 4 | +services: storage |
| 5 | +author: mhopkins-msft |
| 6 | + |
| 7 | +ms.author: mhopkins |
| 8 | +ms.date: 08/09/2019 |
| 9 | +ms.service: storage |
| 10 | +ms.subservice: blobs |
| 11 | +ms.topic: conceptual |
| 12 | +--- |
| 13 | + |
| 14 | +# Manage blob properties and metadata with .NET |
| 15 | + |
| 16 | +In addition to the data they contain, blobs support system properties and user-defined metadata. This article shows how to manage system properties and user-defined metadata with the [Azure Storage client library for .NET](/dotnet/api/overview/azure/storage/client). |
| 17 | + |
| 18 | +## About properties and metadata |
| 19 | + |
| 20 | +- **System properties**: System properties exist on each Blob storage resource. Some of them can be read or set, while others are read-only. Under the covers, some system properties correspond to certain standard HTTP headers. The Azure Storage client library for .NET maintains these properties for you. |
| 21 | + |
| 22 | +- **User-defined metadata**: User-defined metadata consists of one or more name-value pairs that you specify for a Blob storage resource. You can use metadata to store additional values with the resource. Metadata values are for your own purposes only, and don't affect how the resource behaves. |
| 23 | + |
| 24 | +Retrieving metadata and property values for a Blob storage resource is a two-step process. Before you can read these values, you must explicitly fetch them by calling the `FetchAttributes` or `FetchAttributesAsync` method. The exception to this rule is that the `Exists` and `ExistsAsync` methods call the appropriate `FetchAttributes` method under the covers. When you call one of these methods, you don't need to also call `FetchAttributes`. |
| 25 | + |
| 26 | +> [!IMPORTANT] |
| 27 | +> If you find that property or metadata values for a storage resource have not been populated, check that your code calls the `FetchAttributes` or `FetchAttributesAsync` method. |
| 28 | +
|
| 29 | +## Set and retrieve properties |
| 30 | + |
| 31 | +The following code example sets the `ContentType` and `ContentLanguage` system properties on a blob. |
| 32 | + |
| 33 | +```csharp |
| 34 | +public static async Task SetBlobPropertiesAsync(CloudBlob blob) |
| 35 | +{ |
| 36 | + try |
| 37 | + { |
| 38 | + Console.WriteLine("Setting blob properties."); |
| 39 | + |
| 40 | + // You must explicitly set the MIME ContentType every time |
| 41 | + // the properties are updated or the field will be cleared. |
| 42 | + blob.Properties.ContentType = "text/plain"; |
| 43 | + blob.Properties.ContentLanguage = "en-us"; |
| 44 | + |
| 45 | + // Set the blob's properties. |
| 46 | + await blob.SetPropertiesAsync(); |
| 47 | + } |
| 48 | + catch (StorageException e) |
| 49 | + { |
| 50 | + Console.WriteLine("HTTP error code {0}: {1}", |
| 51 | + e.RequestInformation.HttpStatusCode, |
| 52 | + e.RequestInformation.ErrorCode); |
| 53 | + Console.WriteLine(e.Message); |
| 54 | + Console.ReadLine(); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +To retrieve blob properties, call the `FetchAttributes` or `FetchAttributesAsync` method on your blob to populate the `Properties` property. The following code example gets a blob's system properties and displays some of the values: |
| 60 | + |
| 61 | +```csharp |
| 62 | +private static async Task GetBlobPropertiesAsync(CloudBlob blob) |
| 63 | +{ |
| 64 | + try |
| 65 | + { |
| 66 | + // Fetch the blob properties. |
| 67 | + await blob.FetchAttributesAsync(); |
| 68 | + |
| 69 | + // Display some of the blob's property values. |
| 70 | + Console.WriteLine(" ContentLanguage: {0}", blob.Properties.ContentLanguage); |
| 71 | + Console.WriteLine(" ContentType: {0}", blob.Properties.ContentType); |
| 72 | + Console.WriteLine(" Created: {0}", blob.Properties.Created); |
| 73 | + Console.WriteLine(" LastModified: {0}", blob.Properties.LastModified); |
| 74 | + } |
| 75 | + catch (StorageException e) |
| 76 | + { |
| 77 | + Console.WriteLine("HTTP error code {0}: {1}", |
| 78 | + e.RequestInformation.HttpStatusCode, |
| 79 | + e.RequestInformation.ErrorCode); |
| 80 | + Console.WriteLine(e.Message); |
| 81 | + Console.ReadLine(); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Set and retrieve metadata |
| 87 | + |
| 88 | +You can specify metadata as one or more name-value pairs on a blob or container resource. To set metadata, add name-value pairs to the `Metadata` collection on the resource. Then, call one of the following methods to write the values: |
| 89 | + |
| 90 | +- [SetMetadata](/dotnet/api/microsoft.azure.storage.blob.cloudblob.setmetadata) |
| 91 | +- [SetMetadataAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblob.setmetadataasync) |
| 92 | + |
| 93 | +Metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. Metadata names must be valid HTTP header names and valid C# identifiers, may contain only ASCII characters, and should be treated as case-insensitive. [Base64-encode](https://docs.microsoft.com/dotnet/api/system.convert.tobase64string) or [URL-encode](https://docs.microsoft.com/dotnet/api/system.web.httputility.urlencode) metadata values containing non-ASCII characters. |
| 94 | + |
| 95 | +The name of your metadata must conform to the naming conventions for C# identifiers. Metadata names maintain the case used when they were created, but are case-insensitive when set or read. If two or more metadata headers using the same name are submitted for a resource, Azure Blob storage returns HTTP error code 400 (Bad Request). |
| 96 | + |
| 97 | +The following code example sets metadata on a blob. One value is set using the collection's `Add` method. The other value is set using implicit key/value syntax. |
| 98 | + |
| 99 | +```csharp |
| 100 | +public static async Task AddBlobMetadataAsync(CloudBlob blob) |
| 101 | +{ |
| 102 | + try |
| 103 | + { |
| 104 | + // Add metadata to the blob by calling the Add method. |
| 105 | + blob.Metadata.Add("docType", "textDocuments"); |
| 106 | + |
| 107 | + // Add metadata to the blob by using key/value syntax. |
| 108 | + blob.Metadata["category"] = "guidance"; |
| 109 | + |
| 110 | + // Set the blob's metadata. |
| 111 | + await blob.SetMetadataAsync(); |
| 112 | + } |
| 113 | + catch (StorageException e) |
| 114 | + { |
| 115 | + Console.WriteLine("HTTP error code {0}: {1}", |
| 116 | + e.RequestInformation.HttpStatusCode, |
| 117 | + e.RequestInformation.ErrorCode); |
| 118 | + Console.WriteLine(e.Message); |
| 119 | + Console.ReadLine(); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +To retrieve metadata, call the `FetchAttributes` or `FetchAttributesAsync` method on your blob or container to populate the `Metadata` collection, then read the values, as shown in the example below. |
| 125 | + |
| 126 | +```csharp |
| 127 | +public static async Task ReadBlobMetadataAsync(CloudBlob blob) |
| 128 | +{ |
| 129 | + try |
| 130 | + { |
| 131 | + // Fetch blob attributes in order to populate |
| 132 | + // the blob's properties and metadata. |
| 133 | + await blob.FetchAttributesAsync(); |
| 134 | + |
| 135 | + Console.WriteLine("Blob metadata:"); |
| 136 | + |
| 137 | + // Enumerate the blob's metadata. |
| 138 | + foreach (var metadataItem in blob.Metadata) |
| 139 | + { |
| 140 | + Console.WriteLine("\tKey: {0}", metadataItem.Key); |
| 141 | + Console.WriteLine("\tValue: {0}", metadataItem.Value); |
| 142 | + } |
| 143 | + } |
| 144 | + catch (StorageException e) |
| 145 | + { |
| 146 | + Console.WriteLine("HTTP error code {0}: {1}", |
| 147 | + e.RequestInformation.HttpStatusCode, |
| 148 | + e.RequestInformation.ErrorCode); |
| 149 | + Console.WriteLine(e.Message); |
| 150 | + Console.ReadLine(); |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +[!INCLUDE [storage-blob-dotnet-resources-include](../../../includes/storage-blob-dotnet-resources-include.md)] |
| 156 | + |
| 157 | +## See also |
| 158 | + |
| 159 | +- [Set Blob Properties operation](/rest/api/storageservices/set-blob-properties) |
| 160 | +- [Get Blob Properties operation](/rest/api/storageservices/get-blob-properties) |
| 161 | +- [Set Blob Metadata operation](/rest/api/storageservices/set-blob-metadata) |
| 162 | +- [Get Blob Metadata operation](/rest/api/storageservices/set-blob-metadata) |
0 commit comments