このトピックでは、バケットのインベントリを作成する方法と、バケットに設定されたインベントリを照会、一覧表示、および削除する方法について説明します。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba Cloudサービスを使用してOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSでサポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientインスタンスを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
バケットのインベントリを作成、表示、一覧表示、および削除する権限があることを確認します。 デフォルトでは、バケット所有者に上記の操作を実行する権限があります。 上記の操作を実行する権限がない場合は、バケットの所有者に権限を付与するよう依頼してください。
バケットには最大1,000のインベントリを設定できます。
インベントリを構成するソースバケットは、インベントリリストが保存されているターゲットバケットと同じリージョンにデプロイする必要があります。
バケットのインベントリを作成する
次のコードは、バケットのインベントリを作成する方法の例を示しています。
com.aliyun.oss.ClientExceptionをインポートします。com.aliyun.oss.OSSをインポートします。impor t com.aliyun.oss.com mon.auth.*;
com.aliyun.oss.OSSClientBuilderをインポートします。com.aliyun.oss.OSSExceptionをインポートします。com.aliyun.oss.mo delをインポートします。*;
java.util.ArrayListをインポートします。java.util.Listをインポートします。public classデモ {
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";
// 生成されたインベントリリストを保存するバケットの名前を指定します。
文字列destBucketName ="yourDestinationBucketName";
// バケット所有者によって付与されたアカウントIDを指定します。
String accountId ="yourDestinationBucketAccountId";
// インベントリを設定するバケット内のすべてのオブジェクトを読み取る権限と、生成されたインベントリリストを格納するバケットにデータを書き込む権限を付与されたRAMロールの名前を指定します。
String roleArn ="yourDestinationBucketRoleArn";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// インベントリを作成します。
InventoryConfiguration inventoryConfiguration = new InventoryConfiguration();
// インベントリ名を指定します。
String inventoryId = "testid";
inventoryConfiguration.setInventoryId(inventoryId);
// インベントリリストに含まれるオブジェクト属性を指定します。
List<String> fields = new ArrayList<String>();
fields.add(InventoryOptionalFields.Size);
fields.add(InventoryOptionalFields.LastModifiedDate);
fields.add(InventoryOptionalFields.IsMultipartUploaded);
fields.add(InventoryOptionalFields.StorageClass);
fields.add(InventoryOptionalFields.ETag);
fields.add(InventoryOptionalFields.EncryptionStatus);
inventoryConfiguration.setOptionalFields (フィールド);
// 在庫リストを毎日または毎週生成するかどうかを指定します。 次のコードは、週単位でインベントリリストを生成する方法の例を示しています。 Weeklyは、在庫リストが週に1回生成されることを示し、Dailyは、在庫リストが1日に1回生成されることを示す。
inventoryConfiguration.setSchedule(new InventorySchedule().withFrequency(InventoryFrequency.Weekly));
// インベントリリストに現在のバージョンのオブジェクトのみが含まれるように指定します。 InventoryIncludedObjectVersionsパラメーターをAllに設定すると、オブジェクトのすべてのバージョンがインベントリリストに含まれます。 この設定は、バケットのバージョン管理を有効にした場合にのみ有効になります。
inventoryConfiguration.setIncludedObjectVersions(InventoryIncludedObjectVersions.Current);
// インベントリを有効にするかどうかを指定します。 有効な値は、true および false です。 インベントリを有効にするには、値をtrueに設定します。 インベントリを無効にするには、値をfalseに設定します。
inventoryConfiguration.setEnabled(true);
// インベントリリストに含めるオブジェクトのフィルタリングに使用するルールを指定します。 次のコードは、プレフィックスでオブジェクトをフィルタリングする方法の例を示しています。
InventoryFilter inventoryFilter = new InventoryFilter().withPrefix("obj-prefix");
inventoryConfiguration.setInventoryFilter(inventoryFilter);
// 生成されたインベントリリストを格納する宛先バケットを指定します。
InventoryOSSBucketDestination ossInvDest = new InventoryOSSBucketDestination();
// 生成されたインベントリリストを格納するパスのプレフィックスを指定します。
ossInvDest.setPrefix("destination-prefix");
// インベントリリストの形式を指定します。
ossInvDest.setFormat(InventoryFormat.CSV);
// 宛先バケットが属するアカウントのIDを指定します。
ossInvDest.setAccountId(accountId);
// 宛先バケットのロールARNを指定します。
ossInvDest.setRoleArn(roleArn);
// 宛先バケットの名前を指定します。
ossInvDest.setBucket(destBucketName);
// 次のコードは、キー管理システム (KMS) でホストされている顧客マスターキー (CMK) を使用して在庫リストを暗号化する方法の例を示しています。
// InventoryEncryption inventoryEncryption = new InventoryEncryption();
// InventoryServerSideEncryptionKMS serverSideKmsEncryption = new InventoryServerSideEncryptionKMS().withKeyId("test-kms-id");
// inventoryEncryption.setServerSideKmsEncryption(serverSideKmsEncryption);
// ossInvDest.setEncryption(inventoryEncryption);
// 次のコードは、OSSサーバー上のインベントリリストを暗号化する方法の例を示しています。
// InventoryEncryption inventoryEncryption = new InventoryEncryption();
// inventoryEncryption.setServerSideOssEncryption(new InventoryServerSideEncryptionOSS());
// ossInvDest.setEncryption(inventoryEncryption);
// 生成されたインベントリリストの宛先を指定します。
InventoryDestination=新しいInventoryDestination();
destination.setOssBucketDestination(ossInvDest);
inventoryConfiguration.setDestination (宛先);
// バケットのインベントリを設定します。
ossClient.setBucketInventoryConfiguration(bucketName, inventoryConfiguration);
} catch (Exception e) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "しかし、何らかの理由でエラー応答で拒否されました。");
System.out.println("エラーメッセージ:" + oe.getErrorMessage());
System.out.println("エラーコード:" + oe.getErrorCode());
System.out.println("リクエストID:" + oe.getRequestId());
System.out.println("ホストID:" + oe.getHostId());
} catch (ClientException e) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ 「ネットワークにアクセスできないなど」;
System.out.println("エラーメッセージ:" + ce.getMessage());
} 最後に{
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
バケット用に構成されたインベントリの照会
次のコードは、バケットに設定されたインベントリを照会する方法の例を示しています。
com.aliyun.oss.ClientExceptionをインポートします。com.aliyun.oss.OSSをインポートします。impor t com.aliyun.oss.com mon.auth.*;
com.aliyun.oss.OSSClientBuilderをインポートします。com.aliyun.oss.OSSExceptionをインポートします。com.aliyun.oss.mo delをインポートします。*;
java.util.Listをインポートします。public classデモ {
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";
// インベントリ名を指定します。
文字列inventoryId = "yourInventoryConfigurationId";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// 指定されたインベントリの構成を照会します。
GetBucketInventoryConfigurationRequest要求=新しいGetBucketInventoryConfigurationRequest(bucketName、inventoryId);
GetBucketInventoryConfigurationResult getResult = ossClient.getBucketInventoryConfiguration (リクエスト);
// インベントリの設定を表示します。
InventoryConfiguration config = getResult.getInventoryConfiguration();
System.out.println("===== インベントリ設定 =====");
System.out.println("inventoryId:" + config.getInventoryId());
System.out.println("isenabled:" + config.isEnabled());
System.out.println("includedVersions:" + config.getIncludedObjectVersions());
System.out.println("schdule:" + config.getSchedule().getFrequency());
if (config.getInventoryFilter().getPrefix() != null) {
System.out.println("filter, prefix:" + config.getInventoryFilter().getPrefix());
}
List<String> fields = config.getOptionalFields();
for (String field : fields) {
System.out.println("field:" + field);
}
System.out.println("=== bucket destination config ===");
InventoryOSSBucketDestination destin = config.getDestination().getOssBucketDestination();
System.out.println("format:" + destin.getFormat());
System.out.println("bucket:" + destin.getBucket());
System.out.println("prefix:" + destin.getPrefix());
System.out.println("accountId:" + destin.getAccountId());
System.out.println("roleArn:" + destin.getRoleArn());
if (destin.getEncryption() != null) {
if (destin.getEncryption().getServerSideKmsEncryption() != null) {
System.out.println("server-side kms encryption, key id:" + destin.getEncryption().getServerSideKmsEncryption().getKeyId());
} else if (destin.getEncryption().getServerSideOssEncryption() != null) {
System.out.println("server-side oss encryption.");
}
}
} catch (Exception e) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "しかし、何らかの理由でエラー応答で拒否されました。");
System.out.println("エラーメッセージ:" + oe.getErrorMessage());
System.out.println("エラーコード:" + oe.getErrorCode());
System.out.println("リクエストID:" + oe.getRequestId());
System.out.println("ホストID:" + oe.getHostId());
} catch (ClientException e) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ 「ネットワークにアクセスできないなど」;
System.out.println("エラーメッセージ:" + ce.getMessage());
} 最後に{
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
バケットに設定されたインベントリの一覧表示
1回のリクエストで最大100のインベントリを照会できます。 100を超えるインベントリをクエリするには、複数のリクエストを送信し、各リクエストに対して返されたトークンを次のリクエストのパラメーターとして使用する必要があります。
次のコードは、バケットに設定されたインベントリを一覧表示する方法の例です。
パッケージcom.aliyun.oss.de mo;
com.aliyun.oss.ClientExceptionをインポートします。com.aliyun.oss.OSSをインポートします。impor t com.aliyun.oss.com mon.auth.*;
com.aliyun.oss.OSSClientBuilderをインポートします。com.aliyun.oss.OSSExceptionをインポートします。com.aliyun.oss.mo delをインポートします。*;
java.util.Listをインポートします。public classデモ {
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";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// 在庫をリストします。 デフォルトでは、最大100の在庫がリストされます。 100を超えるインベントリが設定されている場合、インベントリはページに一覧表示されます。 continuationTokenパラメーターを使用して、後続のページのインベントリを一覧表示できます。
String continuationToken = null;
一方、(TRUE){
ListBucketInventoryConfigurationsRequest listRequest = new ListBucketInventoryConfigurationsRequest(bucketName, continuationToken);
ListBucketInventoryConfigurationsResult result = ossClient.listBucketInventoryConfigurations(listRequest);
System.out.println("======= リストバケット在庫設定 =======");
System.out.println("istruncated:" + result.isTruncated());
System.out.println("continuationToken:" + result.getContinuationToken());
System.out.println("nextContinuationToken:" + result.getNextContinuationToken());
System.out.println("list size :" + result.getInventoryConfigurationList());
if (result.getInventoryConfigurationList() != null && !result.getInventoryConfigurationList().isEmpty()) {
for (InventoryConfiguration config : result.getInventoryConfigurationList()) {
System.out.println("=== 在庫設定 ===");
System.out.println("inventoryId:" + config.getInventoryId());
System.out.println("isenabled:" + config.isEnabled());
System.out.println("includedVersions:" + config.getIncludedObjectVersions());
System.out.println("schdule:" + config.getSchedule().getFrequency());
if (config.getInventoryFilter().getPrefix() != null) {
System.out.println("filter, prefix:" + config.getInventoryFilter().getPrefix());
}
List<String> fields = config.getOptionalFields();
for (String field : fields) {
System.out.println("field:" + field);
}
System.out.println("=== bucket destination config ===");
InventoryOSSBucketDestination destin = config.getDestination().getOssBucketDestination();
System.out.println("format:" + destin.getFormat());
System.out.println("bucket:" + destin.getBucket());
System.out.println("prefix:" + destin.getPrefix());
System.out.println("accountId:" + destin.getAccountId());
System.out.println("roleArn:" + destin.getRoleArn());
if (destin.getEncryption() != null) {
if (destin.getEncryption().getServerSideKmsEncryption() != null) {
System.out.println("server-side kms encryption key id:" + destin.getEncryption().getServerSideKmsEncryption().getKeyId());
} else if (destin.getEncryption().getServerSideOssEncryption() != null) {
System.out.println("server-side oss encryption.");
}
}
}
if (result.isTruncated()) {
continuationToken = result.getNextContinuationToken();
} else {
break;
}
}
}
} catch (Exception e) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "しかし、何らかの理由でエラー応答で拒否されました。");
System.out.println("エラーメッセージ:" + oe.getErrorMessage());
System.out.println("エラーコード:" + oe.getErrorCode());
System.out.println("リクエストID:" + oe.getRequestId());
System.out.println("ホストID:" + oe.getHostId());
} catch (ClientException e) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ 「ネットワークにアクセスできないなど」;
System.out.println("エラーメッセージ:" + ce.getMessage());
} 最後に{
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
バケット用に設定されたインベントリの削除
次のコードは、バケットに設定されたインベントリを削除する方法の例を示しています。
com.aliyun.oss.ClientExceptionをインポートします。com.aliyun.oss.OSSをインポートします。impor t com.aliyun.oss.com mon.auth.*;
com.aliyun.oss.OSSClientBuilderをインポートします。com.aliyun.oss.OSSExceptionをインポートします。public classデモ {
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";
// 削除するインベントリの名前を指定します。
文字列inventoryId = "yourInventoryConfigurationId";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// インベントリを削除します。
ossClient.de leteBucketInventoryConfiguration(bucketName、inventoryId);
} catch (Exception e) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "しかし、何らかの理由でエラー応答で拒否されました。");
System.out.println("エラーメッセージ:" + oe.getErrorMessage());
System.out.println("エラーコード:" + oe.getErrorCode());
System.out.println("リクエストID:" + oe.getRequestId());
System.out.println("ホストID:" + oe.getHostId());
} catch (ClientException e) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ 「ネットワークにアクセスできないなど」;
System.out.println("エラーメッセージ:" + ce.getMessage());
} 最後に{
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
参考資料
バケットインベントリの完全なサンプルコードの詳細については、『GitHub』をご参照ください。
バケットのインベントリを作成するためのAPI操作の詳細については、「PutBucketInventory」をご参照ください。
バケットに設定されたインベントリを照会するためのAPI操作の詳細については、「GetBucketInventory」をご参照ください。
バケットに設定されたインベントリを一覧表示するAPI操作の詳細については、「ListBucketInventory」をご参照ください。
バケットに設定されたインベントリを削除するAPI操作の詳細については、「DeleteBucketInventory」をご参照ください。