存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。本文介绍如何创建桶。
tos:CreateBucket
权限。您可以通过 IAM 策略授予相应权限,具体步骤,请参见 IAM 策略。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", // 填写域名地址 }); // 存储桶名称 const bucketName = 'harmonyos-sdk-test-bucket'; 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); } } async function createBucket() { try { // 创建桶 await client.createBucket({ Bucket: bucketName, }); // 列举所有桶,将列举出刚刚创建的桶 const res = await client.listBuckets(); // `theBucket` 即为刚刚创建的桶 const theBucket = res.Buckets.find(it => it.Name === bucketName); console.log('the bucket info', theBucket.Name); } catch (error) { handleError(error); } } createBucket();
import { TosClient, TosClientError, TosServerError, StorageClassType } 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", // 填写域名地址 }); // 存储桶名称 const bucketName = 'harmonyos-sdk-test-bucket'; 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); } } async function createBucketWithStorageClass() { try { // 创建桶 await client.createBucket({ Bucket: bucketName, // 指定存储桶存储类型为低频存储 StorageClass: StorageClassType.StorageClassIa, }); // 列举所有桶,将列举出刚刚创建的桶 const res = await client.headBucket({ Bucket: bucketName, }) console.log('the bucket storageClass is', res.StorageClass); } catch (error) { handleError(error); } } createBucketWithStorageClass();
import { TosClient, TosClientError, TosServerError, StorageClassType } 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", // 填写域名地址 }); // 存储桶名称 const bucketName = 'harmonyos-sdk-test-bucket'; 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); } } async function createBucketWithAZ() { try { // 创建桶 await client.createBucket({ Bucket: bucketName, // 指定存储桶 AZ 为多 AZ AzRedundancy: AzRedundancyType.AzRedundancyMultiAz, }); // 列举所有桶,将列举出刚刚创建的桶 const res = await client.headBucket({ Bucket: bucketName, }) console.log('the bucket az type is', res.AzRedundancy); } catch (error) { handleError(error); } } createBucketWithAZ();
关于创建桶的 API 文档,请参见 CreateBucket。