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

对象存储

复制全文
下载 pdf
桶基础接口
获取桶地域(Java SDK)
复制全文
下载 pdf
获取桶地域(Java SDK)

桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。本文介绍如何通过 TOS Java SDK 的 getBucketLocation 接口获取桶所在的地域(Region)信息。

注意事项

只有桶的所有者(Owner)才能查看桶所在的地域(Region)。

示例代码

如下代码展示如何获取桶的地域(Region)信息。

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.GetBucketLocationInput;
import com.volcengine.tos.model.bucket.GetBucketLocationOutput;

public class GetBucketLocationExample {
    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 = "your bucket name";

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

        try{
            GetBucketLocationInput input = new GetBucketLocationInput().setBucket(bucketName);
            GetBucketLocationOutput output = tos.getBucketLocation(input);
            System.out.println("bucket's region is " + output.getRegion());
            System.out.println("bucket's extranet endpoint is " + output.getExtranetEndpoint());
            System.out.println("bucket's intranet endpoint is " + output.getIntranetEndpoint());
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("getBucketLocation failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("getBucketLocation 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("getBucketLocation failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}
最近更新时间:2024.02.04 18:31:03
这个页面对您有帮助吗?
有用
有用
无用
无用