All Products
Search
Document Center

Object Storage Service:Get object tags (Node.js SDK)

Last Updated:Nov 29, 2025

After you configure tags for an object, you can query the tags of the object. If versioning is enabled for a bucket that stores the object whose tags you want to query, Object Storage Service (OSS) returns the tags of the current version of the object by default. To query the tags of a specified version of the object, you must specify the version ID of the object.

Get object tag information

If versioning is not enabled for the bucket that stores the object whose tags you want to query, you can query the tags of the object based on your requirements. If versioning is enabled for the bucket that stores the object whose tags you want to query, OSS returns the tags of the current version of the object by default.

The following sample code provides an example on how to query the tags of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

const OSS = require('ali-oss')

const client = new OSS({
  // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name. For example, examplebucket.
  bucket: 'examplebucket',
});

// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
constobjectName='exampledir/exampleobject.txt'

// Get the tag information of the object.
async function getObjectTagging(objectName) {
  try {
    const result = await client.getObjectTagging(objectName);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

getObjectTagging(objectName)

Get tag information for a specific version of an object

If versioning is enabled for the bucket that stores the object whose tags you want to query, you can query the tags of a specified version of the object by specifying the version ID of the object.

The following sample code provides an example on how to query the tags of a specified version of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

Note

For more information about how to obtain the version ID, see List objects.

const OSS = require('ali-oss')

const client = new OSS({
  // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name. For example, examplebucket.
  bucket: 'examplebucket',
});

// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
const objectName='exampledir/exampleobject.txt'
// Specify the version ID of the object.
const versionId='CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'

// Get the tag information of the object.
async function getObjectTagging(objectName) {
  try {
    const options = {
      versionId
    };
    const result = await client.getObjectTagging(objectName, options);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

getObjectTagging(objectName)

References

  • For the complete sample code for retrieving object tags, see GitHub.

  • For more information about the API operation for retrieving object tags, see GetObjectTagging.