Use getObjectTagging to retrieve the tags of an object in Object Storage Service (OSS). For versioned buckets, OSS returns the tags of the current version by default. To get the tags of a specific version, pass the version ID in the options.
Prerequisites
Before you begin, ensure that you have:
Installed the
ali-ossnpm packageSet the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables
Get object tags
The following example shows how to get tags for the current version and for a specific version. Pass { versionId } in the options to target a specific version; omit it to get the current version's tags.
const OSS = require('ali-oss')
const client = new OSS({
// Set to the region where your bucket is located, for example: oss-cn-hangzhou
region: 'yourRegion',
// Load access credentials from environment variables
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name
bucket: 'examplebucket',
});
const objectName = 'exampledir/exampleobject.txt'
// Get tags for the current version
async function getTagsCurrentVersion() {
try {
const result = await client.getObjectTagging(objectName);
console.log(result);
} catch (e) {
console.log(e);
}
}
// Get tags for a specific version
async function getTagsSpecificVersion() {
try {
const options = {
versionId: 'CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'
};
const result = await client.getObjectTagging(objectName, options);
console.log(result);
} catch (e) {
console.log(e);
}
}
getTagsCurrentVersion()
// getTagsSpecificVersion()Parameters:
| Parameter | Description | Example |
|---|---|---|
objectName | Full path of the object, excluding the bucket name | exampledir/exampleobject.txt |
options.versionId | (Optional) Version ID of the object. Required when querying a specific version in a versioned bucket. To get the version ID, see List objects. | CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk**** |
What's next
For the complete sample code, see GitHub.
For the API reference, see GetObjectTagging.