本文档介绍如何添加、查看、列举和删除存储空间(Bucket)的清单(Inventory)配置。
说明 请确保您拥有调用以上操作的权限。Bucket所有者默认拥有此类权限,若您无此类权限,请先向Bucket所有者申请对应操作的权限。
添加清单配置
说明
- 单个Bucket最多只能有1000条清单配置。
- 配置清单的源Bucket与存放导出的清单文件所在的目标Bucket必须位于同一个Region。
以下代码用于为某个Bucket添加清单(Inventory)配置:
# -*- coding: utf-8 -*-
import oss2
from oss2.models import (InventoryConfiguration,
InventoryFilter,
InventorySchedule,
InventoryDestination,
InventoryBucketDestination,
InventoryServerSideEncryptionKMS,
InventoryServerSideEncryptionOSS,
INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
INVENTORY_INCLUDED_OBJECT_VERSIONS_ALL,
INVENTORY_FREQUENCY_DAILY,
INVENTORY_FREQUENCY_WEEKLY,
INVENTORY_FORMAT_CSV,
FIELD_SIZE,
FIELD_LAST_MODIFIED_DATE,
FIELD_STORAG_CLASS,
FIELD_ETAG,
FIELD_IS_MULTIPART_UPLOADED,
FIELD_ENCRYPTION_STATUS)
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
account_id = '<yourtBucketDestinationAccountId>'
role_arn = '<yourBucketDestinationRoleArn>'
dest_bucket_name = '<yourDestinationBucketName>'
# 设置清单配置id。
inventory_id = "test-config-id"
# 清单中包含的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的用户account_id。
account_id=account_id,
# 目的地bucket的role_arn。
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"),
# 设置清单中包含的object属性。
optional_fields=optional_fields,
# 设置清单的接收目的地。
inventory_destination=InventoryDestination(bucket_destination=bucket_destination))
# 上传清单配置。
bucket.put_bucket_inventory_configuration(inventory_configuration)
有关添加存储空间清单配置的详情,请参见PutBucketInventory。
查看清单配置
以下代码用于查看某个Bucket的清单配置:
# -*- coding: utf-8 -*-
import oss2
import os
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 设置清单配置id。
inventory_id = "test-config-id"
# 获取清单配置。
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.')
有关查看清单配置的详情,请参见GetBucketInventory。
批量列举清单配置
说明 单次请求最多可获取100条清单配置项内容。若需获取超过100条清单配置项,则需发送多次请求,并保留相应的token,作为下一次请求的参数。
以下代码用于批量列举某个Bucket的清单配置:
# -*- coding: utf-8 -*-
import oss2
import os
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 打印清单配置信息。
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
有关批量列举清单配置的详情,请参见ListBucketInventory。
删除清单配置
以下代码用于删除某个Bucket的清单配置:
# -*- coding: utf-8 -*-
import oss2
import os
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 设置清单配置id。
inventory_id = "test-config-id"
# 删除指定id的清单配置。
bucket.delete_bucket_inventory_configuration(inventory_id)
有关删除存储空间清单配置的详情,请参见DeleteBucketInventory。
在文档使用中是否遇到以下问题
更多建议
匿名提交