オブジェクトをバケットにアップロードすると、Object Storage Service (OSS) はオブジェクトへのアクセスに使用できる URL を自動的に生成します。この URL には、バケットのパブリックエンドポイントが含まれています。カスタムドメイン名を使用してオブジェクトにアクセスするには、CNAME レコードを追加して、カスタムドメイン名をオブジェクトが保存されているバケットにバインドする必要があります。
注意事項
本トピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。同じリージョン内の他の Alibaba Cloud サービスから OSS にアクセスするには、内部エンドポイントを使用します。サポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
本トピックでは、アクセス認証情報は環境変数から取得します。アクセス認証情報の設定方法の詳細については、「アクセス認証情報の設定」をご参照ください。
本トピックでは、OSSClient インスタンスは OSS エンドポイントを使用して作成されます。カスタムドメイン名またはセキュリティトークンサービス (STS) を使用して OSSClient インスタンスを作成する場合は、「一般的なシナリオの設定例」をご参照ください。
CnameToken の作成
次のコードは、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 {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。エンドポイントを実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// カスタムドメイン名を指定します。
String domain = "www.example.com";
// バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンを示す cn-hangzhou を使用します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// CnameToken を作成します。
CreateBucketCnameTokenRequest request = new CreateBucketCnameTokenRequest(bucketName);
// カスタムドメイン名を設定します。
request.setDomain(domain);
CreateBucketCnameTokenResult result = ossClient.createBucketCnameToken(request);
// バインドされている CNAME を出力します。
System.out.println(result.getCname());
// OSS から返された CnameToken を出力します。
System.out.println(result.getToken());
// 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();
}
}
}
}CnameToken の取得
次のコードは、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 {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。エンドポイントを実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// カスタムドメイン名を指定します。
String domain = "www.example.com";
// バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンを示す cn-hangzhou を使用します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
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 の有効期限を出力します。
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.*;
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 {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。エンドポイントを実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// カスタムドメイン名を指定します。
String domain = "www.example.com";
// 証明書のパスを指定します。
String crtPath = "oss/generic.testputcname.com.crt";
// 秘密鍵のパスを指定します。
String keyPath = "oss/generic.testputcname.com.key";
// バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンを示す cn-hangzhou を使用します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
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()
// 証明書の公開鍵を設定します。
.withPublicKey(pubKey)
// 証明書の秘密鍵を設定します。
.withPrivateKey(priKey)
/*// 現在の証明書 ID を設定します。
.withPreviousId("493****-cn-hangzhou")
// 証明書を強制的に上書きするかどうかを指定します。
.withForceOverwriteCert(false)
// 証明書を削除するかどうかを指定します。
.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.*;
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 {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。エンドポイントを実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンを示す cn-hangzhou を使用します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
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){
// 証明書 ID を出力します。
System.out.println(cnameConfigurations.get(0).getCertId());
// カスタムドメイン名を出力します。
System.out.println(cnameConfigurations.get(0).getDomain());
// 証明書のソースを出力します。
System.out.println(cnameConfigurations.get(0).getCertType());
// ドメイン名のステータスを出力します。
System.out.println(cnameConfigurations.get(0).getStatus());
// 証明書のステータスを出力します。
System.out.println(cnameConfigurations.get(0).getCertStatus());
// カスタムドメイン名がバインドされた時間を出力します。
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.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。エンドポイントを実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// カスタムドメイン名を指定します。
String domain = "www.example.com";
// バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンを示す cn-hangzhou を使用します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
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();
}
}
}
}関連ドキュメント
ドメインの所有権の検証のために CnameToken を作成する API 操作の詳細については、「CreateCnameToken」をご参照ください。
CnameToken を取得する API 操作の詳細については、「GetCnameToken」をご参照ください。
CNAME レコードを追加する API 操作の詳細については、「PutCname」をご参照ください。
CNAME レコードを表示する API 操作の詳細については、「ListCname」をご参照ください。
CNAME レコードを削除する API 操作の詳細については、「DeleteCname」をご参照ください。