本文介绍如何通过Python SDK V2创建、查看、更新和删除存储空间(Bucket)的元数据表配置,包括日志表(Journal Table)配置和清单表(Inventory Table)配置。
功能说明
元数据管理(Metadata)为存储空间(Bucket)提供元数据表配置能力,包含以下两类配置:
日志表配置(JournalTableConfiguration):通过
RecordExpiration控制日志记录的开启状态(Expiration取值ENABLED或DISABLED)及记录保留天数(Days)。清单表配置(InventoryTableConfiguration):通过
ConfigurationState控制清单表的启用状态(取值ENABLED或DISABLED),并可通过EncryptionConfiguration指定加密算法(SseAlgorithm)。
注意事项
本文以从环境变量读取访问凭证为例。使用前请确保已配置访问凭证(AccessKey)。
请确保您拥有对目标Bucket执行元数据表配置操作的权限。Bucket所有者默认拥有此类权限,如果您无此类权限,请先向Bucket所有者申请对应操作的权限。
您也可以通过OSS控制台开启、修改或关闭元数据管理功能。
示例代码
创建元数据表配置
以下代码用于为某个Bucket创建元数据表配置(同时设置日志表和清单表):
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="create bucket metadata configuration sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.create_bucket_metadata_configuration(oss.CreateBucketMetadataConfigurationRequest(
bucket=args.bucket,
metadata_configuration=oss.MetadataConfiguration(
journal_table_configuration=oss.JournalTableConfiguration(
record_expiration=oss.MetadataTableRecordExpiration(
expiration='ENABLED',
days=30,
),
),
inventory_table_configuration=oss.InventoryTableConfiguration(
configuration_state='DISABLED',
),
),
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()查看元数据表配置
以下代码用于查看某个Bucket的元数据表配置:
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="get bucket metadata configuration sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.get_bucket_metadata_configuration(oss.GetBucketMetadataConfigurationRequest(
bucket=args.bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' metadata configuration: {result.metadata_configuration_result},'
)
if __name__ == "__main__":
main()更新清单表(Inventory Table)配置
以下代码用于更新某个Bucket的清单表配置:
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="update bucket metadata inventory table configuration sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--state', help='The state of the inventory metadata table. Valid values: ENABLED, DISABLED.', choices=['ENABLED', 'DISABLED'], default='ENABLED')
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.update_bucket_metadata_inventory_table_configuration(oss.UpdateBucketMetadataInventoryTableConfigurationRequest(
bucket=args.bucket,
inventory_table_configuration=oss.InventoryTableConfiguration(
configuration_state=args.state,
),
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()更新日志表(Journal Table)配置
以下代码用于更新某个Bucket的日志表配置:
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="update bucket metadata journal table configuration sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--expiration', help='The expiration state of journal table records. Valid values: ENABLED, DISABLED.', choices=['ENABLED', 'DISABLED'], default='ENABLED')
parser.add_argument('--days', help='The number of days to retain journal records. The minimum value is 7.', type=int, default=30)
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.update_bucket_metadata_journal_table_configuration(oss.UpdateBucketMetadataJournalTableConfigurationRequest(
bucket=args.bucket,
journal_table_configuration=oss.JournalTableConfiguration(
record_expiration=oss.MetadataTableRecordExpiration(
expiration=args.expiration,
days=args.days if args.expiration == 'ENABLED' else None,
),
),
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()删除元数据表配置
以下代码用于删除某个Bucket的元数据表配置:
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="delete bucket metadata configuration sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.delete_bucket_metadata_configuration(oss.DeleteBucketMetadataConfigurationRequest(
bucket=args.bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()相关文档
创建元数据表配置示例代码,请参见 create_bucket_metadata_configuration.py。
查看元数据表配置示例代码,请参见 get_bucket_metadata_configuration.py。
更新清单表配置示例代码,请参见 update_bucket_metadata_inventory_table_configuration.py。
更新日志表配置示例代码,请参见 update_bucket_metadata_journal_table_configuration.py。
删除元数据表配置示例代码,请参见 delete_bucket_metadata_configuration.py。