Este tópico descreve como criar um inventário para um bucket, além de consultar, listar e excluir inventários configurados.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
As credenciais de acesso neste tópico são obtidas de variáveis de ambiente. Para saber mais sobre a configuração de credenciais de acesso, consulte Configurar credenciais de acesso (Python SDK V1).
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Inicialização.
Certifique-se de ter permissões para criar, visualizar, listar e excluir inventários de um bucket. Por padrão, o proprietário do bucket tem permissão para executar essas operações. Caso não possua as permissões necessárias, solicite-as ao proprietário do bucket.
É possível configurar até 1.000 inventários por bucket.
O bucket de source, para o qual você deseja configurar um inventário, deve estar na mesma região do bucket de destino onde a lista de inventário será armazenada.
Crie um inventário para um bucket
O exemplo a seguir mostra como criar um inventário para um 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.ProviderAuthV4(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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# 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)
Visualize os inventários de um bucket
O exemplo a seguir mostra como consultar um inventário configurado para um 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.ProviderAuthV4(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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# 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.')
Listar vários inventários simultaneamente
É possível consultar até 100 inventários em uma única solicitação. Para consultar mais de 100 inventários, envie várias solicitações e utilize o token retornado em cada solicitação como parâmetro para a próxima.
O exemplo a seguir mostra como listar inventários configurados para um 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.ProviderAuthV4(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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# 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
Exclua um inventário de um bucket
O exemplo a seguir mostra como excluir um inventário configurado para um 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.ProviderAuthV4(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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the inventory name.
inventory_id = "inventory1"
# Delete the inventory.
bucket.delete_bucket_inventory_configuration(inventory_id)
Referências
Para obter o código de exemplo completo sobre gerenciamento de inventários de buckets, visite o GitHub.
Para obter mais informações sobre a operação de API destinada a criar um inventário para um bucket, consulte PutBucketInventory.
Para obter detalhes sobre a operação de API usada para consultar um inventário configurado para um bucket, consulte GetBucketInventory.
Para saber mais sobre a operação de API que lista inventários configurados para um bucket, consulte ListBucketInventory.
Para obter mais informações sobre a operação de API para excluir inventários configurados para um bucket, consulte DeleteBucketInventory.