Bucket inventory (Java SDK V2)

Updated at:

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-v2 to the pom.xml file.

  • Access credentials configured by using environment variables. The sample code in this topic uses EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment 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

oss: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