Delete all tags from an object using the OSS Node.js SDK. When versioning is enabled on the bucket, OSS removes tags from the current version by default. To target a specific version, pass the version ID in the request.
Prerequisites
Before you begin, make sure that you have:
Installed the
ali-osspackage (npm install ali-oss)Set the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables
Delete object tags
Method signature
client.deleteObjectTagging(objectName[, options]) → result| Parameter | Type | Required | Description |
|---|---|---|---|
objectName | string | Yes | Full path of the object, excluding the bucket name. For example: exampledir/exampleobject.txt |
options.versionId | string | No | Version ID of the object. Required when targeting a specific version. |
Example
const OSS = require('ali-oss');
const client = new OSS({
// Set region 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: '<your-region>',
// Load credentials from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'examplebucket',
});
async function deleteObjectTagging(objectName) {
try {
const result = await client.deleteObjectTagging(objectName);
console.log(result);
} catch (e) {
console.log(e);
}
}
// Specify the full path of the object, excluding the bucket name.
// For example: exampledir/exampleobject.txt
deleteObjectTagging('exampledir/exampleobject.txt');Delete tags from a specific version of an object
When versioning is enabled, pass the version ID to target a specific version.
Note
To get the version ID of an object, see List objects.
// Reuse the client initialized above.
async function deleteObjectTaggingByVersion(objectName, versionId) {
try {
const result = await client.deleteObjectTagging(objectName, { versionId });
console.log(result);
} catch (e) {
console.log(e);
}
}
deleteObjectTaggingByVersion(
'exampledir/exampleobject.txt',
'CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'
);References
Complete sample code: GitHub
API reference: DeleteObjectTagging