แก้ไข

แชร์ผ่าน


Manage container properties and metadata with JavaScript or 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

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, use the following method:

The following code example fetches a container's properties and writes some property values to a console window:

async function getContainerProperties(containerClient) {
  
  try {
    const containerProperties = await containerClient.getProperties();

    console.log(`Public access type: ${containerProperties.blobPublicAccess}`);
    console.log(`Lease status: ${containerProperties.leaseStatus}`);
    console.log(`Lease state: ${containerProperties.leaseState}`);
    console.log(`Has immutability policy: ${containerProperties.hasImmutabilityPolicy}`);
  } catch (err) {
    // Handle the error
  }
}

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:

The following code example sets metadata on a container:

async function setContainerMetadata(containerClient) {
  
  const metadata = {
    docType: "textDocuments",
    docCategory: "testing",
  };
  
  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.

Code samples

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:

The getProperties method retrieves container properties and metadata by calling both the Get Blob Properties operation and the Get Blob Metadata operation.

Client library resources