All Products
Search
Document Center

Object Storage Service:Bucket inventories

Last Updated:Aug 21, 2023

This topic describes how to create an inventory for a bucket and how to query, list, and delete the inventories configured for a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • Make sure that you have the permissions to create, view, list, and delete inventories for a bucket. By default, the bucket owner has the permissions to perform the preceding operations. If you do not have the permissions to perform the preceding operations, apply for the permissions from the bucket owner.
  • You can configure up to 1,000 inventories for a bucket.
  • You must deploy the source bucket for which you want to configure an inventory in the same region as the destination bucket in which the inventory list is stored.

Create an inventory for a bucket

The following code provides an example on how to create an inventory for a bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import (InventoryConfiguration,
                         InventoryFilter,
                         InventorySchedule,
                         InventoryDestination,
                         InventoryBucketDestination,
                         INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
                         INVENTORY_FREQUENCY_DAILY,
                         INVENTORY_FORMAT_CSV,
                         FIELD_SIZE,
                         FIELD_LAST_MODIFIED_DATE,
                         FIELD_STORAG_CLASS,
                         FIELD_ETAG,
                         FIELD_IS_MULTIPART_UPLOADED,
                         FIELD_ENCRYPTION_STATUS)

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Specify the ID of the account to which the bucket owner grants permissions to perform the operation. Example: 1283641033516515.
account_id = 'yourtBucketDestinationAccountId'
# Specify the name of the RAM role that is granted permissions to read all objects in the source bucket for which you want to configure the inventory and permissions to write data to the destination bucket in which you want to store the generated inventory lists. Example: acs:ram::1283641033516515:role/AliyunOSSRole.
role_arn = 'yourBucketDestinationRoleArn'
# Specify the name of the bucket in which you want to store the generated inventory lists. 
dest_bucket_name = 'yourDestinationBucketName'

# Specify the inventory name. 
inventory_id = "inventory1"

# Specify the object attributes that are included in the inventory lists. 
optional_fields = [FIELD_SIZE, FIELD_LAST_MODIFIED_DATE, FIELD_STORAG_CLASS,
                   FIELD_ETAG, FIELD_IS_MULTIPART_UPLOADED, FIELD_ENCRYPTION_STATUS]

# Create the bucket in which you want to store the generated inventory lists. 
bucket_destination = InventoryBucketDestination(
    # Specify the ID of the account to which the destination bucket belongs. 
    account_id=account_id,
    # Specify the ARN of the RAM role that you want to use to access the destination bucket. 
    role_arn=role_arn,
    # Specify the name of the destination bucket. 
    bucket=dest_bucket_name,
    # Specify the format of the inventory lists. 
    inventory_format=INVENTORY_FORMAT_CSV,
    # Specify the prefix of the path in which you want to store the generated inventory lists. 
    prefix='destination-prefix',
    # The following code provides an example on how to encrypt inventory lists by using customer master keys (CMKs) that are managed by Key Management Service (KMS). 
    # sse_kms_encryption=InventoryServerSideEncryptionKMS("test-kms-id"),
    # The following code provides an example on how to encrypt the inventory lists on the OSS server. 
    # sse_oss_encryption=InventoryServerSideEncryptionOSS()
)

# Create an inventory. 
inventory_configuration = InventoryConfiguration(
    # Specify the ID of the inventory. 
    inventory_id=inventory_id,
    # Specify whether to enable the inventory for the bucket. Valid values: true and false. 
    is_enabled=True,
    # Specify whether to generate inventory lists on a daily or weekly basis. The following code provides an example on how to generate inventory lists on a daily basis. A value of Weekly indicates that inventory lists are generated on a weekly basis. A value of Daily indicates that inventory lists are generated on a daily basis. 
    inventory_schedule=InventorySchedule(frequency=INVENTORY_FREQUENCY_DAILY),
    # Specify that inventory lists include only the current versions of objects. If you set the INVENTORY_INCLUDED_OBJECT_VERSIONS parameter to ALL, all versions of objects are included in inventory lists. This inventory takes effect only when versioning is enabled for the source bucket. 
    included_object_versions=INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
    # Specify the name prefix of objects that you want to include in inventory lists. 
    # inventory_filter=InventoryFilter(prefix="obj-prefix"),
    # Specify conditions to filter inventory lists. For example, the beginning of the time range during which the object was last modified is 1637883649. 
    inventory_filter=InventoryFilter(
        # Specify the prefix that is used to filter inventories. 
        "obj-prefix",
        # Specify the beginning of the time range during which the object was last modified. Unit: seconds. 
        1637883649,
        # Specify the end of the time range during which the object was last modified. Unit: seconds. 
        1638347592,
        # Specify the minimum size of the object. Unit: B. 
        1024,
        # Specify the maximum size of the object. Unit: B. 
        1048576,
        # Specify the storage classes of objects. 
        'Standard,IA'),
    # Specify the object attributes that you want to include in inventory lists. 
    optional_fields=optional_fields,    
    inventory_destination=InventoryDestination(bucket_destination=bucket_destination))

