Object Storage Service (OSS) lets you use object tagging to classify objects in buckets. You can set lifecycle rules, access permissions, and other configurations for objects that have the same tags.
Usage notes
Before you configure object tagging, make sure that you understand this feature. For more information, see Object tagging.
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.
Object tagging is supported only in OSS SDK for Java 3.5.0 and later.
To set object tags, you must have the
oss:PutObjectTaggingpermission. For more information, see Attach a custom policy to a RAM user.
Add object tags during object upload
Add object tags during a simple upload
The following code shows how to add object tags during a simple upload using the PutObject method.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. const objectName = 'exampledir/exampleobject.txt' // Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt. // If you specify only the local file name (for example, examplefile.txt) without a full path, the file is uploaded from the local path that corresponds to the project of the sample program. const localFilepath = 'D:\\localpath\\examplefile.txt' // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', } client.put(objectName, localFilepath, { headers })Add object tags during a multipart upload
The following code shows how to add object tags during a multipart upload using the multipartUpload method.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. const objectName = 'exampledir/exampleobject.txt' // Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt. // If you specify only the local file name (for example, examplefile.txt) without a full path, the file is uploaded from the local path that corresponds to the project of the sample program. const localFilepath = 'D:\\localpath\\examplefile.txt' // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', } async function setTag() { await client.multipartUpload(objectName, localFilepath, { // Set the part size in bytes. The minimum part size is 100 KB. The size of the last part is not limited. partSize: 100 * 1024, headers }); const tag = await client.getObjectTagging(objectName); console.log(tag); } setTag()Add object tags during an append upload
The following code shows how to add object tags during an append upload using the AppendObject method.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. const objectName = 'exampledir/exampleobject.txt' // Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt. // If you specify only the local file name (for example, examplefile.txt) without a full path, the file is uploaded from the local path that corresponds to the project of the sample program. const localFilepath = 'D:\\localpath\\examplefile.txt' // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', } // Append data to an object. If you specify headers in the append operation, tags are set for the object. // Only the tags set in the first append operation take effect. Tags set in subsequent append operations are ignored. async function setTag() { await client.append(objectName, localFilepath, { // Set the part size in bytes. The minimum part size is 100 KB. The size of the last part is not limited. partSize: 100 * 1024, headers }); const tag = await client.getObjectTagging(objectName); console.log(tag); } setTag()Add object tags during a resumable upload
The following code shows how to add object tags during a resumable upload using the multipartUpload method.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. const objectName = 'exampledir/exampleobject.txt' // Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt. // If you specify only the local file name (for example, examplefile.txt) without a full path, the file is uploaded from the local path that corresponds to the project of the sample program. const localFilepath = 'D:\\localpath\\examplefile.txt' // Set the breakpoint information. let checkpoint; // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', } async function setTag() { await client.multipartUpload(objectName, localFilepath, { checkponit, async progress(percentage, cpt) { checkpoint = cpt; }, headers }); const tag = await client.getObjectTagging(objectName); console.log(tag); } setTag()
Add or change object tags for an existing object
If you did not add tags to an object when you uploaded it, or if the existing tags do not meet your needs, you can add or modify the tags for the object after it is uploaded.
The following code shows how to add or change object tags for an existing object.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou).
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
const objectName = 'exampledir/exampleobject.txt'
// Specify the key (for example, owner) and value (for example, John) of the object tag.
const tag = { owner: 'John', type: 'document' };
async function putObjectTagging(objectName, tag) {
try {
const result = await client.putObjectTagging(objectName, tag);
console.log(result);
} catch (e) {
console.log(e);
}
}
putObjectTagging(objectName, tag)Add or change object tags for a specific object version
In a bucket for which versioning is enabled, you can add or modify tags for a specific version of an object by specifying the version ID of the object.
The following code shows how to add or change object tags for a specific version of an object.
For more information about how to obtain a version ID, see List objects (Node.js SDK).
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou).
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
const objectName = 'exampledir/exampleobject.txt'
// Specify the key (for example, owner) and value (for example, John) of the object tag.
const tag = { owner: 'John', type: 'document' };
// Specify the version ID of the object.
const versionId='CAEQIRiBgMDqvPqA3BciIDJhMjE4MWZkN2ViYTRmYzJhZjkxMzk2YWM2NjJk****'
async function putObjectTagging(objectName, tag) {
try {
const options = {
versionId
};
const result = await client.putObjectTagging(objectName, tag, options);
console.log(result);
} catch (e) {
console.log(e);
}
}
putObjectTagging(objectName, tag)Set object tags when you copy an object
You can use the one of following methods to configure object tagging when you copy an object:
Copy: The tag of the source object is copied to the destination object.
Replace: The destination object has the tag specified in the request instead of the tag of the source object.
The following examples describe how to add tags to an object smaller than 1 GB in simple copy mode and larger than 1 GB in multipart copy mode:
Add object tags during a simple copy
The following code shows how to set object tags when you perform a simple copy of an object that is smaller than 1 GB.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the source object. The full path cannot contain the bucket name. For example, srcexampledir/exampleobject.txt. const sourceObjectName = 'srcexampledir/exampleobject.txt'; // Specify the full path of the destination object. The full path cannot contain the bucket name. For example, destexampledir/exampleobject.txt. const targetObjectName = 'destexampledir/exampleobject.txt'; // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', // Specify how to set the tags for the destination object. Valid values: Copy and Replace. The default value is Copy. Copy indicates that the tags of the source object are copied to the destination object. Replace indicates that the tags of the source object are ignored and the tags specified in the request are used. 'x-oss-tagging-directive': 'Replace' } async function setTag() { const result = await client.copy(targetObjectName, sourceObjectName, { headers }); const tag = await client.getObjectTagging(targetObjectName) console.log(tag) } setTag()Add object tags during a multipart copy
The following code shows how to set object tags when you perform a multipart copy of an object that is larger than 1 GB.
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou). region: 'yourregion', // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the bucket name. bucket: 'yourbucketname' }); // Specify the full path of the source object. The full path cannot contain the bucket name. For example, srcexampledir/exampleobject.txt. const sourceObjectName = 'srcexampledir/exampleobject.txt' // Specify the full path of the destination object. The full path cannot contain the bucket name. For example, destexampledir/exampleobject.txt. const targetObjectName = 'destexampledir/exampleobject.txt' // Set request headers. const headers = { // Specify the key (for example, owner) and value (for example, John) of the object tag. 'x-oss-tagging': 'owner=John&type=document', } async function setTag() { await client.multipartUploadCopy(targetObjectName, { sourceKey: sourceObjectName, sourceBucketName: 'examplebucket' }, { // Set the part size in bytes. The minimum part size is 100 KB. The size of the last part is not limited. partSize: 256 * 1024, headers }); const tag = await client.getObjectTagging(targetObjectName) console.log(tag) } setTag()
Set tags for a symbolic link
The following code shows how to set tags for a symbolic link.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, set region to oss-cn-hangzhou for China (Hangzhou).
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// Specify the full path of the symbolic link. For example, shortcut/myobject.txt.
const symLink = "shortcut/myobject.txt";
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
const targetObjectName = 'exampledir/exampleobject.txt'
// Set request headers.
const headers = {
// Specify the key (for example, owner) and value (for example, John) of the object tag.
'x-oss-tagging': 'owner=John&type=document',
}
async function setTag() {
await client.putSymlink(symLink, targetObjectName, {
storageClass: 'IA',
meta: {
uid: '1',
slus: 'test.html'
},
headers
});
const tag = await client.getObjectTagging(targetObjectName)
console.log(tag)
}
setTag()References
For the complete sample code for setting object tags, see GitHub.
For more information about the API operation for setting object tags, see PutObjectTagging.