本文档介绍如何添加、查看、列举和删除存储空间(Bucket)的清单(Inventory)配置。
说明 请确保您拥有调用以上操作的权限。Bucket所有者默认拥有此类权限,若您无此类权限,请先向Bucket所有者申请对应操作的权限。
添加清单配置
说明
- 单个Bucket最多只能有1000条清单配置。
- 配置清单的源Bucket与存放导出的清单文件所在的目标Bucket必须位于同一个Region。
以下代码用于为某个Bucket添加清单(Inventory)配置:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
String destBucketName ="<yourDestinationBucketName>";
String accountId ="<yourDestinationBucketAccountId>";
String roleArn ="<yourDestinationBucketRoleArn>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 创建清单配置。
InventoryConfiguration inventoryConfiguration = new InventoryConfiguration();
// 设置清单配置id。
String inventoryId = "testid";
inventoryConfiguration.setInventoryId(id);
// 设置清单中包含的object属性。
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对应每周一次,Daily对应每天一次。
inventoryConfiguration.setSchedule(new InventorySchedule().withFrequency(InventoryFrequency.Weekly));
// 设置清单中包含的object的版本为当前版本。如果设置为InventoryIncludedObjectVersions.All则表示object的所有版本,在版本控制状态下生效。
inventoryConfiguration.setIncludedObjectVersions(InventoryIncludedObjectVersions.Current);
// 清单配置是否启用的标识, true或false。
inventoryConfiguration.setEnabled(true);
// 设置清单筛选规则,指定筛选object的前缀。
InventoryFilter inventoryFilter = new InventoryFilter().withPrefix("obj-prefix");
inventoryConfiguration.setInventoryFilter(inventoryFilter);
// 创建清单的bucket目的地配置。
InventoryOSSBucketDestination ossInvDest = new InventoryOSSBucketDestination();
// 设置产生清单结果的存储路径前缀。
ossInvDest.setPrefix("destination-prefix");
// 设置清单格式。
ossInvDest.setFormat(InventoryFormat.CSV);
// 目的地bucket的用户accountId。
ossInvDest.setAccountId(accountId);
// 目的地bucket的roleArn。
ossInvDest.setRoleArn(roleArn);
// 目的地bucket的名称。
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);
// 关闭ossClient。
ossClient.shutdown();
有关添加存储空间清单配置的详情,请参见PutBucketInventory。
查看清单配置
以下代码用于查看某个Bucket的清单配置:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String inventoryId = "<yourInventoryConfigurationId>";
// 查看指定id的清单配置信息。
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.");
}
}
// 关闭ossClient。
ossClient.shutdown();
有关查看清单配置的详情,请参见GetBucketInventory。
批量列举清单配置
说明 单次请求最多可获取100条清单配置项内容。若需获取超过100条清单配置项,则需发送多次请求,并保留相应的Token,作为下一次请求的参数。
以下代码用于批量列举某个Bucket的清单配置:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 列举清单配置项。默认每次最多列举100条结果,如果配置超过100条,结果将会分页返回,通过传入Token方式列举下一页。
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;
}
}
// 关闭ossClient。
ossClient.shutdown();
有关批量列举清单配置的详情,请参见ListBucketInventory。
删除清单配置
以下代码用于删除某个Bucket的清单配置:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String inventoryId = "<yourInventoryConfigurationId>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 删除指定id的清单配置。
ossClient.deleteBucketInventoryConfiguration(bucketName, inventoryId);
// 关闭ossClient。
ossClient.shutdown();
有关删除存储空间清单配置的详情,请参见DeleteBucketInventory。
在文档使用中是否遇到以下问题
更多建议
匿名提交