Java綁定自定義域名
文件(Object)上傳至存儲(chǔ)空間(Bucket)后,OSS會(huì)自動(dòng)生成文件URL,您可以直接通過(guò)文件URL(即Bucket外網(wǎng)訪問(wèn)域名)訪問(wèn)該文件。如果您希望通過(guò)自定義域名訪問(wèn)這些Object,需要添加CNAME記錄將自定義域名綁定至Object所在的Bucket。
注意事項(xiàng)
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過(guò)與OSS同地域的其他阿里云產(chǎn)品訪問(wèn)OSS,請(qǐng)使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對(duì)應(yīng)關(guān)系,請(qǐng)參見(jiàn)OSS訪問(wèn)域名、數(shù)據(jù)中心、開(kāi)放端口。
本文以從環(huán)境變量讀取訪問(wèn)憑證為例。如何配置訪問(wèn)憑證,請(qǐng)參見(jiàn)Java配置訪問(wèn)憑證。
本文以OSS域名新建OSSClient為例。如果您希望通過(guò)自定義域名、STS等方式新建OSSClient,請(qǐng)參見(jiàn)新建OSSClient。
創(chuàng)建CnameToken
以下代碼用于創(chuàng)建CnameToken。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CreateBucketCnameTokenRequest;
import com.aliyun.oss.model.CreateBucketCnameTokenResult;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)自定義域名。
String domain = "www.example.com";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 創(chuàng)建CnameToken。
CreateBucketCnameTokenRequest request = new CreateBucketCnameTokenRequest(bucketName);
// 設(shè)置自定義域名。
request.setDomain(domain);
CreateBucketCnameTokenResult result = ossClient.createBucketCnameToken(request);
// 打印綁定的Cname名稱。
System.out.println(result.getCname());
// 打印OSS返回的CnameToken。
System.out.println(result.getToken());
// 打印CnameToken的過(guò)期時(shí)間。
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();
}
}
}
}
獲取CnameToken
以下代碼用于獲取CnameToken。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetBucketCnameTokenRequest;
import com.aliyun.oss.model.GetBucketCnameTokenResult;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)自定義域名。
String domain = "www.example.com";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 獲取CnameToken。
GetBucketCnameTokenRequest request = new GetBucketCnameTokenRequest(bucketName);
request.setDomain(domain);
GetBucketCnameTokenResult result = ossClient.getBucketCnameToken(request);
// 打印綁定的Cname名稱。
System.out.println(result.getCname());
// 打印OSS返回的CnameToken。
System.out.println(result.getToken());
// 打印CnameToken的過(guò)期時(shí)間。
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();
}
}
}
}
添加CNAME記錄
以下代碼用于添加CNAME記錄。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
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 {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)自定義域名。
String domain = "www.example.com";
// 填寫(xiě)證書(shū)路徑。
String crtPath = "oss/generic.testputcname.com.crt";
// 填寫(xiě)私鑰路徑。
String keyPath = "oss/generic.testputcname.com.key";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
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");
// 添加CNAME記錄。
AddBucketCnameRequest request = new AddBucketCnameRequest(bucketName)
.withDomain(domain)
.withCertificateConfiguration(new CertificateConfiguration()
// 設(shè)置證書(shū)公鑰。
.withPublicKey(pubKey)
// 設(shè)置證書(shū)私鑰。
.withPrivateKey(priKey)
/*// 設(shè)置當(dāng)前證書(shū)ID。
.withPreviousId("493****-cn-hangzhou")
// 是否強(qiáng)制覆蓋證書(shū)。
.withForceOverwriteCert(false)
// 是否刪除證書(shū)。
.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();
}
}
}
}
查看CNAME記錄
以下代碼用于查看CNAME記錄。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CnameConfiguration;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 獲取CNAME記錄。
List<CnameConfiguration> cnameConfigurations = ossClient.getBucketCname(bucketName);
if(cnameConfigurations.get(0) != null){
// 打印證書(shū)ID。
System.out.println(cnameConfigurations.get(0).getCertId());
// 打印自定義域名。
System.out.println(cnameConfigurations.get(0).getDomain());
// 打印證書(shū)來(lái)源。
System.out.println(cnameConfigurations.get(0).getCertType());
// 打印域名所處狀態(tài)。
System.out.println(cnameConfigurations.get(0).getStatus());
// 打印證書(shū)狀態(tài)。
System.out.println(cnameConfigurations.get(0).getCertStatus());
// 打印綁定自定義域名的時(shí)間。
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();
}
}
}
}
刪除CNAME記錄
以下代碼用于刪除CNAME記錄。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)自定義域名。
String domain = "www.example.com";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 刪除CNAME記錄。
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();
}
}
}
}
相關(guān)文檔
關(guān)于創(chuàng)建域名所有權(quán)驗(yàn)證所需的CnameToken的API接口說(shuō)明,請(qǐng)參見(jiàn)CreateCnameToken。
關(guān)于獲取CnameToken的API接口說(shuō)明,請(qǐng)參見(jiàn)GetCnameToken。
關(guān)于添加CNAME記錄的API接口說(shuō)明,請(qǐng)參見(jiàn)PutCname。
關(guān)于查看CNAME記錄的API接口說(shuō)明,請(qǐng)參見(jiàn)ListCname。
關(guān)于刪除CNAME記錄的API接口說(shuō)明,請(qǐng)參見(jiàn)DeleteCname。