After you upload an object to a bucket, OSS automatically generates a URL for the object. You can use this URL, which is the public endpoint of the bucket, to directly access the object. If you want to access objects using a custom domain name, you must add a CNAME record to map the custom domain name to the bucket.
Precautions
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.
Create a CnameToken
The following code provides an example of how to create a CnameToken.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.CreateBucketCnameTokenRequest; import com.aliyun.oss.model.CreateBucketCnameTokenResult; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used as an example. Replace it with the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the custom domain name. String domain = "www.example.com"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Create a CnameToken. CreateBucketCnameTokenRequest request = new CreateBucketCnameTokenRequest(bucketName); // Set the custom domain name. request.setDomain(domain); CreateBucketCnameTokenResult result = ossClient.createBucketCnameToken(request); // Print the mapped CNAME. System.out.println(result.getCname()); // Print the CnameToken that is returned by OSS. System.out.println(result.getToken()); // Print the expiration time of the CnameToken. System.out.println(result.getExpireTime()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); 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("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
Obtain a CnameToken
The following code provides an example of how to obtain a CnameToken.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.GetBucketCnameTokenRequest; import com.aliyun.oss.model.GetBucketCnameTokenResult; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used as an example. Replace it with the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the custom domain name. String domain = "www.example.com"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Obtain the CnameToken. GetBucketCnameTokenRequest request = new GetBucketCnameTokenRequest(bucketName); request.setDomain(domain); GetBucketCnameTokenResult result = ossClient.getBucketCnameToken(request); // Print the mapped CNAME. System.out.println(result.getCname()); // Print the CnameToken that is returned by OSS. System.out.println(result.getToken()); // Print the expiration time of the CnameToken. System.out.println(result.getExpireTime()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); 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("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
Add a CNAME record
The following code provides an example of how to add a CNAME record.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.common.utils.IOUtils; import com.aliyun.oss.model.AddBucketCnameRequest; import com.aliyun.oss.model.CertificateConfiguration; import java.io.FileInputStream; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used as an example. Replace it with the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the custom domain name. String domain = "www.example.com"; // Specify the path of the certificate. String crtPath = "oss/generic.testputcname.com.crt"; // Specify the path of the private key. String keyPath = "oss/generic.testputcname.com.key"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { String pubKey = IOUtils.readStreamAsString(new FileInputStream(crtPath), "utf8"); String priKey = IOUtils.readStreamAsString(new FileInputStream(keyPath), "utf8"); // Add a CNAME record. AddBucketCnameRequest request = new AddBucketCnameRequest(bucketName) .withDomain(domain) .withCertificateConfiguration(new CertificateConfiguration() // Set the public key of the certificate. .withPublicKey(pubKey) // Set the private key of the certificate. .withPrivateKey(priKey) /*// Set the ID of the current certificate. .withPreviousId("493****-cn-hangzhou") // Specify whether to forcibly overwrite the certificate. .withForceOverwriteCert(false) // Specify whether to delete the certificate. .withDeleteCertificate(false)*/); ossClient.addBucketCname(request); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); 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("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
View CNAME records
The following code provides an example of how to view CNAME records.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.CnameConfiguration; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used as an example. Replace it with the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Obtain CNAME records. List<CnameConfiguration> cnameConfigurations = ossClient.getBucketCname(bucketName); if(cnameConfigurations.get(0) != null){ // Print the certificate ID. System.out.println(cnameConfigurations.get(0).getCertId()); // Print the custom domain name. System.out.println(cnameConfigurations.get(0).getDomain()); // Print the source of the certificate. System.out.println(cnameConfigurations.get(0).getCertType()); // Print the status of the domain name. System.out.println(cnameConfigurations.get(0).getStatus()); // Print the status of the certificate. System.out.println(cnameConfigurations.get(0).getCertStatus()); // Print the time when the custom domain name was bound. System.out.println(cnameConfigurations.get(0).getLastMofiedTime()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); 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("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
Delete a CNAME record
The following code provides an example of how to delete a CNAME record.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used as an example. Replace it with the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the custom domain name. String domain = "www.example.com"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Delete the CNAME record. ossClient.deleteBucketCname(bucketName, domain); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); 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("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
References
For more information about the API operation that you can call to create a CnameToken for domain name ownership verification, see CreateCnameToken.
For more information about the API operation that you can call to obtain a CnameToken, see GetCnameToken.
For more information about the API operation that you can call to add a CNAME record, see PutCname.
For more information about the API operation that you can call to view CNAME records, see ListCname.
For more information about the API operation that you can call to delete a CNAME record, see DeleteCname.