You need to enable JavaScript to run this app.
导航
目录场景(Go SDK)
最近更新时间:2024.02.04 18:31:02首次发布时间:2023.01.18 16:55:22

TOS 中只有对象的概念,内部使用扁平结构存储数据,为方便您对对象进行分组并简化权限管理,您可以使用目录层次来组织对象。

创建目录

package main

import (
   "bytes"
   "context"
   "fmt"

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

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 ***"

      ctx = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)

   // 创建目录
   key := "exampledir/"
   output, err := client.PutObjectV2(ctx, &tos.PutObjectV2Input{
      PutObjectBasicInput: tos.PutObjectBasicInput{Bucket: bucketName, Key: key},
      Content:             bytes.NewReader([]byte("")),
   })
   checkErr(err)
   fmt.Println("PutObjectV2 Request ID:", output.RequestID)
   // 创建多级目录
   key = "exampledir/test/"
   output, err = client.PutObjectV2(ctx, &tos.PutObjectV2Input{
      PutObjectBasicInput: tos.PutObjectBasicInput{Bucket: bucketName, Key: key},
      Content:             bytes.NewReader([]byte("")),
   })
   checkErr(err)
   fmt.Println("PutObjectV2 Request ID:", output.RequestID)
}

模拟目录查询

TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 / 结尾的对象, 模拟目录的功能。
通过 DelimiterPrefix 两个参数可以模拟目录的功能:

  • 首先设置 Delimiter/ 同时设置 Prefix 为空, 可返回根目录下的对象和子目录信息。
  • 在设置 Delimiter/ 同时设置 Prefix 为子目录(subfiledir), 可返回子目录的对象和次级目录。

以下代码用于列举根目录下的对象和子目录。

package main

import (
   "context"
   "fmt"

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

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 ***"

      ctx = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 列举目录下的文件和子目录
   truncated := true
   continuationToken := ""
   for truncated {
      output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{
         Bucket:            bucketName,
         MaxKeys:           1000,
         ContinuationToken: continuationToken,
         Delimiter:         "/",
      })
      checkErr(err)
      // 返回根目录下的子目录
      for _, prefix := range output.CommonPrefixes {
         fmt.Println("Sub Dir:", prefix.Prefix)
      }
      // Contents 中返回根目录下的对象
      for _, obj := range output.Contents {
         fmt.Println("Obj:", obj.Key)
      }
      truncated = output.IsTruncated
      continuationToken = output.NextContinuationToken
   }

}

删除目录下的文件

以下代码用于删除存储桶中 exampledir/ 目录下的所有文件。

package main

import (
   "context"
   "fmt"

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

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 ***"

      ctx = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 列举指定前缀下的所有对象
   truncated := true
   continuationToken := ""
   prefix := "exampledir/"

   for truncated {
      output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{
         Bucket:            bucketName,
         MaxKeys:           1000,
         ContinuationToken: continuationToken,
         Prefix:            prefix,
      })
      checkErr(err)
      for _, obj := range output.Contents {
         deleteOutput, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
            Bucket: bucketName,
            Key:    obj.Key,
         })
         checkErr(err)
         fmt.Println("DeleteObjectV2 Request ID:", deleteOutput.RequestID)
      }
      truncated = output.IsTruncated
      continuationToken = output.NextContinuationToken
   }

}