Bucket inventory (Java SDK V2)
Manage bucket inventories by calling the PutBucketInventory, GetBucketInventory, ListBucketInventory, and DeleteBucketInventory operations with OSS Java SDK V2.
Prerequisites
OSS Java SDK V2 installed. If you use Maven, add the dependency
com.aliyun:alibabacloud-oss-v2to the pom.xml file.Access credentials configured by using environment variables. The sample code in this topic uses
EnvironmentVariableCredentialsProviderto read credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables.
Permissions
An Alibaba Cloud account has all permissions by default. RAM users and RAM roles created under the account have no permissions by default. The Alibaba Cloud account or an account administrator must grant the required permissions.
API | Action | Description |
PutBucketInventory |
| Sets, queries, lists, or deletes bucket inventory configurations. |
Sample code
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import java.util.Arrays;
public class BucketInventorySample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
String bucket = "examplebucket";
String inventoryId = "report1";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
// Set the inventory configuration
InventoryDestination destination = InventoryDestination.newBuilder()
.ossBucketDestination(InventoryOSSBucketDestination.newBuilder()
.format("CSV")
.bucket("acs:oss:::examplebucket")
.prefix("inventory-result/")
.accountId("100000000000000")
.roleArn("acs:ram::100000000000000:role/AliyunOSSRole")
.build())
.build();
InventoryConfiguration inventoryConfig = InventoryConfiguration.newBuilder()
.id(inventoryId)
.isEnabled(true)
.includedObjectVersions("All")
.schedule(InventorySchedule.newBuilder().frequency("Weekly").build())
.filter(InventoryFilter.newBuilder().prefix("docs/").build())
.destination(destination)
.optionalFields(OptionalFields.newBuilder()
.fields(Arrays.asList("Size", "LastModifiedDate", "StorageClass"))
.build())
.build();
PutBucketInventoryResult putResult = client.putBucketInventory(
PutBucketInventoryRequest.newBuilder()
.bucket(bucket)
.inventoryId(inventoryId)
.inventoryConfiguration(inventoryConfig)
.build());
System.out.printf("PutBucketInventory status code:%d%n", putResult.statusCode());
// Query the inventory configuration
GetBucketInventoryResult getResult = client.getBucketInventory(
GetBucketInventoryRequest.newBuilder()
.bucket(bucket)
.inventoryId(inventoryId)
.build());
System.out.printf("Inventory ID: %s, Enabled: %s%n",
getResult.inventoryConfiguration().id(),
getResult.inventoryConfiguration().isEnabled());
// List the inventory configurations
ListBucketInventoryResult listResult = client.listBucketInventory(
ListBucketInventoryRequest.newBuilder().bucket(bucket).build());
for (InventoryConfiguration cfg : listResult.listInventoryConfigurationsResult().inventoryConfigurations()) {
System.out.printf(" Inventory: %s%n", cfg.id());
}
// Delete the inventory configuration
DeleteBucketInventoryResult delResult = client.deleteBucketInventory(
DeleteBucketInventoryRequest.newBuilder()
.bucket(bucket)
.inventoryId(inventoryId)
.build());
System.out.printf("DeleteBucketInventory status code:%d%n", delResult.statusCode());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For sample code in synchronous mode, see PutBucketInventory.java.
For sample code in asynchronous mode, see PutBucketInventoryAsync.java.
For sample code in synchronous mode, see GetBucketInventory.java.
For sample code in asynchronous mode, see GetBucketInventoryAsync.java.
For sample code in synchronous mode, see ListBucketInventory.java.
For sample code in asynchronous mode, see ListBucketInventoryAsync.java.
For sample code in synchronous mode, see DeleteBucketInventory.java.
For sample code in asynchronous mode, see DeleteBucketInventoryAsync.java.