Export (0) Print
Expand All

Set and Retrieve Properties and Metadata

Updated: November 12, 2014

Containers and blobs support two forms of associated data, in addition to the data they contain:

  • System properties. System properties exist on each container or blob 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, which the Azure Managed Library maintains for you.

  • User-defined metadata. User-defined metadata is metadata that you specify on a given resource, in the form of a name-value pair. You can use metadata to store additional values with a container or blob; these values are for your own purposes only and do not affect how the container or blob behaves.

ImportantImportant
Retrieving property and metadata values for a resource is a two-step process. Before you can read these values, you must explicitly fetch them on your CloudBlobContainer, CloudBlockBlob, or CloudPageBlob objects. To fetch properties and metadata synchronously, call FetchAttributes on the container or blob; to fetch them asynchronously, call BeginFetchAttributes and EndFetchAttributes.

A container has only read-only properties, while a blob has both read-only and read-write properties. To set properties on a blob, specify the property value, then call the SetProperties method or SetProperties method.

To read properties on a container or blob, call the FetchAttributes method, then retrieve the property value.

The following code example creates a container and a blob and writes property values to a console window. This example uses the storage emulator, so the emulated storage service must be running in order for the code to work:


// Use the storage emulator account.
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

// As an alternative, you can create the credentials from the account name and key.
// string accountName = "myaccount";
// string accountKey = "SzlFqgzqhfkj594cFoveYqCuvo8v9EESAnOLcTBeBIo31p16rJJRZx/5vU/oY3ZsK/VdFNaVpm6G8YSD2K48Nw==";
// StorageCredentials credentials = new StorageCredentials(accountName, accountKey);
// CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it does not already exist.
container.CreateIfNotExists();

// Fetch container properties and write out their values.
container.FetchAttributes();
Console.WriteLine("Properties for container " + container.Uri + ":");
Console.WriteLine("LastModifiedUTC: " + container.Properties.LastModified);
Console.WriteLine("ETag: " + container.Properties.ETag);
Console.WriteLine();

// Create a blob.
CloudBlockBlob blob = container.GetBlockBlobReference("myblob.txt");

// Create or overwrite the "myblob.txt" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"c:\test\myblob.txt"))
{
   blob.UploadFromStream(fileStream);
} 

// Fetch container properties and write out their values.
container.FetchAttributes();
Console.WriteLine("Properties for container " + container.Uri + ":");
Console.WriteLine("LastModifiedUTC: " + container.Properties.LastModified);
Console.WriteLine("ETag: " + container.Properties.ETag);
Console.WriteLine();

// Create a blob.
Uri blobUri =new UriBuilder(containerUri.AbsoluteUri + "/ablob.txt").Uri;
CloudPageBlob blob = new CloudPageBlob(blobUri, credentials);
blob.Create(1024);
            

// Set the CacheControl property.
blob.Properties.CacheControl = "public, max-age=31536000";
blob.SetProperties();

// Fetch blob attributes.
blob.FetchAttributes();

Console.WriteLine("Read-only properties for blob " + blob.Uri + ":");
Console.WriteLine("BlobType: " + blob.Properties.BlobType);
Console.WriteLine("ETag: " + blob.Properties.ETag);
Console.WriteLine("LastModifiedUtc: " + blob.Properties.LastModified);
Console.WriteLine("Length: " + blob.Properties.Length);
Console.WriteLine();

Console.WriteLine("Read-write properties for blob " + blob.Uri + ":");
Console.WriteLine("CacheControl: " +
   (blob.Properties.CacheControl == null ? "Not set" : blob.Properties.CacheControl));
Console.WriteLine("ContentEncoding: " +
   (blob.Properties.ContentEncoding == null ? "Not set" : blob.Properties.ContentEncoding));
Console.WriteLine("ContentLanguage: " +
   (blob.Properties.ContentLanguage == null ? "Not set" : blob.Properties.ContentLanguage));
Console.WriteLine("ContentMD5: " +
   (blob.Properties.ContentMD5 == null ? "Not set" : blob.Properties.ContentMD5));
Console.WriteLine("ContentType: " +
   (blob.Properties.ContentType == null ? "Not set" : blob.Properties.ContentType));

// Clean up.
blob.DeleteIfExists();
container.Delete();

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 the SetMetadata method to save the values to the service.

noteNote
The name of your metadata must conform to the naming conventions for C# identifiers.

To retrieve metadata, call the FetchAttributes method on your blob or container to populate the Metadata collection, then read the values.

The following code example creates a new container and sets metadata for it, then reads the metadata values back out:



// Account name and key.  Modify for your account.
string accountName = "myaccount";
string accountKey = "SzlFqgzqhfkj594cFoveYqCuvo8v9EESAnOLcTBeBIo31p16rJJRZx/5vU/oY3ZsK/VdFNaVpm6G8YSD2K48Nw==";

// Get a reference to the storage account and client with authentication credentials.
StorageCredentials credentials = new StorageCredentials(accountName, accountKey);
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it does not already exist.
container.CreateIfNotExists();

// Set metadata for the container.
container.Metadata["category"] = "images";
container.Metadata["owner"] = "azure";
container.SetMetadata();

// Get container metadata.
container.FetchAttributes();
foreach (string key in container.Metadata.Keys)
{
   Console.WriteLine("Metadata key: " + key);
   Console.WriteLine("Metadata value: " + container.Metadata[key]);
}

//Clean up.
container.Delete();

See Also

Show:
© 2014 Microsoft