You need to enable JavaScript to run this app.
导航
列举未合并的对象(列举分片上传任务)-Go SDK
最近更新时间:2024.02.04 18:31:01首次发布时间:2023.01.18 16:55:22

该接口用于列举桶中的未合并的对象,即列举分片上传任务。

注意事项

列举分片上传任务前,您需要拥有 tos:ListBucketMultipartUploads 权限,具体操作,请参见 IAM 策略概述

参数说明

参数

描述

Delimiter

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

EncodingType

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

MaxUploads

返回分片上传任务的最大数量。默认值为 1000。
取值:大于 0 小于等于 1000。

Prefix

用于指定列举返回对象的前缀名称。

KeyMarker

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

UploadIDMarker

与 keyMarker 配合使用,设定从该值之后按字典排序返回分片上传任务列表。从上次列举结果中 NextUploadIDMarker 获取。

示例代码

简单列举

以下代码用于列举指定存储桶中最多 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.ListMultipartUploadsV2(ctx, &tos.ListMultipartUploadsV2Input{
      Bucket:     bucketName,
      MaxUploads: 10,
   })
   checkErr(err)
   for _, upload := range output.Uploads {
      fmt.Println("Object Key:", upload.Key)
      fmt.Println("UploadID:", upload.UploadID)
      fmt.Println("Owner:", upload.Owner)
      fmt.Println("StorageClass:", upload.StorageClass)
   }

}

列举指定前缀的所有未合并的分片任务

以下代码用于列举指定存储桶指定前缀下的所有未合并的分片上传任务。

package main

import (
    "context"
    "fmt"
    "os"

    "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 := ""
    uploadIdMarker := ""
    for truncated {

       output, err := client.ListMultipartUploadsV2(ctx, &tos.ListMultipartUploadsV2Input{
          Bucket:         bucketName,
          MaxUploads:     1000,
          KeyMarker:      marker,
          Prefix:         prefix,
          UploadIDMarker: uploadIdMarker,
       })
       checkErr(err)
       for _, upload := range output.Uploads {
          fmt.Println("Object Key:", upload.Key)
          fmt.Println("UploadID:", upload.UploadID)
          fmt.Println("Owner:", upload.Owner)
          fmt.Println("StorageClass:", upload.StorageClass)
       }
       truncated = output.IsTruncated
       marker = output.NextKeyMarker
       uploadIdMarker = output.NextUploadIDMarker
    }
}

列举所有分片上传任务

以下代码用于列举制定存储桶未合并的所有分片上传任务。

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 {
          fmt.Println("Error:", err)
       }
       panic(err)
    }
}

func main() {
    var (
       accessKey = "*** Provide your access key ***"
       secretKey = "*** Provide your 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 := ""
    uploadIdMarker := ""
    for truncated {

       output, err := client.ListMultipartUploadsV2(ctx, &tos.ListMultipartUploadsV2Input{
          Bucket:         bucketName,
          MaxUploads:     1000,
          KeyMarker:      marker,
          UploadIDMarker: uploadIdMarker,
       })
       checkErr(err)
       for _, upload := range output.Uploads {
          fmt.Println("Object Key:", upload.Key)
          fmt.Println("UploadID:", upload.UploadID)
          fmt.Println("Owner:", upload.Owner)
          fmt.Println("StorageClass:", upload.StorageClass)
       }
       truncated = output.IsTruncated
       marker = output.NextKeyMarker
       uploadIdMarker = output.UploadIDMarker
    }
}

相关文档

关于列举对象分片的 API 文档,请参见 ListMultipartUploads