桶(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()); } } }