このトピックでは、バケットインベントリ設定の追加、表示、リスト、削除の方法について説明します。
注意事項
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。同じリージョン内の他の Alibaba Cloud サービスから Object Storage Service (OSS) にアクセスするには、内部エンドポイントを使用します。サポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、アクセス認証情報は環境変数から取得されます。アクセス認証情報の設定方法の詳細については、「アクセス認証情報の設定」をご参照ください。
このトピックでは、OSS エンドポイントを使用して OSSClient インスタンスが作成されます。カスタムドメイン名または Security Token Service (STS) を使用して OSSClient インスタンスを作成する場合は、「一般的なシナリオの設定例」をご参照ください。
バケットのインベントリを作成、表示、リスト、削除する権限があることを確認してください。デフォルトでは、バケットの所有者は前述の操作を実行する権限を持っています。前述の操作を実行する権限がない場合は、バケットの所有者に権限の付与を依頼してください。
バケットには最大 1,000 個のインベントリを設定できます。
インベントリを設定するソースバケットと、インベントリリストが格納される宛先バケットは、同じリージョンにデプロイする必要があります。
インベントリ設定の追加
次のコードは、バケットにインベントリ設定を追加する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
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";
// インベントリ結果を格納するバケットの名前を指定します。
String destBucketName ="yourDestinationBucketName";
// 宛先バケットが属するアカウントの ID を指定します。
String accountId ="yourDestinationBucketAccountId";
// ソースバケットからすべてのオブジェクトを読み取り、宛先バケットにファイルを書き込む権限を持つ RAM ロールを指定します。
String roleArn ="yourDestinationBucketRoleArn";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを 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 {
// インベントリ設定を作成します。
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(fields);
// インベントリの生成スケジュールを設定します。次の例では、スケジュールを週次に設定します。Weekly はインベントリが週に 1 回生成されることを示します。Daily はインベントリが 1 日に 1 回生成されることを示します。
inventoryConfiguration.setSchedule(new InventorySchedule().withFrequency(InventoryFrequency.Weekly));
// インベントリに含めるオブジェクトのバージョンを現在のバージョンに設定します。このパラメーターを InventoryIncludedObjectVersions.All に設定すると、バージョン管理が有効な場合、すべてのオブジェクトバージョンが含まれます。
inventoryConfiguration.setIncludedObjectVersions(InventoryIncludedObjectVersions.Current);
// インベントリ設定を有効にするかどうかを指定します。値を 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);
// 宛先バケットのユーザー AccountId を設定します。
ossInvDest.setAccountId(accountId);
// 宛先バケットのロール ARN を設定します。
ossInvDest.setRoleArn(roleArn);
// 宛先バケットの名前を設定します。
ossInvDest.setBucket(destBucketName);
// KMS を使用してインベントリを暗号化するには、次の設定を使用します。
// 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 destination = new InventoryDestination();
destination.setOssBucketDestination(ossInvDest);
inventoryConfiguration.setDestination(destination);
// インベントリ設定を設定します。
ossClient.setBucketInventoryConfiguration(bucketName, inventoryConfiguration);
} 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();
}
}
}
}インベントリ設定の表示
次のコードは、バケットのインベントリ設定を表示する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
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";
// インベントリルール名を指定します。
String inventoryId = "yourInventoryConfigurationId";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを 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 {
// 指定されたインベントリルールの設定情報を表示します。
GetBucketInventoryConfigurationRequest request = new GetBucketInventoryConfigurationRequest(bucketName, inventoryId);
GetBucketInventoryConfigurationResult getResult = ossClient.getBucketInventoryConfiguration(request);
// インベントリ設定情報を出力します。
InventoryConfiguration config = getResult.getInventoryConfiguration();
System.out.println("=====Inventory configuration=====");
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 (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();
}
}
}
}インベントリ設定のリスト
1 回のリクエストで取得できるインベントリ設定は最大 100 件です。100 件を超えるインベントリ設定を取得するには、複数のリクエストを送信する必要があります。次のリクエストのパラメーターとして、前のリクエストから返されたトークンを使用します。
次のコードは、バケットのインベントリ設定をリストする方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
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 {
// インベントリ設定をリストします。デフォルトでは、一度に最大 100 件の結果が返されます。100 件を超える設定が存在する場合、結果はページ分割されます。トークンを渡して次のページをリストします。
String continuationToken = null;
while (true) {
ListBucketInventoryConfigurationsRequest listRequest = new ListBucketInventoryConfigurationsRequest(bucketName, continuationToken);
ListBucketInventoryConfigurationsResult result = ossClient.listBucketInventoryConfigurations(listRequest);
System.out.println("=======List bucket inventory configuration=======");
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("===Inventory configuration===");
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 (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();
}
}
}
}インベントリ設定の削除
次のコードは、バケットのインベントリ設定を削除する方法を示しています。
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 inventoryId = "yourInventoryConfigurationId";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを 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 {
// インベントリ設定を削除します。
ossClient.deleteBucketInventoryConfiguration(bucketName, inventoryId);
} 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();
}
}
}
}関連ドキュメント
バケットインベントリの完全なサンプルコードについては、「GitHub サンプル」をご参照ください。
バケットのインベントリを作成するための API 操作の詳細については、「PutBucketInventory」をご参照ください。
バケットに設定されたインベントリをクエリするための API 操作の詳細については、「GetBucketInventory」をご参照ください。
バケットに設定されたインベントリをリストするための API 操作の詳細については、「ListBucketInventory」をご参照ください。
バケットに設定されたインベントリを削除するための API 操作の詳細については、「DeleteBucketInventory」をご参照ください。