Manage container properties and metadata with TypeScript
Blob containers support system properties and user-defined metadata, in addition to the data they contain. This article shows how to manage system properties and user-defined metadata with the Azure Storage client library for JavaScript.
Prerequisites
- The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and TypeScript.
- The authorization mechanism must have permissions to work with container properties or metadata. To learn more, see the authorization guidance for the following REST API operations:
About properties and metadata
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 JavaScript maintains these properties for you.
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.
Metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. For more information about metadata naming requirements, see Metadata names.
Retrieve container properties
To retrieve container properties, create a ContainerClient object then use the following method:
- ContainerClient.getProperties (returns ContainerProperties)
The following code example fetches a container's properties and writes the property values to a console window:
async function getContainerProperties(
containerClient: ContainerClient
): Promise<void> {
const properties: ContainerGetPropertiesResponse =
await containerClient.getProperties();
if (properties.errorCode) throw Error(properties.errorCode);
console.log(containerClient.containerName + ' properties: ');
for (const property in properties) {
switch (property) {
// nested properties are stringified
case 'metadata':
//case 'objectReplicationRules':
console.log(` ${property}: ${JSON.stringify(properties[property])}`);
break;
default:
console.log(` ${property}: ${properties[property]}`);
break;
}
}
}
/* Example output:
{
"metadata": {
"lastfilereview": "3/20/2023",
"reviewer": "johnh"
},
"etag": "\"0x8DB295348CDCD54\"",
"lastModified": "2023-03-20T14:56:28.000Z",
"leaseState": "available",
"leaseStatus": "unlocked",
"clientRequestId": "0bc8c31a-c607-477e-9846-f2121b10297a",
"requestId": "1e4ee737-b01e-0042-4e3c-5b2207000000",
"version": "2021-12-02",
"date": "2023-03-20T14:56:28.000Z",
"blobPublicAccess": "container",
"hasImmutabilityPolicy": false,
"hasLegalHold": false,
"defaultEncryptionScope": "$account-encryption-key",
"denyEncryptionScopeOverride": false,
"isImmutableStorageWithVersioningEnabled": false
}
*/
Set and retrieve metadata
You can specify metadata as one or more name-value pairs container resource. To set metadata, create a ContainerClient object then use the following method:
- ContainerClient.setMetadata
The following code example sets metadata on a container.
/*
const metadata = {
// values must be strings
lastFileReview: currentDate.toString(),
reviewer: `johnh`
}
*/
async function setContainerMetadata(
containerClient: ContainerClient,
metadata: Metadata
) {
await containerClient.setMetadata(metadata);
}
To retrieve metadata, get the container properties then use the returned metadata property.
Resources
To learn more about setting and retrieving container properties and metadata using the Azure Blob Storage client library for JavaScript, see the following resources.
REST API operations
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for setting and retrieving properties and metadata use the following REST API operations:
- Get Container Properties (REST API)
- Set Container Metadata (REST API)
- Get Container Metadata (REST API)
The getProperties
method retrieves container properties and metadata by calling both the Get Blob Properties operation and the Get Blob Metadata operation.