You need to enable JavaScript to run this app.
文档中心
文档控制台
注册
对象存储

对象存储

复制全文
下载 pdf
桶基础接口
列举桶(Java SDK)
复制全文
下载 pdf
列举桶(Java SDK)

桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。本文介绍如何通过 TOS Java SDK 的 listBuckets 接口列举您账号下所有地域的桶(Bucket)列表。

注意事项

  • 列举桶之前,您必须具有 tos:ListBuckets 权限。具体操作,请参见 IAM 策略概述
  • 此接口会返回当前账号所有地域的桶。

示例代码

以下代码展示如何列举当前账号所有地域的桶。

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.bucket.ListBucketsV2Input;
import com.volcengine.tos.model.bucket.ListBucketsV2Output;
import com.volcengine.tos.model.bucket.ListedBucket;

public class ListBucketExample {
    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");

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);

        try{
            ListBucketsV2Input input = new ListBucketsV2Input();
            ListBucketsV2Output output = tos.listBuckets(input);
            System.out.println("bucket owner is " + output.getOwner());
            if (output.getBuckets() != null) {
                System.out.println("you have listed " + output.getBuckets().size() + " buckets");
                for (int i = 0; i < output.getBuckets().size(); i++){
                    ListedBucket bucket = output.getBuckets().get(i);
                    System.out.printf("Listed bucket, name is %s, location is %s, extranetEndpoint is %s, " +
                            "intranetEndpoint is %s, creationDate is %s.\n", bucket.getName(), bucket.getLocation(),
                            bucket.getExtranetEndpoint(), bucket.getIntranetEndpoint(), bucket.getCreationDate());
                }
            } else {
                System.out.println("there are no buckets in your account.");
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("listBuckets failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("listBuckets 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("listBuckets failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

相关文档

关于列举桶的 API 文档,请参见 ListBuckets

最近更新时间:2024.02.04 18:30:53
这个页面对您有帮助吗?
有用
有用
无用
无用