You need to enable JavaScript to run this app.
导航
创建桶(Go SDK)
最近更新时间:2024.02.04 18:30:55首次发布时间:2021.12.31 17:38:36

存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。本文介绍如何创建桶。

注意事项

  • 存储桶命名规范如下:
    • 只能包括小写字母 a~z、数字和短横线(-)。
    • 开头和结尾只能是数字或字母。
    • 长度必须在 3~63 个字符之间。
  • 通过与 TOS 同地域的其他火山引擎产品访问 TOS 时,推荐您使用内网 Endpoint。关于地域和 Endpoint 的更多信息,请参见地域和访问域名
  • 创建存储桶前,您需要确保账号拥有 tos:CreateBucket 权限。您可以通过 IAM 策略授予相应权限,具体步骤,请参见 IAM 策略

示例代码

创建存储桶时并创建时指定 ACL

package main

import (
   "context"
   "fmt"
   "os"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
   "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else if clientErr, ok := err.(*tos.TosClientError); ok {
         fmt.Println("Error:", clientErr.Error())
         fmt.Println("Client Cause Err:", clientErr.Cause.Error())
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 指定存储权限类型为私有
   acl := enum.ACLPrivate
   // 创建存储桶
   resp, err := client.CreateBucketV2(context.Background(), &tos.CreateBucketV2Input{
      Bucket:       bucketName,
      ACL:          acl,
   })
   checkErr(err)
   fmt.Println("create bucket request id: ", resp.RequestID)
   fmt.Println("create bucket response status code: ", resp.StatusCode)
}

创建时指定存储类型

package main

import (
   "context"
   "fmt"
   "os"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
   "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else if clientErr, ok := err.(*tos.TosClientError); ok {
         fmt.Println("Error:", clientErr.Error())
         fmt.Println("Client Cause Err:", clientErr.Cause.Error())
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 指定存储桶存储类型为低频存储
   storageClass := enum.StorageClassIa
   // 创建存储桶
   resp, err := client.CreateBucketV2(context.Background(), &tos.CreateBucketV2Input{
      Bucket:       bucketName,
      StorageClass: storageClass,
   })
   checkErr(err)
   fmt.Println("create bucket request id: ", resp.RequestID)
   fmt.Println("create bucket response status code: ", resp.StatusCode)
}

创建时指定 AZ 类型

package main

import (
   "context"
   "fmt"
   "os"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
   "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else if clientErr, ok := err.(*tos.TosClientError); ok {
         fmt.Println("Error:", clientErr.Error())
         fmt.Println("Client Cause Err:", clientErr.Cause.Error())
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 指定存储桶为多AZ
   azRedundancy := enum.AzRedundancyMultiAz
   // 创建存储桶
   resp, err := client.CreateBucketV2(context.Background(), &tos.CreateBucketV2Input{
      Bucket:       bucketName,
      AzRedundancy: azRedundancy,
   })
   checkErr(err)
   fmt.Println("create bucket request id: ", resp.RequestID)
   fmt.Println("create bucket response status code: ", resp.StatusCode)
}

相关文档

关于创建桶的 API 文档,请参见 CreateBucket