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

该接口用于列举桶中的多版本对象。每次请求返回桶的部分多版本对象(单次请求最多 1000 个),可以使用请求参数作为选择标准来返回桶中对象的子集。

注意事项

列举多版本对象前,您需要具有 tos:ListBucketVersions 权限,具体操作请参见 IAM 策略概述

参数说明

参数

描述

Prefix

本次查询结果的前缀。

Delimiter

对对象名称进行分组的字符。

MaxKeys

返回列举对象的最大数,默认值为 1000。
取值:大于 0 小于等于 1000。

EncodingType

对返回的内容进行编码并指定编码的类型。
默认值:None

KeyMarker

列举多版本对象的起始位置。设定从该值之后按字母排序返回对象列表。通常为上次请求返回的 NextKeyMarker 值。

VersionIdMarker

与 KeyMarker 配合使用,设定从该值之后按字典排序返回多版本对象列表。从上次列举结果中 NextVersionIDMarker 获取。

示例代码

简单列举

以下代码用于列举指定存储桶中最多10个多版本对象。

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)

   // 获取 bucket 中最多 10 个多版本对象
   output, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{
      Bucket: bucketName,
      ListObjectVersionsInput: tos.ListObjectVersionsInput{
         MaxKeys: 10,
      },
   })
   checkErr(err)
   for _, obj := range output.Versions {
      fmt.Println("Obj Key:", obj.Key)
      fmt.Println("Obj Version ID:", obj.VersionID)
      fmt.Println("Obj Last Modified:", obj.LastModified)
      fmt.Println("Obj Is Latest Version:", obj.IsLatest)
   }
   for _, deleteMarker := range output.DeleteMarkers {
      fmt.Println("Delete Maker Key:", deleteMarker.Key)
      fmt.Println("Delete Maker Version ID:", deleteMarker.VersionID)
      fmt.Println("Delete Maker Last Modified:", deleteMarker.LastModified)
      fmt.Println("Delete Maker Is Latest Version:", deleteMarker.IsLatest)
   }

}

列举指定前缀的所有对象

以下代码用于列举指定存储桶指定前缀下所有多版本对象和删除标记。

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 ***"
      prefix     = "*** Provide your object key prefix ***"

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

   // 列举指定前缀的对象
   truncated := true
   marker := ""
   versionIdMarker := ""

   for truncated {
      output, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{
         Bucket: bucketName,
         ListObjectVersionsInput: tos.ListObjectVersionsInput{
            MaxKeys:         10,
            Prefix:          prefix,
            KeyMarker:       marker,
            VersionIDMarker: versionIdMarker,
         },
      })
      checkErr(err)
      for _, obj := range output.Versions {
         fmt.Println("Obj Key:", obj.Key)
         fmt.Println("Obj Version ID:", obj.VersionID)
         fmt.Println("Obj Last Modified:", obj.LastModified)
         fmt.Println("Obj Is Latest Version:", obj.IsLatest)
      }
      for _, deleteMarker := range output.DeleteMarkers {
         fmt.Println("Delete Maker Key:", deleteMarker.Key)
         fmt.Println("Delete Maker Version ID:", deleteMarker.VersionID)
         fmt.Println("Delete Maker Last Modified:", deleteMarker.LastModified)
         fmt.Println("Delete Maker Is Latest Version:", deleteMarker.IsLatest)
      }
      truncated = output.IsTruncated
      marker = output.NextKeyMarker
      versionIdMarker = output.NextVersionIDMarker
   }

}

列举所有对象

以下代码用于列举制定桶内所有多版本对象和删除标记。

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
   marker := ""
   versionIdMarker := ""

   for truncated {
      output, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{
         Bucket: bucketName,
         ListObjectVersionsInput: tos.ListObjectVersionsInput{
            MaxKeys:         1000,
            KeyMarker:       marker,
            VersionIDMarker: versionIdMarker,
         },
      })
      checkErr(err)
      for _, obj := range output.Versions {
         fmt.Println("Obj Key:", obj.Key)
         fmt.Println("Obj Version ID:", obj.VersionID)
         fmt.Println("Obj Last Modified:", obj.LastModified)
         fmt.Println("Obj Is Latest Version:", obj.IsLatest)
      }
      for _, deleteMarker := range output.DeleteMarkers {
         fmt.Println("Delete Maker Key:", deleteMarker.Key)
         fmt.Println("Delete Maker Version ID:", deleteMarker.VersionID)
         fmt.Println("Delete Maker Last Modified:", deleteMarker.LastModified)
         fmt.Println("Delete Maker Is Latest Version:", deleteMarker.IsLatest)
      }
      truncated = output.IsTruncated
      marker = output.NextKeyMarker
      versionIdMarker = output.NextVersionIDMarker
   }

}

递归列举目录下的对象和子目录

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
   marker := ""
   versionIdMarker := ""
   delimiter := "/"

   for truncated {
      output, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{
         Bucket: bucketName,
         ListObjectVersionsInput: tos.ListObjectVersionsInput{
            MaxKeys:         1000,
            KeyMarker:       marker,
            VersionIDMarker: versionIdMarker,
            Delimiter:       delimiter,
         },
      })
      checkErr(err)

      // 返回根目录下的子目录
      for _, prefix := range output.CommonPrefixes {
         fmt.Println("Sub Dir:", prefix.Prefix)
      }
      // 列举根目录下的多版本对象
      for _, obj := range output.Versions {
         fmt.Println("Obj Key:", obj.Key)
         fmt.Println("Obj Version ID:", obj.VersionID)
         fmt.Println("Obj Last Modified:", obj.LastModified)
         fmt.Println("Obj Is Latest Version:", obj.IsLatest)
      }
      for _, deleteMarker := range output.DeleteMarkers {
         fmt.Println("Delete Maker Key:", deleteMarker.Key)
         fmt.Println("Delete Maker Version ID:", deleteMarker.VersionID)
         fmt.Println("Delete Maker Last Modified:", deleteMarker.LastModified)
         fmt.Println("Delete Maker Is Latest Version:", deleteMarker.IsLatest)
      }
      truncated = output.IsTruncated
      marker = output.NextKeyMarker
      versionIdMarker = output.NextVersionIDMarker
   }

}

相关文档

关于列举多版本对象的 API 文档,请参见 ListObjectsVersions