From 913f2a0407844476a6e908cf3cf33e46fa2056c0 Mon Sep 17 00:00:00 2001 From: zxl01071856 Date: Tue, 7 Jun 2022 16:08:03 +0800 Subject: [PATCH] Add bucket cname sample --- src/samples/BucketCnameSample.java | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/samples/BucketCnameSample.java diff --git a/src/samples/BucketCnameSample.java b/src/samples/BucketCnameSample.java new file mode 100644 index 00000000..a9e50ae7 --- /dev/null +++ b/src/samples/BucketCnameSample.java @@ -0,0 +1,56 @@ +import com.aliyun.oss.ClientException; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.aliyun.oss.model.*; +import java.util.List; + +public class BucketCnameSample { + + private static String endpoint = "*** Provide OSS endpoint ***"; + private static String accessKeyId = "*** Provide your AccessKeyId ***"; + private static String accessKeySecret = "*** Provide your AccessKeySecret ***"; + private static String bucketName = "*** Provide bucket name ***"; + + public static void main(String[] args) { + // Create an OSSClient instance. + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + + try { + // Create cnametoken required for domain name ownership verification + CreateBucketCnameTokenRequest request = new CreateBucketCnameTokenRequest(bucketName); + request.setDomain("www.example.com"); + ossClient.createBucketCnameToken(request); + + // Get the created cnametoken + GetBucketCnameTokenRequest grequest = new GetBucketCnameTokenRequest(bucketName); + grequest.setDomain("www.example.com"); + GetBucketCnameTokenResult gresult = ossClient.getBucketCnameToken(grequest); + System.out.println(gresult.getCname()); + System.out.println(gresult.getToken()); + + // Bind a custom domain name to a bucket + ossClient.addBucketCname(new AddBucketCnameRequest(bucketName).withDomain("www.example.com")); + + // Query the list of all cnames bound under a storage space (bucket) + List list = ossClient.getBucketCname(bucketName); + System.out.println(list.get(0).getDomain()); + + // Delete the bound CNAME of a storage space (bucket) + ossClient.deleteBucketCname(bucketName, "www.example.com"); + + } catch (OSSException oe) { + System.out.println("Error Message: " + oe.getErrorMessage()); + System.out.println("Error Code: " + oe.getErrorCode()); + System.out.println("Request ID: " + oe.getRequestId()); + System.out.println("Host ID: " + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Error Message: " + ce.getMessage()); + } finally { + /* + * Do not forget to shut down the client finally to release all allocated resources. + */ + ossClient.shutdown(); + } + } +}