当您将文件上传至 TOS 后,您可以使用 TOS 自动生成的文件 URL 访问或下载文件。如果您希望使用您自己的域名访问存储在 TOS 的文件,您可以为存储桶绑定您自己的域名。绑定后,即可使用相应域名访问文件。
您可以通过 TOS Java SDK 的 putBucketCustomDomain
接口获取指定桶的自定义域名配置。
注意
以下代码展示如何设置桶的自定义域名。
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.CustomDomainRule; import com.volcengine.tos.model.bucket.PutBucketCustomDomainInput; import com.volcengine.tos.model.bucket.PutBucketCustomDomainOutput; public class PutBucketCustomDomainExample { 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"; String customDomain = "example.test.com"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ CustomDomainRule rule = new CustomDomainRule().setDomain(customDomain); PutBucketCustomDomainInput input = new PutBucketCustomDomainInput().setBucket(bucketName).setRule(rule); PutBucketCustomDomainOutput output = tos.putBucketCustomDomain(input); System.out.println("putBucketCustomDomain succeed"); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("putBucketCustomDomain failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("putBucketCustomDomain 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("putBucketCustomDomain failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 listBucketCustomDomain
接口获取指定桶配置的自定义域名列表。
注意
要列举桶的自定义域名列表,默认您必须为桶所有者。
以下代码展示如何列举桶的自定义域名列表。
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.CustomDomainRule; import com.volcengine.tos.model.bucket.ListBucketCustomDomainInput; import com.volcengine.tos.model.bucket.ListBucketCustomDomainOutput; public class GetBucketCustomDomainExample { 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"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ ListBucketCustomDomainInput input = new ListBucketCustomDomainInput().setBucket(bucketName); ListBucketCustomDomainOutput output = tos.listBucketCustomDomain(input); System.out.println("listBucketCustomDomain succeed"); if (output.getRule() != null && output.getRule().size() > 0) { for(int i = 0; i < output.getRule().size(); i++) { CustomDomainRule rule = output.getRule().get(i); System.out.println("No." + i + " domain is " + rule.getDomain()); } } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("listBucketCustomDomain failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("listBucketCustomDomain 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("listBucketCustomDomain failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 deleteBucketCustomDomain
接口删除指定桶配置的自定义域名。
注意
要删除桶的自定义域名,默认您必须为桶所有者。
以下代码展示如何删除桶的自定义域名规则。
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.DeleteBucketCustomDomainInput; import com.volcengine.tos.model.bucket.DeleteBucketCustomDomainOutput; public class DeleteBucketCustomDomainExample { 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"; String customDomain = "example.test.com"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketCustomDomainInput input = new DeleteBucketCustomDomainInput().setBucket(bucketName) .setDomain(customDomain); DeleteBucketCustomDomainOutput output = tos.deleteBucketCustomDomain(input); System.out.println("deleteBucketCustomDomain succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucketCustomDomain failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucketCustomDomain 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("deleteBucketCustomDomain failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于自定义域名配置的更多信息,请参见自定义域名。