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

当对象不会再使用时,您可以删除对象,降低成本开销。目前 TOS Go SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。

注意事项

  • 删除对象前,您必须具有 tos:DeleteObject 权限,具体操作,请参见权限配置指南
  • 对于开启多版本的桶,删除指定版本对象前,您必须具有 tos:DeleteObjectVersion 权限,具体操作,请参见权限配置指南

示例代码

删除指定对象

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

      // 指定的 objectKey
      objectKey = "example_dir/example.txt"
      ctx       = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 删除存储桶中指定对象
   output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
      Bucket: bucketName,
      Key:    objectKey,
   })
   checkErr(err)
   fmt.Println("DeleteObjectV2 Request ID:", output.RequestID)

}

删除指定版本的对象

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

      // 指定的 objectKey
      objectKey = "example_dir/example.txt"
      ctx       = context.Background()
      // 开启多版本后,在上传对象后会返回对象 Version ID
      versionID = "*** Provide object Version ID"
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 删除存储桶中指定对象指定版本
   output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
      Bucket:    bucketName,
      Key:       objectKey,
      VersionID: versionID,
   })
   checkErr(err)
   fmt.Println("DeleteObjectV2 Request ID:", output.RequestID)

}

批量删除对象

注意

  • 在开启版本控制的桶中,在调用DeleteMultiObjects接口来批量删除对象时,如果在Delete请求中未指定versionId,将插入删除标记。如果指定了versionId,将永久删除该对象的指定版本。
  • 批量删除对象支持的响应方式可以通过quiet进行设置:
    • quietfalse时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。
    • quiettrue时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。
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 ***"

      // 指定的 objectKey
      objectKey1 = "*** Provide Object Key ***"
      objectKey2 = "*** Provide Object Key ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   objects := make([]tos.ObjectTobeDeleted, 2)
   objects[0] = tos.ObjectTobeDeleted{Key: objectKey1}
   objects[1] = tos.ObjectTobeDeleted{Key: objectKey2}
   // 批量删除存储桶中指定对象
   output, err := client.DeleteMultiObjects(ctx, &tos.DeleteMultiObjectsInput{
      Bucket:  bucketName,
      Objects: objects,
   })
   checkErr(err)
   fmt.Println("DeleteMultiObjects Request ID:", output.RequestID)

}

批量删除指定版本的对象

以下代码用于删除指定存储桶中指定对象名与版本号的多个对象数据。

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

      // 指定的 objectKey
      objectKey1 = "*** Provide Object Key ***"
      // objectKey 所对应的 object 版本号
      versionID1 = "*** Provide objectKey1 Version ID ***"
      objectKey2 = "*** Provide Object Key ***"
      versionID2 = "*** Provide objectKey2 Version ID ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   objects := make([]tos.ObjectTobeDeleted, 2)
   objects[0] = tos.ObjectTobeDeleted{Key: objectKey1, VersionID: versionID1}
   objects[1] = tos.ObjectTobeDeleted{Key: objectKey2, VersionID: versionID2}
   // 批量删除存储桶中指定对象指定版本
   output, err := client.DeleteMultiObjects(ctx, &tos.DeleteMultiObjectsInput{
      Bucket:  bucketName,
      Objects: objects,
   })
   checkErr(err)
   fmt.Println("DeleteMultiObjects Request ID:", output.RequestID)

}

删除桶中所有对象

以下代码展示删除指定 Bucket 中所有对象。

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)
   // 删除桶中所有对象
   keyMarker := ""
   isTruncated := true
   for isTruncated {
      listOutput, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{
         Bucket: bucketName,
         ListObjectVersionsInput: tos.ListObjectVersionsInput{
            KeyMarker: keyMarker,
         },
      })
      checkErr(err)
      for _, obj := range listOutput.Versions {
         // 删除多版本对象
         output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
            Bucket:    bucketName,
            Key:       obj.Key,
            VersionID: obj.VersionID,
         })
         checkErr(err)
         fmt.Printf("Delete %s, Request ID: %s\n", obj.Key, output.RequestID)

      }
      // 删除标记信息
      for _, deleteMarker := range listOutput.DeleteMarkers {
         output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{
            Bucket:    bucketName,
            Key:       deleteMarker.Key,
            VersionID: deleteMarker.VersionID,
         })
         checkErr(err)
         fmt.Printf("Delete deleteMarker %s, Request ID: %s\n", deleteMarker.Key, output.RequestID)
      }

      isTruncated = listOutput.IsTruncated
      keyMarker = listOutput.NextKeyMarker
   }

}

相关文档

关于删除对象的 API 文档,请参见 DeleteObject