You need to enable JavaScript to run this app.
导航
移动及重命名场景(Go SDK)
最近更新时间:2024.04.26 17:28:04首次发布时间:2023.01.18 16:55:22

如果您需要在 Bucket 中对文件进行重命名,您可以通过 CopyObject 接口将源对象拷贝至目标对象,然后通过 DeleteObject 接口删除源对象。

重命名单个对象

以下代码用于将存储桶中对象 src-object 重命名为 dest-object

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 {
         if serverErr.StatusCode == http.StatusNotFound {
            return
         }
         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)
   srcObjectKey := "src-object"
   dstObjectKey := "dest-object"
   // 将源对象复制到目标对象
   output, err := client.CopyObject(ctx, &tos.CopyObjectInput{
      Bucket:    bucketName,
      Key:       dstObjectKey,
      SrcBucket: bucketName,
      SrcKey:    srcObjectKey,
   })
   checkErr(err)
   fmt.Println("CopyObject Request ID:", output.RequestID)
   // 删除源对象
   deleteOutput, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
      Bucket: bucketName,
      Key:    srcObjectKey,
   })
   checkErr(err)
   fmt.Println("DeleteObjectV2 Request ID:", deleteOutput.RequestID)
}

重命名目录

以下代码用于将指定存储桶中目录 exampledir1/ 重命名为 exampledir2/

package main

import (
   "context"
   "fmt"
   "strings"

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

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         if serverErr.StatusCode == http.StatusNotFound {
            return
         }
         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 := ""
   oldPrefix := "exampledir1/"
   newPrefix := "exampledir2/"

   for truncated {

      output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{
         Bucket:            bucketName,
         MaxKeys:           1000,
         ContinuationToken: continuationToken,
         Prefix:            oldPrefix,
      })
      checkErr(err)
      for _, obj := range output.Contents {
         // 将源对象复制到目标对象
         newKey := strings.Replace(obj.Key, oldPrefix, newPrefix, 1)
         output, err := client.CopyObject(ctx, &tos.CopyObjectInput{
            Bucket:    bucketName,
            Key:       newKey,
            SrcBucket: bucketName,
            SrcKey:    obj.Key,
         })
         checkErr(err)
         fmt.Println("CopyObject Request ID:", output.RequestID)
         // 删除源对象
         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
   }

}