如何使用Boto3和AWS客户端获取S3存储桶的所有权控制详情?

如何使用Boto3和AWS客户端获取S3存储桶的所有权控制详情?

问题描述 − 使用Python中的boto3库,获取S3存储桶的所有权控制详情。

例如,在S3中查找Bucket_1的所有权控制详情。

阅读更多:Python 教程

解决此问题的方法/算法

步骤1 − 导入boto3和botocore异常以处理异常。

步骤2 − 使用bucket_name作为函数的参数。

步骤3 − 使用boto3库创建AWS会话。

步骤4 − 为S3创建AWS客户端。

步骤5 − 现在使用函数get_bucket_ownership_controls并传递存储桶名称。

步骤6 − 它返回包含有关S3的详细信息的字典。

步骤7 − 处理通用异常,如果在删除文件时发生了错误。

示例

使用以下代码获取桶的所有权详情 −

import boto3
from botocore.exceptions import 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'
         },
      ]
   }
}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程