All Products
Search
Document Center

Object Storage Service:Query object tags

Last Updated:Oct 17, 2023

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.

Query the tags of an object

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({
  // 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: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
constobjectName='exampledir/exampleobject.txt'

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

getObjectTagging(objectName)

Query the tags of a specified 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 IDs of an object, see List objects.
const OSS = require('ali-oss')

const client = new OSS({
  // 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: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
const objectName='exampledir/exampleobject.txt'
// Specify the version ID of the object. 
const versionId='CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'

// Query the tags 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 that is used to query the tags of an object, visit GitHub.

  • For more information about the API operation that you can call to query the tags of an object, see GetObjectTagging.