You need to enable JavaScript to run this app.
导航

列举未合并的对象(列举分片上传任务)- Python SDK

最近更新时间2024.02.04 18:31:03

首次发布时间2023.01.19 18:21:16

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

注意事项

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

参数说明

参数

描述

prefix

本次查询结果的前缀。

delimiter

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

max_uploads

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

key_marker

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

示例代码

简单列举未合并分片任务

以下代码用于列举指定桶 bucket-test 中最多 10 个未合并的分片上传任务。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    out = client.list_multipart_uploads(bucket_name, max_uploads=10)
    for upload in out.uploads:
        print('object key', upload.key)
        print('upload id', upload.upload_id)
        print('owner', upload.owner)
        print('storage class', upload.storage_class)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

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

以下代码用于列举桶 bucket-test 前缀 exampledir/ 下的所有未合并的分片上传任务。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
prefix = 'exampledir/'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    is_truncated = True
    marker = ''
    while is_truncated:
        out = client.list_multipart_uploads(bucket_name, prefix=prefix, key_marker=marker)
        for upload in out.uploads:
            print('object key', upload.key)
            print('upload id', upload.upload_id)
            print('owner', upload.owner)
            print('storage class', upload.storage_class)
        is_truncated = out.is_truncated
        marker = out.next_key_marker
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

列举所有未合并的分片上传任务

以下代码用于列举桶 bucket-test 未合并的所有分片上传任务。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    is_truncated = True
    marker = ''
    while is_truncated:
        out = client.list_multipart_uploads(bucket_name, key_marker=marker)
        for upload in out.uploads:
            print('object key', upload.key)
            print('upload id', upload.upload_id)
            print('owner', upload.owner)
            print('storage class', upload.storage_class)
        is_truncated = out.is_truncated
        marker = out.next_key_marker
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

按目录递归列举未合并分片任务

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

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

以下代码用于按目录列举桶bucket-test中未合并的分片上传任务。

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
prefix = 'exampledir/'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    is_truncated = True
    marker = ''
    while is_truncated:
        out = client.list_multipart_uploads(bucket_name, key_marker=marker, delimiter='/', prefix=prefix)
        for prefix in out.common_prefixes:
            print('subDir', prefix)
            
        for upload in out.uploads:
            print('object key', upload.key)
            print('upload id', upload.upload_id)
            print('owner', upload.owner)
            print('storage class', upload.storage_class)
        is_truncated = out.is_truncated
        marker = out.next_key_marker
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))