# Apply the inventory to the bucket. 
result = bucket.put_bucket_inventory_configuration(inventory_configuration)
print(result.status)

Query the inventories of a bucket

The following code provides an example on how to query an inventory configured for a bucket:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Specify the inventory name. 
inventory_id = "inventory1"

# Query the inventory. 
result = bucket.get_bucket_inventory_configuration(inventory_id=inventory_id);

# Display information about the inventory. 
print('======inventory configuration======')
print('inventory_id', result.inventory_id)
print('is_enabled', result.is_enabled)
print('frequency', result.inventory_schedule.frequency)
print('included_object_versions', result.included_object_versions)
print('inventory_filter prefix', result.inventory_filter.prefix)
print('fields', result.optional_fields)
bucket_destin = result.inventory_destination.bucket_destination
print('===bucket destination===')
print('account_id', bucket_destin.account_id)
print('role_arn', bucket_destin.role_arn)
print('bucket', bucket_destin.bucket)
print('format', bucket_destin.inventory_format)
print('prefix', bucket_destin.prefix)
if bucket_destin.sse_kms_encryption is not None:
    print('server side encryption by kms, key id:', bucket_destin.sse_kms_encryption.key_id)
elif bucket_destin.sse_oss_encryption is not None:
    print('server side encryption by oss.')

List multiple inventories at a time

Note You can query up to 100 inventories in a single request. If you want to query more than 100 inventories, send multiple requests and use the token returned for each request as a parameter for the next request.

The following code provides an example on how to list inventories configured for a bucket:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Display information about the inventories. 
def print_inventory_configuration(configuration):
    print('======inventory configuration======')
    print('inventory_id', configuration.inventory_id)
    print('is_enabled', configuration.is_enabled)
    print('frequency', configuration.inventory_schedule.frequency)
    print('included_object_versions', configuration.included_object_versions)
    print('inventory_filter prefix', configuration.inventory_filter.prefix)
    print('fields', configuration.optional_fields)
    bucket_destin = configuration.inventory_destination.bucket_destination
    print('===bucket destination===')
    print('account_id', bucket_destin.account_id)
    print('role_arn', bucket_destin.role_arn)
    print('bucket', bucket_destin.bucket)
    print('format', bucket_destin.inventory_format)
    print('prefix', bucket_destin.prefix)
    if bucket_destin.sse_kms_encryption is not None:
        print('server side encryption by kms, key id:', bucket_destin.sse_kms_encryption.key_id)
    elif bucket_destin.sse_oss_encryption is not None:
        print('server side encryption by oss.')

# List all inventories. 
# If the number of inventories exceeds 100, the results are returned by page. The pagination information is stored in the <oss2.models.ListInventoryConfigurationResult> class. 
continuation_token = None
while 1:
    result = bucket.list_bucket_inventory_configurations(continuation_token=continuation_token)
    # Obtain whether the results are listed by page. 
    print('is truncated', result.is_truncated)
    # Obtain the token to carry in this list. 
    print('continuaiton_token', result.continuaiton_token)
    # Obtain the token to carry in the subsequent list. 
    print('next_continuation_token', result.next_continuation_token)

    # Display information about the inventories. 
    for inventory_config in result.inventory_configurations:
        print_inventory_configuration(inventory_config)

    # If the results are listed by page, continue the listing. The subsequent list must carry the continuation token. 
    if result.is_truncated:
        continuation_token = result.next_continuation_token
    else:
         break

Delete an inventory of a bucket

The following code provides an example on how to delete an inventory configured for a bucket:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Specify the inventory name. 
inventory_id = "inventory1"

# Delete the inventory. 
bucket.delete_bucket_inventory_configuration(inventory_id)

References

  • For the complete sample code for managing bucket inventories, visit GitHub.

  • For more information about the API operation for creating an inventory for a bucket, see PutBucketInventory.

  • For more information about the API operation for querying an inventory configured for a bucket, see GetBucketInventory.

  • For more information about the API operation for listing inventories configured for a bucket, see ListBucketInventory.

  • For more information about the API operation for deleting the inventories configured for a bucket, see DeleteBucketInventory.