全部产品
Search
文档中心

对象存储 OSS:Python存储空间清单

更新时间:Dec 20, 2023

本文介绍如何添加、查看、批量列举和删除存储空间(Bucket)的清单(Inventory)配置。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见访问域名和数据中心

  • 本文以从环境变量读取访问凭证为例。如何配置访问凭证,请参见配置访问凭证

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见初始化

  • 请确保您拥有调用添加、查看、列举和删除存储空间清单配置的权限。Bucket所有者默认拥有此类权限,如果您无此类权限,请先向Bucket所有者申请对应操作的权限。

  • 单个Bucket最多只能有1000条清单配置。

  • 配置清单的源Bucket与存放导出的清单文件所在的目标Bucket必须位于同一个Region。

添加清单配置

以下代码用于为某个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)

# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 填写Bucket所有者授予的账户ID。例如:1283641033516515
account_id = 'yourtBucketDestinationAccountId'
# 填写具有读取源Bucket所有文件和向目标Bucket写入文件权限的角色名称。例如:acs:ram::1283641033516515:role/AliyunOSSRole
role_arn = 'yourBucketDestinationRoleArn'
# 填写存放清单结果的Bucket名称。
dest_bucket_name = 'yourDestinationBucketName'

# 设置清单规则名称。
inventory_id = "inventory1"

# 设置清单结果中包含的Object属性。
optional_fields = [FIELD_SIZE, FIELD_LAST_MODIFIED_DATE, FIELD_STORAG_CLASS,
                   FIELD_ETAG, FIELD_IS_MULTIPART_UPLOADED, FIELD_ENCRYPTION_STATUS]

# 创建存放清单文件的目标Bucket配置。
bucket_destination = InventoryBucketDestination(
    # 目标Bucket的用户accountId。
    account_id=account_id,
    # 目标Bucket的roleArn。
    role_arn=role_arn,
    # 目标Bucket的名称。
    bucket=dest_bucket_name,
    # 指定清单格式。
    inventory_format=INVENTORY_FORMAT_CSV,
    # 清单结果的存储路径前缀。
    prefix='destination-prefix',
    # 如果需要使用KMS加密清单,请参考如下设置。
    # sse_kms_encryption=InventoryServerSideEncryptionKMS("test-kms-id"),
    # 如果需要使用OSS服务端加密清单,请参考如下设置。
    # sse_oss_encryption=InventoryServerSideEncryptionOSS()
)

# 创建清单配置。
inventory_configuration = InventoryConfiguration(
    # 设置清单的配置id。
    inventory_id=inventory_id,
    # 清单配置是否启用的标识, true或false。
    is_enabled=True,
    # 设置清单的生成计划,以下示例为每天一次。其中,WEEKLY对应每周一次,DAILY对应每天一次。
    inventory_schedule=InventorySchedule(frequency=INVENTORY_FREQUENCY_DAILY),
    # 设置清单中包含的object的版本为当前版本。如果设置为INVENTORY_INCLUDED_OBJECT_VERSIONS_ALL则表示object的所有版本,在版本控制状态下生效。
    included_object_versions=INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
    # 设置清单清筛选object的前缀。
    # inventory_filter=InventoryFilter(prefix="obj-prefix"),
    # 设置清单清筛选条件。假如筛选文件最后修改时间的起始时间戳为1637883649,
    inventory_filter=InventoryFilter(
        # 筛选规则的匹配前缀。
        "obj-prefix",
        # 筛选文件最后修改时间的起始时间戳,单位为秒。
        1637883649,
        # 筛选文件最后修改时间的终止时间戳,单位为秒。
        1638347592,
        # 筛选文件的最小大小,单位为B。
        1024,
        # 筛选文件的最大大小,单位为B。
        1048576,
        # 筛选文件的存储类型,支持指定多种存储类型。
        'Standard,IA'),
    # 设置清单中包含的object属性。
    optional_fields=optional_fields,    
    inventory_destination=InventoryDestination(bucket_destination=bucket_destination))

# 上传清单配置。
result = bucket.put_bucket_inventory_configuration(inventory_configuration)
print(result.status)

查看清单配置

以下代码用于查看某个Bucket的清单配置:

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

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 填写清单规则名称。
inventory_id = "inventory1"

# 获取清单配置。
result = bucket.get_bucket_inventory_configuration(inventory_id=inventory_id);

# 打印清单配置信息。
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.')

批量列举清单配置

说明

单次请求最多可获取100条清单配置项内容。若需获取超过100条清单配置项,则需发送多次请求,并保留相应的Token,作为下一次请求的参数。

以下代码用于批量列举某个Bucket的清单配置:

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

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 打印清单配置信息。
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.')

# 列举所有的清单配置。
# 如果存在超过100条配置,列举结果将会分页,分页信息保存在class: <oss2.models.ListInventoryConfigurationResult>中。
continuation_token = None
while 1:
    result = bucket.list_bucket_inventory_configurations(continuation_token=continuation_token)
    # 本次列举结果是否分页。
    print('is truncated', result.is_truncated)
    # 本次列举携带的token。
    print('continuaiton_token', result.continuaiton_token)
    # 下次列举需要携带的token。
    print('next_continuation_token', result.next_continuation_token)

    # 打印清单配置信息。
    for inventory_config in result.inventory_configurations:
        print_inventory_configuration(inventory_config)

    # 如果本次列举为分页列举,则继续列举,且需要携带分页token。
    if result.is_truncated:
        continuation_token = result.next_continuation_token
    else:
         break

删除清单配置

以下代码用于删除某个Bucket的清单配置:

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

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 填写清单规则名称。
inventory_id = "inventory1"

# 删除清单配置。
bucket.delete_bucket_inventory_configuration(inventory_id)

相关文档