You need to enable JavaScript to run this app.
导航
获取桶信息(Harmony SDK)
最近更新时间:2024.12.23 17:29:48首次发布时间:2024.12.23 17:29:48

通过 HeadBucket 获取存储桶(Bucket)信息, 同时可以根据返回的 err 中的 HTTP Code 信息,判断桶是否存在。本文介绍如何获取桶元数据和判断桶是否存在。

注意事项

  • 获取桶元数据之前,您必须具有 tos:HeadBucket 权限。
  • 若桶不存在则该接口会返回 404,也常用于判断桶是否存在。

示例代码

判断桶是否存在

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak",
  accessKeySecret: "Provide your sk",
  securityToken: 'Provide your securityToken',
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

function handleError(error: 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);
 }
}

const bucketName = 'harmonyos-sdk-test-bucket';
async function main() {
  try {
    // 判断桶是否存在
    const res = await client.headBucket({
      Bucket: bucketName
    });

    console.log(`the region of ${bucketName} is`, res.Region);
    console.log(`the storageClass of ${bucketName} is`, res.StorageClass);
  } catch (error) {
    if (error instanceof TosServerError) {
      if (error.StatusCode === 404) {
        // 存储桶不存在
        console.log('Bucket not found.');
        return;
      }
    }

    handleError(error);
  }
}

main();

获取桶元数据信息

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak",
  accessKeySecret: "Provide your sk",
  securityToken: 'Provide your securityToken',
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

function handleError(error: 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);
 }
}

const bucketName = 'harmonyos-sdk-test-bucket';
async function main() {
  try {
    // 判断桶是否存在
    const res = await client.headBucket({
      Bucket: bucketName
    });

    console.log(`the region of ${bucketName} is`, res.Region);
    console.log(`the storageClass of ${bucketName} is`, res.StorageClass);
    console.log(`the azRedundancy of ${bucketName} is`, res.AzRedundancy)
  } catch (error) {
    handleError(error);
  }
}

main();

相关文档

关于获取桶的元数据 API 文档,请参见 HeadBucket