You need to enable JavaScript to run this app.
导航
管理对象标签(Node.js SDK)
最近更新时间:2024.02.04 18:31:10首次发布时间:2023.12.19 18:47:29

TOS 支持为对象设置自定义标签,并根据对象标签,完成特定对象的生命周期管理。本文介绍如何通过 TOS Node.js SDK 管理对象标签。

注意事项

  • 设置对象标签,您必须具有 tos:PutObjectTagging 权限,具体操作,请参见权限配置指南
  • 获取对象标签,您必须具有 tos:GetObjectTagging 权限,具体操作,请参见权限配置指南
  • 删除对象标签,您必须具有 tos:DeleteObjectTagging 权限,具体操作,请参见权限配置指南
  • 每个对象最多添加 10 个标签,并且同一对象的 Key 不能重复,关于对象标签的限制说明,请参见对象标签限制说明

设置对象标签

// 导入 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