如何使用 Boto3 和 AWS 客户端获取 S3 存储桶的通知配置详细信息?
问题陈述- 使用 Python 中的 boto3 库获取 S3 存储桶的通知配置。比如在S3中找到Bucket_1的通知配置细节。
解决这个问题的方法/算法
步骤 1 - 导入 boto3 和 botocore 异常以处理异常。
步骤 2 - 使用 bucket_name 作为函数中的参数。
步骤 3 - 使用 boto3 库创建 AWS 会话。
第 4 步- 为 S3 创建一个 AWS 客户端。
第 5 步- 现在使用函数get_bucket_notification_configuration并传递存储桶名称。
步骤 6 - 它返回包含有关 S3 的详细信息的字典。如果未设置通知,则返回 NONE
步骤 7 - 如果在删除文件时出现问题,请处理通用异常。
示例
使用以下代码获取通知配置详细信息 -
import boto3输出结果frombotocore.exceptionsimport ClientError
def get_bucket_notificationconfiguration_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_notification_configuration(Bucket=bucket_name,)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_notificationconfiguration_of_s3: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in get_bucket_notificationconfiguration_of_s3 function: " + e.__str__())
return result
print(get_bucket_notificationconfiguration_of_s3("Bucket_1"))
{'TopicConfigurations': [
{
'Id': 'string',
'TopicArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated: Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
],
'QueueConfigurations': [
{
'Id': 'string',
'QueueArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated:
Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
],
'LambdaFunctionConfigurations': [
{
'Id': 'string',
'LambdaFunctionArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated:
Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
]
}
注意:此输出取决于存储桶的设置/权限。如果某些设置/权限为默认设置或未设置,则忽略结果。同样,如果未为存储桶设置通知,则返回 NONE。
以上是 如何使用 Boto3 和 AWS 客户端获取 S3 存储桶的通知配置详细信息? 的全部内容, 来源链接: utcz.com/z/358024.html