Azure Blob containers and metadata properties

Azure Blob containers and metadata properties

Azure blob containers has some system properties and some user defined metadata. This article will show you how to manage the metadata properties using  the Azure Storage client library for .NET.

Properties and Metadata

System properties exists on each blob storage resource, some of them are readonly while the others can be set.

The metadata is a set of key-value pairs which you specify for a blob storage resource. As the name suggests, the metadata is just for your purpose. It does not affect the behavior of the resource.

The metadata key-value pairs are like HTTP headers, so all rules of HTTP headers name value pair apply here. The keys are case insensitive and only support ASCII character. All non-ASCII characters should be base-64 encoded.

Editing Metadata using Azure Portal

You can also use Azure portal to update the metadata properties.

Login to Azure Portal and navigate to storage account. Go to blob containers under the blob service in the left side navigation as shown in below snapshot.

The panel will show list of all available containers. If you go to right side three dots menu, you can find the Edit metadata option.

Once you click on the Edit metadata, a panel will open on right side. It will show a key value pair interface where you can add new pairs.

You can also set the metadata on the blob data objects. If you open the storage container in portal and then click on the data object, then you can add new key-value pairs as shown in below snapshot.

You can also use storage explorer to set metadata as shown in below snapshot.

Using .NET SDK

If you are using Azure.Storage.Blobs nuget package, then below code shows how to set metadata and how to get properties.

string connectionString = "{connectionString}";
string containerName = "samplecontainer";
// BlobServiceClient which will be used to create a container client
var blobServiceClient = new BlobServiceClient(connectionString);
// Create the container and return a container client 
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

// Get blob data objects
var blobClient = containerClient.GetBlobClient("Docker_AKS_2.png");

// Set metadata
Dictionary<string, string> metadataProperties = new Dictionary<string, string>();
metadataProperties.Add("somekey", "somevalue");

// Get system properties
var propertiesResponse = blobClient.GetProperties();

If you are using the Azure Storage client library for .NET, then you can use below code to set the properties.

// You must explicitly set the MIME ContentType every time
// the properties are updated or the field will be cleared.
blob.Properties.ContentType = "text/plain";
blob.Properties.ContentLanguage = "en-us";

// Set the blob's properties.
await blob.SetPropertiesAsync();

Below code snippet shows how can you get the properties using Fetch call.

// Fetch the blob properties.
await blob.FetchAttributesAsync();

// Display some of the blob's property values.
Console.WriteLine(" ContentLanguage: {0}", blob.Properties.ContentLanguage);
Console.WriteLine(" ContentType: {0}", blob.Properties.ContentType);

Below code snippet shows how to set the user defined metadata to the blobs.

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

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

// Set the blob's metadata.
await blob.SetMetadataAsync();

// To fetch the metadata
await blob.FetchAttributesAsync();

// Showing metadata 
foreach (var metadataItem in blob.Metadata)
    Console.WriteLine("\tKey: {0}", metadataItem.Key);
    Console.WriteLine("\tValue: {0}", metadataItem.Value);

I hope this article helps you to know more about Azure blob storage. Let me know your thoughts.

Leave a Reply