TOS 支持为对象设置自定义标签,并根据对象标签,完成特定对象的生命周期管理。本文介绍如何通过 TOS Node.js SDK 管理对象标签。
tos:PutObjectTagging
权限,具体操作,请参见权限配置指南。tos:GetObjectTagging
权限,具体操作,请参见权限配置指南。tos:DeleteObjectTagging
权限,具体操作,请参见权限配置指南。// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey = '*** Provide your object key ***'; const { requestId } = await client.putObjectTagging({ bucket: bucketName, key: objectKey, tagSet: { Tags: [ { Key: 'tagA', Value: 'valueA' }, { Key: 'tagB', Value: 'valueB' }, ], }, }); console.log('putObjectTagging Request ID:', requestId); } catch (error) { handleError(error); } } main();
设置对象标签后,您可以获取对象的标签信息。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey = '*** Provide your object key ***'; const { data, requestId } = await client.getObjectTagging({ bucket: bucketName, key: objectKey, }); console.log('getObjectTagging Request ID:', requestId); console.log('getObjectTagging Tags.length:', data.TagSet.Tags.length); for (const tag of data.TagSet.Tags) { console.log('Tag Key:', tag.Key); console.log('Tag Value:', tag.Value); } } catch (error) { handleError(error); } } main();
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } async function main() { try { const bucketName = 'node-sdk-test-bucket'; // 指定的 objectKey const objectKey = '*** Provide your object key ***'; const { requestId } = await client.deleteObjectTagging({ bucket: bucketName, key: objectKey, }); console.log('deleteObjectTagging Request ID:', requestId); } catch (error) { handleError(error); } } main();
关于对象标签的更多信息,请参见 PutObjectTagging。