Manage blob properties and metadata with TypeScript

In addition to the data they contain, blobs support system properties and user-defined metadata. This article shows how to manage system properties and user-defined metadata with the Azure Storage client library for JavaScript.

Prerequisites

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.

Note

Blob index tags also provide the ability to store arbitrary user-defined key/value attributes alongside an Azure Blob storage resource. While similar to metadata, only blob index tags are automatically indexed and made searchable by the native blob service. Metadata cannot be indexed and queried unless you utilize a separate service such as Azure Search.

To learn more about this feature, see Manage and find data on Azure Blob storage with blob index (preview).

Set blob http headers

The following code example sets blob HTTP system properties on a blob.

To set the HTTP properties for a blob, create a BlobClient then call BlobClient.setHTTPHeaders. Review the BlobHTTPHeaders properties to know which HTTP properties you want to set. Any HTTP properties not explicitly set are cleared.

async function setHTTPHeaders(blobClient: BlobClient, headers): Promise<void> {
  /*
  headers= {
      blobContentType: 'text/plain',
      blobContentLanguage: 'en-us',
      blobContentEncoding: 'utf-8',
      // all other http properties are cleared
    }
  */

  const headerResults = await blobClient.setHTTPHeaders(headers);

  if (!headerResults.errorCode) {
    console.log(`headers set successfully ${headerResults.date}`);
  }
}

Set metadata

You can specify metadata as one or more name-value pairs on a blob or container resource. To set metadata, create a BlobClient then send a JSON object of name-value pairs with

The following code example sets metadata on a blob.

async function setBlobMetadata(
  blobClient: BlobClient,
  metadata: Metadata
): Promise<void> {
  /*
    metadata= {
        reviewedBy: 'Bob',
        releasedBy: 'Jill',
    }
*/
  const metadataResults = await blobClient.setMetadata(metadata);

  if (!metadataResults.errorCode) {
    console.log(`metadata set successfully ${metadataResults.date}`);
  }
}

To read the metadata, get the blob's properties (shown below), specifically referencing the metadata property.

Get blob properties

The following code example gets a blob's system properties, including HTTP headers and metadata, and displays those values.

async function getProperties(blobClient: BlobClient): Promise<void> {
  const propertiesResponse: BlobGetPropertiesResponse =
    await blobClient.getProperties();

  if (!propertiesResponse.errorCode) {
    console.log(blobClient.name + ' properties: ');

    for (const property in propertiesResponse) {
      switch (property) {
        // nested properties are stringified and returned as strings
        case 'metadata':
        case 'objectReplicationRules':
          console.log(
            `    ${property}: ${JSON.stringify(propertiesResponse[property])}`
          );
          break;
        default:
          console.log(`    ${property}: ${propertiesResponse[property]}`);
          break;
      }
    }
  }
}

Blob properties can include:

lastModified: Mon Mar 20 2023 11:04:17 GMT-0700 (Pacific Daylight Time)
createdOn: Mon Mar 20 2023 11:04:17 GMT-0700 (Pacific Daylight Time)
metadata: {"releasedby":"Jill","reviewedby":"Bob"}
objectReplicationPolicyId: undefined
objectReplicationRules: {}
blobType: BlockBlob
copyCompletedOn: undefined
copyStatusDescription: undefined
copyId: undefined
copyProgress: undefined
copySource: undefined
copyStatus: undefined
isIncrementalCopy: undefined
destinationSnapshot: undefined
leaseDuration: undefined
leaseState: available
leaseStatus: unlocked
contentLength: 19
contentType: text/plain
etag: "0x8DB296D85EED062"
contentMD5: undefined
isServerEncrypted: true
encryptionKeySha256: undefined
encryptionScope: undefined
accessTier: Hot
accessTierInferred: true
archiveStatus: undefined
accessTierChangedOn: undefined
versionId: undefined
isCurrentVersion: undefined
tagCount: undefined
expiresOn: undefined
isSealed: undefined
rehydratePriority: undefined
lastAccessed: undefined
immutabilityPolicyExpiresOn: undefined
immutabilityPolicyMode: undefined
legalHold: undefined
errorCode: undefined
body: true
_response: [object Object]
objectReplicationDestinationPolicyId: undefined
objectReplicationSourceProperties:

Resources

To learn more about how to manage system properties and user-defined 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 managing system properties and user-defined metadata use the following REST API operations:

Code samples

Client library resources