如何使用 Boto3 和 AWS 客户端获取 S3 存储桶的所有权控制详细信息?
问题陈述-使用Python中的boto3库获取S3存储桶的所有权控制详细信息。
例如,在S3中查找Bucket_1的所有权控制详细信息。
解决这个问题的方法/算法
步骤1-导入boto3和botocore异常以处理异常。
步骤2-使用bucket_name作为函数中的参数。
步骤3-使用boto3库创建AWS会话。
第4步-为S3创建一个AWS客户端。
第5步-现在使用函数get_bucket_ownership_controls并传递存储桶名称。
步骤6-它返回包含有关S3的详细信息的字典。
步骤7-如果在删除文件时出现问题,请处理通用异常。
示例
使用以下代码获取存储桶的所有权详细信息-
import boto3
frombotocore.exceptionsimport ClientError
def get_bucket_ownership_control_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_ownership_controls(Bucket=bucket_name,)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_ownership_control_of_s3: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in get_bucket_ownership_control_of_s3: " + e.__str__())
return result
print(get_bucket_ownership_control_of_s3("Bucket_1"))输出结果{
'OwnershipControls': {
'Rules': [
{
'ObjectOwnership': 'BucketOwnerPreferred'|'ObjectWriter'
},
]
}
}热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下