如何使用 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
from botocore.exceptions import 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。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP