Setting and Retrieving Azure Blob Storage Properties and Metadata


Setting and Retrieving Azure Blob Storage Properties and Metadata

The course is part of this learning path

Setting and Retrieving Azure Blob Storage Properties and Metadata

In this short course, I’ll explain how to manage blob properties and metadata using the Azure Portal and the Azure Storage client libraries for .NET.

Learning Objectives

  • Manage blob properties and metadata using:
    • Azure Portal
    • Azure Storage client libraries for .NET

Intended Audience

  • Azure developers


  • Knowledge of Azure Blob Storage
  • Experience with programming, especially C#



Welcome to “Setting and Retrieving Azure Blob Storage Properties and Metadata”. I’m Guy Hummel. To get the most from this course, you should already have some knowledge of Azure Blob Storage. You should also have some experience with programming, especially C#.

In this short course, I’ll explain how to manage blob properties and metadata using the Azure Portal and the Azure Storage client libraries for .NET.

So what’s the difference between blob properties and metadata? Properties are automatically created by Azure, while metadata is user-defined. That is, metadata is optional, and you only create it if you need it.

Let’s have a look at some properties. I’m in the Azure Portal, and I’m looking at a blob container in a storage account. These are the blobs in the container. To see the system properties for one of them, you right-click on the blob and select “Properties”.

Some of the properties are pretty straightforward, such as Last Modified, Creation Time, and Size. You’ll notice that these properties can’t be modified, which makes sense because there’s only one true value for each of them. Some of the properties will change if you change the blob itself. For example, if you add more data to the blob, then its size will increase, or if you move the blob from the Hot tier to the Cool tier, then the access tier property will change.

You can see that there are a handful of properties that you can change, though. The first one only applies if this blob is configured to be cached in the Azure Content Delivery Network. The rest of them are things like content type and content language. Azure doesn’t necessarily know what these should be set to, so it allows you to set them. For example, you could set the content language to "en-us", which means US English. Then, you’d click “Save”.

There are actually a few more you can set using the portal even though it doesn’t look like you can. These three lease properties can be set indirectly by clicking the “Acquire lease” button. If you apply a lease to a blob, then the blob can’t be modified by anyone else until the lease has expired. It’s a good way to prevent two people or programs from making conflicting changes to a blob.

When we click “Acquire lease”, it changes the lease status to “Locked”, the lease state to “Leased”, and the lease duration to “Infinite”. The duration is infinite because we didn’t set a time when the lease should expire. Of course, we weren’t given the option to set an expiration time, so we didn’t have a choice. To set one, we’d need to use the Azure Storage client libraries. To end the lease, we can click “Break lease”. Now, these three properties have changed again.

Okay, down here we can see a Metadata section. It’s empty because, as I mentioned, metadata is optional. Suppose we wanted to set a category for each of our documents. Then we’d create a key called “category”, and we’d set the value to the appropriate category for the document, such as “finance”. Then we’d click “Save”, and our key/value pair would show up in the metadata list.

Using the Azure Portal to read and write blob properties and metadata is fine if you only need to change a few blobs, but if you need to make a lot of changes, then it would be easier to write a script. You could do that using either the Azure command-line interface or Azure PowerShell. If you have an application that needs to manage blob properties and metadata, then you can use Azure Storage client libraries. They’re available for a number of languages, but I’ll show you an example from Microsoft’s documentation that uses the .NET version.

This is an example C# method called SetBlobPropertiesAsync. It shows how to set the ContentType and ContentLanguage properties. First, it retrieves the existing properties by calling the GetPropertiesAsync method. It does this because when it creates a new BlobHttpHeaders class, it needs to set all of the properties for the blob, so it needs to know what to set the other properties to. By the way, even though it’s called BlobHttpHeaders, it just means BlobProperties.

First, it sets the ContentType to “text/plain” and the ContentLanguage to “en-us”, which is the whole purpose of this method. Then it sets the remaining properties to the values it retrieved at the beginning. If it didn’t set these properties, then their existing values would be wiped out, so it has to set them.

Now it calls the SetHttpHeadersAsync method to actually set the properties of the blob to the values in the BlobHttpHeaders class that was created above. Then there’s a catch block to say what to do if the operation fails.

Okay, now how about setting blob metadata? It’s pretty similar. Here’s an example method called AddBlobMetadataAsync. First, it creates a Dictionary object called “metadata” that you can put key/value pairs in.

Then it shows you two different ways to add a key/value pair. The first is to use the Add method and pass it the key and value. In this example, it’s setting a key called "docType" to the value "textDocuments", but you could set these to whatever you want because they’re user-defined.

The second way is to use this key/value syntax where the key, called “category” in this example, is in square brackets, and the value (“guidance”, in this example) comes after the equal sign.

When you’re done adding key/value pairs to the dictionary, you call the SetMetadataAsync method and pass it the dictionary object that contains the metadata. Finally, there’s a catch block to handle errors.

Now, I’ll show you one more code sample. This one shows you how to read metadata from a blob. Interestingly, you use the GetPropertiesAsync method, which seems weird considering we’re trying to get the metadata, not the properties. Well, this method actually gets both the properties and the metadata. We already saw how to read the properties from this in the first example, but to retrieve the metadata, you use a foreach loop to read all of the key/value pairs from the Metadata dictionary.

And that’s it for setting and retrieving properties and metadata in Azure Blob Storage. Please give this course a rating, and if you have any questions or comments, please let us know. Thanks!


About the Author
Learning Paths

Guy launched his first training website in 1995 and he's been helping people learn IT technologies ever since. He has been a sysadmin, instructor, sales engineer, IT manager, and entrepreneur. In his most recent venture, he founded and led a cloud-based training infrastructure company that provided virtual labs for some of the largest software vendors in the world. Guy’s passion is making complex technology easy to understand. His activities outside of work have included riding an elephant and skydiving (although not at the same time).