Todos los productos
Search
Centro de documentos

Object Storage Service:Gestione inventarios de buckets con OSS SDK for Python 1.0

Última actualización: Sep 22, 2026

Este tema describe cómo crear un inventario para un bucket, así como consultar, listar y eliminar los inventarios configurados en él.

Notas

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • Las credenciales de acceso de este tema se obtienen de variables de entorno. Para conocer cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para otras configuraciones, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Asegúrese de contar con permisos para crear, consultar, listar y eliminar inventarios de un bucket. De forma predeterminada, el propietario del bucket tiene permisos para ejecutar estas operaciones. Si no cuenta con ellos, solicite al propietario del bucket que se los otorgue.

  • Se pueden configurar hasta 1,000 inventarios por bucket.

  • El bucket de origen, donde desea configurar el inventario, debe estar en la misma región que el bucket de destino en el que se almacenará la lista de inventario.

    • Cree un inventario para un bucket

      A continuación se muestra un ejemplo de código para crear un inventario en un 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)

      Consulte los inventarios de un bucket

      A continuación se muestra un ejemplo de código para consultar un inventario configurado en un 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.')

      Liste varios inventarios a la vez

      Nota

      Es posible consultar hasta 100 inventarios en una sola solicitud. Si necesita consultar más de 100, envíe varias solicitudes y utilice el token devuelto en cada una como parámetro de la siguiente.

      A continuación se muestra un ejemplo de código para listar los inventarios configurados en un 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

      Elimine un inventario de un bucket

      A continuación se muestra un ejemplo de código para eliminar un inventario configurado en un 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)

      Referencias

      • Para consultar el código de ejemplo completo sobre la gestión de inventarios de buckets, visite GitHub.

      • Para obtener más información sobre la operación de API que permite crear un inventario para un bucket, consulte PutBucketInventory.

      • Si requiere detalles sobre la operación de API para consultar un inventario configurado en un bucket, consulte GetBucketInventory.

      • La operación de API correspondiente al listado de inventarios configurados en un bucket se documenta en ListBucketInventory.

      • Para conocer la operación de API que elimina los inventarios configurados en un bucket, consulte DeleteBucketInventory.