You need to enable JavaScript to run this app.
导航
列举未合并的对象(列举分片上传任务 )-Java SDK
最近更新时间:2024.02.04 18:31:02首次发布时间:2023.01.19 14:37:15

TOS Java SDK 的 listMultipartUploads 接口用于列举桶中正在进行的分片上传任务。本文介绍如何通过 listMultipartUploads 接口简单列举指定桶中正在进行的分片上传任务、列举所有正在进行的分片上传任务、列举包含指定前缀的正在进行的分片上传任务、列举指定目录下的正在进行的分片上传任务等。

注意事项

  • 要列举分片上传任务,您需要拥有对桶的 tos:ListBucketMultipartUploads 权限,具体操作请参见 IAM 策略概述
  • 此列举针对的是正在进行的、未合并分片的分片上传任务。如果某个分片上传任务已经合并分片,该任务将无法被列举到。

参数说明

listMultipartUploads 接口支持的参数如下。

参数

描述

prefix

本次查询结果的对象名前缀。

delimiter

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

keyMarker

此次列举对象的起点。初始为 null,后续可取值为上次请求返回的 nextKeyMarker。

uploadIDMarker

与 keyMarker 配合使用。初始为 null,后续可取值为上次请求返回的 nextUploadIDMarker。

maxUploads

返回列举分片上传任务的最大数。一次最多返回 1000 条结果,若不设置则默认返回 1000 条。

encodingType

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

示例代码

简单列举

以下代码展示如何列举桶 bucket-example 中最多 10 个未合并的分片上传任务。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.*;

public class ListMultipartUploadsBasicExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        int maxKeys = 10;

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
        try{
            ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                    .setMaxUploads(maxKeys);
            ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
            System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                    "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
            if (output.getUploads() != null) {
                for (int i = 0; i < output.getUploads().size(); i++) {
                    ListedUpload upload = output.getUploads().get(i);
                    System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n", 
                            upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                }
            }
            if (output.getCommonPrefixes() != null) {
                for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                    ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                    System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                }
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listMultipartUploads failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listMultipartUploads failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("listMultipartUploads failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

分页列举

以下代码展示如何分页列举桶 bucket-example 中的未合并的分片上传任务。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.*;

public class ListMultipartUploadsByPageExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        int maxKeys = 10;

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
        try{
            ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                    .setMaxUploads(maxKeys);
            ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
            System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                    "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
            if (output.getUploads() != null) {
                for (int i = 0; i < output.getUploads().size(); i++) {
                    ListedUpload upload = output.getUploads().get(i);
                    System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n", 
                            upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                }
            }
            if (output.getCommonPrefixes() != null) {
                for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                    ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                    System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                }
            }
            if (output.isTruncated()) {
                // isTruncated 为 true 说明接下来还有数据,可以继续列举
                // 下一页的列举需要设置上一次列举返回的 NextKeyMarker 和 NextUploadIDMarker,必须同时设置
                input.setKeyMarker(output.getNextKeyMarker()).setUploadIDMarker(output.getNextUploadIdMarker());
                output = tos.listMultipartUploads(input);
                System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                        "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
                if (output.getUploads() != null) {
                    for (int i = 0; i < output.getUploads().size(); i++) {
                        ListedUpload upload = output.getUploads().get(i);
                        System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n", 
                            upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                    }
                }
                if (output.getCommonPrefixes() != null) {
                    for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                        ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                        System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                    }
                }
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listMultipartUploads failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listMultipartUploads failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("listMultipartUploads failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

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

以下代码展示如何列举桶 bucket-example 中指定前缀的所有未合并的分片上传任务。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.*;

public class ListMultipartUploadsByPrefixExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        int maxKeys = 10;
        String prefix = "example_dir/";

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
        try{
            boolean isTruncated = true;
            String keyMarker = null;
            String uploadIdMarker = null;
            while (isTruncated) {
                ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                        .setMaxUploads(maxKeys).setPrefix(prefix).setKeyMarker(keyMarker).setUploadIDMarker(uploadIdMarker);
                ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
                System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                        "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
                if (output.getUploads() != null) {
                    for (int i = 0; i < output.getUploads().size(); i++) {
                        ListedUpload upload = output.getUploads().get(i);
                        System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n",
                                upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                    }
                }
                if (output.getCommonPrefixes() != null) {
                    for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                        ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                        System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                    }
                }
                isTruncated = output.isTruncated();
                keyMarker = output.getNextKeyMarker();
                uploadIdMarker = output.getNextUploadIdMarker();
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listMultipartUploads failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listMultipartUploads failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("listMultipartUploads failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

列举桶内所有分片任务

以下代码展示如何列举桶 bucket-example 中所有未合并的分片上传任务。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.*;

public class ListMultipartUploadsAllObjectExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        int maxKeys = 10;

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
        try{
            boolean isTruncated = true;
            String keyMarker = null;
            String uploadIdMarker = null;
            while (isTruncated) {
                ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                        .setMaxUploads(maxKeys).setKeyMarker(keyMarker).setUploadIDMarker(uploadIdMarker);
                ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
                System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                        "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
                if (output.getUploads() != null) {
                    for (int i = 0; i < output.getUploads().size(); i++) {
                        ListedUpload upload = output.getUploads().get(i);
                        System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n", 
                            upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                    }
                }
                if (output.getCommonPrefixes() != null) {
                    for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                        ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                        System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                    }
                }
                isTruncated = output.isTruncated();
                keyMarker = output.getNextKeyMarker();
                uploadIdMarker = output.getNextUploadIdMarker();
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listMultipartUploads failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listMultipartUploads failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("listMultipartUploads failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

列举指定目录下的未合并的分片上传任务和子目录

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

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

以下代码展示如何列举桶 bucket-example 根目录下的未合并的分片上传任务和子目录。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.*;

public class ListMultipartUploadsByDelimiterExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        int maxKeys = 10;
        String delimiter = "/";

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
        try{
            boolean isTruncated = true;
            String keyMarker = null;
            String uploadIdMarker = null;
            while (isTruncated) {
                ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                        .setMaxUploads(maxKeys).setDelimiter(delimiter).setKeyMarker(keyMarker).setUploadIDMarker(uploadIdMarker);
                ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
                System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                        "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
                if (output.getUploads() != null) {
                    for (int i = 0; i < output.getUploads().size(); i++) {
                        ListedUpload upload = output.getUploads().get(i);
                        System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n",
                                upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                    }
                }
                if (output.getCommonPrefixes() != null) {
                    for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                        ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                        System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                        // commonPrefix 为子目录,可用其作为 prefix 参数继续往下列举
                        listCommonPrefix(bucketName, maxKeys, delimiter, commonPrefix.getPrefix(), tos);
                    }
                }
                isTruncated = output.isTruncated();
                keyMarker = output.getNextKeyMarker();
                uploadIdMarker = output.getNextUploadIdMarker();
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listMultipartUploads failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listMultipartUploads failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("listMultipartUploads failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }

    private static void listCommonPrefix(String bucketName, int maxKeys, String delimiter, String prefix, TOSV2 tos) {
        boolean isTruncated = true;
        String keyMarker = null;
        String uploadIdMarker = null;
        while (isTruncated) {
            ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)
                    .setMaxUploads(maxKeys).setDelimiter(delimiter).setKeyMarker(keyMarker)
                    .setUploadIDMarker(uploadIdMarker).setPrefix(prefix);
            ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);
            System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +
                    "next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());
            if (output.getUploads() != null) {
                for (int i = 0; i < output.getUploads().size(); i++) {
                    ListedUpload upload = output.getUploads().get(i);
                    System.out.printf("Listed upload, key is %s, uploadId is %s, storageClass is %s\n",
                            upload.getKey(), upload.getUploadID(), upload.getStorageClass().getStorageClass());
                }
            }
            if (output.getCommonPrefixes() != null) {
                for (int i = 0; i < output.getCommonPrefixes().size(); i++) {
                    ListedCommonPrefix commonPrefix = output.getCommonPrefixes().get(i);
                    System.out.println("Listed commonPrefix is " + commonPrefix.getPrefix());
                    // commonPrefix 为子目录,可用其作为 prefix 参数继续往下列举
                }
            }
            isTruncated = output.isTruncated();
            keyMarker = output.getNextKeyMarker();
            uploadIdMarker = output.getNextUploadIdMarker();
        }
    }
}

相关文档

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