All Products
Search
Document Center

Object Storage Service:Manage object metadata

Last Updated:Mar 18, 2025

This topic describes how to configure and query object metadata by using Object Storage Service (OSS) SDK for Harmony.

Usage notes

  • For more information about the regions and endpoints, see Regions and endpoints.

  • To configure object metadata, you must have the oss:PutObject permission. To query object metadata, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Configure object metadata when you upload an object

The following sample code provides an example on how to set the object access control list (ACL) to public-read when you upload an object:

import Client, { EObjectAcl, RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

const bucket = 'yourBucketName'; // Specify the name of the bucket.

const key='yourObjectName '; // Specify the name of the object.

const putObject = async () => {
  try {
    // Use the putObject method to upload data to object in the specified bucket and pass parameters.
    const res = await client.putObject({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
      data: 'hello world' // Specify the data that you want to upload. In this example, a simple string is uploaded.
      objectAcl: EObjectAcl.PRIVATE
    });

    // Display the result of the object upload operation.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code);
      console.log('message: ', err.message);
      console.log('requestId: ', err.requestId);
      console.log('status: ', err.status);
      console.log('ec: ', err.ec);
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
}

// Call the putObject function to perform the object upload operation.
putObject();

Query object metadata

Query all metadata of an object by using the HeadObject method

The following sample code provides an example on how to query all metadata of an object by using the HeadObject method:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from STS.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object.
const key = 'yourObjectName';

/**
 * Query the metadata of the object. 
 * Use the HeadObject method to query the metadata of the object without downloading the object. 
 */
const headObject = async () => {
  try {
    // Use the HeadObject method to query the metadata of the object.
    const res = await client.headObject({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
    });

    // Display the object metadata.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the headObject function to query object metadata.
headObject();

Use the GetObjectMeta method to query the partial metadata of an object

Note

You can use the GetObjectMeta method to query only partial object metadata, including the returned content length (ContentLength), entity tag (ETag), last modified time (LastModified), last access time (LastAccessTime), version ID (VersionId), and CRC-64 hash (HashCRC64).

The following sample code provides an example on how to query the partial metadata of an object by using the GetObjectMeta method:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from STS.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object.
const key = 'yourObjectName';

/**
 * Query the metadata of the object. 
 * Use the getObjectMeta method to query the metadata of the object, including the HTTP headers. 
 */
const getObjectMeta = async () => {
  try {
    // Use the getObjectMeta method to query the metadata of the object.
    const res = await client.getObjectMeta({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
    });

    // Display the object metadata.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the getObjectMeta function to query object metadata.
getObjectMeta();