Set and retrieve Blob Storage properties and metadata
5 minutes
5 Questions
Azure Blob Storage allows developers to work with properties and metadata to manage and organize blobs effectively. Properties are system-defined attributes that describe the blob's characteristics, while metadata consists of user-defined name-value pairs that provide custom information about the b…Azure Blob Storage allows developers to work with properties and metadata to manage and organize blobs effectively. Properties are system-defined attributes that describe the blob's characteristics, while metadata consists of user-defined name-value pairs that provide custom information about the blob.
**Blob Properties:**
Properties include system-managed values such as ContentType, ContentLength, LastModified, ETag, and ContentEncoding. These help identify the blob's format, size, and state. You can retrieve and modify certain properties using the Azure SDK or REST API.
**Setting Properties:**
Using the Azure.Storage.Blobs SDK in .NET, you can set properties through the BlobClient class:
csharp
BlobClient blobClient = containerClient.GetBlobClient("myblob.txt");
BlobHttpHeaders headers = new BlobHttpHeaders
{
ContentType = "text/plain",
CacheControl = "max-age=3600"
};
await blobClient.SetHttpHeadersAsync(headers);
**Retrieving Properties:**
To fetch properties, use the GetPropertiesAsync method:
csharp
BlobProperties properties = await blobClient.GetPropertiesAsync();
Console.WriteLine($"Content Type: {properties.ContentType}");
Console.WriteLine($"Last Modified: {properties.LastModified}");
**Blob Metadata:**
Metadata enables you to attach custom key-value pairs to blobs for categorization, tagging, or tracking purposes.
**Setting Metadata:**
csharp
IDictionary<string, string> metadata = new Dictionary<string, string>
{
{ "author", "JohnDoe" },
{ "category", "documents" }
};
await blobClient.SetMetadataAsync(metadata);
**Retrieving Metadata:**
csharp
BlobProperties props = await blobClient.GetPropertiesAsync();
foreach (var item in props.Metadata)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
**REST API Approach:**
You can also use HTTP headers with REST calls. Properties use x-ms-blob-* headers, while metadata uses x-ms-meta-* prefixed headers.
Both properties and metadata are essential for building robust storage solutions, enabling better organization, caching strategies, and application-specific data management in Azure Blob Storage.
Set and Retrieve Blob Storage Properties and Metadata - Complete Guide
Why This Topic Is Important
Understanding how to set and retrieve Blob Storage properties and metadata is crucial for the AZ-204 exam because it demonstrates your ability to manage blob characteristics programmatically. This knowledge is essential for building applications that need to track custom information about blobs, implement content management systems, and optimize blob delivery through proper content-type settings.
What Are Blob Properties and Metadata?
System Properties: System properties are predefined attributes maintained by Azure Blob Storage. These include: - ContentType: The MIME type of the blob (e.g., image/png, application/json) - ContentLength: The size of the blob in bytes - LastModified: The date and time the blob was last modified - ETag: A unique identifier that changes when the blob content changes - ContentEncoding: The encoding of the blob content (e.g., gzip) - CacheControl: Specifies caching behavior for the blob - ContentDisposition: Controls how the blob should be presented when downloaded
User-Defined Metadata: Metadata consists of custom name-value pairs that you define to store additional information about a blob. Key characteristics: - Maximum of 8 KB total size for all metadata - Names must be valid C# identifiers - Names are case-insensitive - Values are strings
- GET with ?comp=metadata: Retrieves metadata only - PUT with ?comp=metadata: Sets metadata - HEAD request: Retrieves properties and metadata in headers - PUT with ?comp=properties: Sets system properties
Exam Tips: Answering Questions on Set and Retrieve Blob Storage Properties and Metadata
1. Know the Method Names: Remember that SetHttpHeadersAsync is used for system properties while SetMetadataAsync is used for custom metadata.
2. Understand the Difference: Properties are system-managed attributes; metadata is user-defined key-value pairs. Questions often test whether you can distinguish between them.
3. Remember Size Limits: Metadata is limited to 8 KB total. If a question involves storing large amounts of data, metadata is not the correct answer.
4. Case Sensitivity: Metadata names are case-insensitive when stored but preserve the original case. This is a common exam topic.
5. ContentType Scenarios: When questions ask about serving files correctly in browsers or ensuring proper MIME types, think about the ContentType property.
6. FetchAttributes Pattern: In older SDK versions, you must call FetchAttributesAsync before accessing properties. Know which SDK version the question references.
7. REST API Headers: Custom metadata is returned with the x-ms-meta- prefix in HTTP headers. Questions may test this knowledge.
8. Batch Operations: Setting metadata replaces all existing metadata. If a question asks about updating a single metadata value, remember you must include all existing values plus the new one.