存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。本文介绍如何创建桶。
tos:CreateBucket
权限。您可以通过 IAM 策略授予相应权限,具体步骤,请参见 IAM 策略。// 导入 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", // 填写域名地址 }); const bucketName = 'node-sdk-test-bucket-acl'; 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 createBucketWithAcl() { try { // 创建桶 await client.createBucket({ bucket: bucketName, // 指定存储权限类型为私有 acl: 'private', }); // 列举所有桶,将列举出刚刚创建的桶 const { data } = await client.listBuckets(); // `theBucket` 即为刚刚创建的桶 const theBucket = data.Buckets.find(it => it.Name === bucketName); console.log('the bucket info', theBucket); } catch (error) { handleError(error); } } createBucketWithAcl();
// 导入 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", // 填写域名地址 }); const bucketName = 'node-sdk-test-bucket-class'; 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 createBucketWithStorageClass() { try { // 创建桶 await client.createBucket({ bucket: bucketName, // 指定存储桶存储类型为低频存储 storageClass: 'IA', }); // 列举所有桶,将列举出刚刚创建的桶 const { data } = await client.listBuckets(); // `theBucket` 即为刚刚创建的桶 const theBucket = data.Buckets.find(it => it.Name === bucketName); console.log('the bucket info', theBucket); } catch (error) { handleError(error); } } createBucketWithStorageClass();
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError, AzRedundancyType } 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", // 填写域名地址 }); const bucketName = 'node-sdk-test-bucket-az-redundancy'; 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 createBucketWithAzRedundancy() { try { // 创建桶 await client.createBucket({ bucket: bucketName, // 指定存储桶为多AZ azRedundancy: AzRedundancyType.AzRedundancyMultiAz, }); // 列举所有桶,将列举出刚刚创建的桶 const { data } = await client.listBuckets(); // `theBucket` 即为刚刚创建的桶 const theBucket = data.Buckets.find((it) => it.Name === bucketName); console.log('the bucket info', theBucket); } catch (error) { handleError(error); } } createBucketWithAzRedundancy();