You need to enable JavaScript to run this app.
导航
列举未合并的对象(列举分片上传任务)- Node.js SDK
最近更新时间:2024.02.04 18:30:57首次发布时间:2022.05.13 18:28:15

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

注意事项

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

参数说明

参数

描述

delimiter

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

encodingType

对返回的内容进行编码并指定编码的类型。取值说明如下:

  • undefined:默认值,无编码。
  • 'url':进行 url 编码。

maxUploads

返回分片上传任务的最大数量。默认值为 1000。
取值:(0,1000]

prefix

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

keyMarker

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

uploadIDMarker

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

示例代码

简单列举

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

// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: process.env['TOS_ACCESS_KEY'],
  accessKeySecret: process.env['TOS_SECRET_KEY'],
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

function handleError(error) {
  if (error instanceof TosClientError) {
    console.log('Client Err Msg:', error.message);
    console.log('Client Err Stack:', error.stack);
  } else if (error instanceof TosServerError) {
    console.log('Request ID:', error.requestId);
    console.log('Response Status Code:', error.statusCode);
    console.log('Response Header:', error.headers);
    console.log('Response Err Code:', error.code);
    console.log('Response Err Msg:', error.message);
  } else {
    console.log('unexpected exception, message: ', error);
  }
}

const bucketName = 'node-sdk-test-bucket';
async function main() {
  try {
    // 获取 bucket 中最多 10 个未完成任务
    const { data } = await client.listMultipartUploads({
      bucket: bucketName,
      maxKeys: 1000,
    });

    console.log('Uploads.length:', data.Uploads.length);
    for (const upload of data.Uploads) {
      console.log('Object Key:', upload.Key);
      console.log('UploadID:', upload.UploadId);
      console.log('Owner:', upload.Owner);
      console.log('StorageClass:', upload.StorageClass);
    }
  } catch (error) {
    handleError(error);
  }
}

main();

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

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

// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: process.env['TOS_ACCESS_KEY'],
  accessKeySecret: process.env['TOS_SECRET_KEY'],
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

function handleError(error) {
  if (error instanceof TosClientError) {
    console.log('Client Err Msg:', error.message);
    console.log('Client Err Stack:', error.stack);
  } else if (error instanceof TosServerError) {
    console.log('Request ID:', error.requestId);
    console.log('Response Status Code:', error.statusCode);
    console.log('Response Header:', error.headers);
    console.log('Response Err Code:', error.code);
    console.log('Response Err Msg:', error.message);
  } else {
    console.log('unexpected exception, message: ', error);
  }
}

const bucketName = 'node-sdk-test-bucket';
async function main() {
  try {
    // 列举指定前缀的所有多版本对象
    const prefix = '*** Provide your object key prefix ***';
    for (let truncated = true, keyMarker = '', uploadIdMarker = ''; truncated; ) {
      const { data } = await client.listMultipartUploads({
        bucket: bucketName,
        maxUploads: 1000,
        prefix,
        keyMarker,
        uploadIdMarker,
      });

      console.log('Uploads.length:', data.Uploads.length);
      for (const upload of data.Uploads) {
        console.log('Object Key:', upload.Key);
        console.log('UploadID:', upload.UploadId);
        console.log('Owner:', upload.Owner);
        console.log('StorageClass:', upload.StorageClass);
      }

      truncated = data.IsTruncated;
      keyMarker = data.NextKeyMarker;
      uploadIdMarker = data.NextUploadIdMarker;
    }
  } catch (error) {
    handleError(error);
  }
}

main();

列举所有分片上传任务

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

// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: process.env['TOS_ACCESS_KEY'],
  accessKeySecret: process.env['TOS_SECRET_KEY'],
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});

function handleError(error) {
  if (error instanceof TosClientError) {
    console.log('Client Err Msg:', error.message);
    console.log('Client Err Stack:', error.stack);
  } else if (error instanceof TosServerError) {
    console.log('Request ID:', error.requestId);
    console.log('Response Status Code:', error.statusCode);
    console.log('Response Header:', error.headers);
    console.log('Response Err Code:', error.code);
    console.log('Response Err Msg:', error.message);
  } else {
    console.log('unexpected exception, message: ', error);
  }
}

const bucketName = 'node-sdk-test-bucket';
async function main() {
  try {
    // 列举所有多版本对象
    for (let truncated = true, keyMarker = '', uploadIdMarker = ''; truncated; ) {
      const { data } = await client.listMultipartUploads({
        bucket: bucketName,
        maxUploads: 1000,
        keyMarker,
        uploadIdMarker,
      });

      console.log('Uploads.length:', data.Uploads.length);
      for (const upload of data.Uploads) {
        console.log('Object Key:', upload.Key);
        console.log('UploadID:', upload.UploadId);
        console.log('Owner:', upload.Owner);
        console.log('StorageClass:', upload.StorageClass);
      }

      truncated = data.IsTruncated;
      keyMarker = data.NextKeyMarker;
      uploadIdMarker = data.NextUploadIdMarker;
    }
  } catch (error) {
    handleError(error);
  }
}

main();