Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách lấy cấu hình thông báo bucket S3 bằng Boto3 và AWS Client

Đề bài − Sử dụng thư viện boto3 trong Python để lấy cấu hình thông báo (notification configuration) của bucket S3. Ví dụ: tìm chi tiết cấu hình thông báo của Bucket_1 trong S3.

Cách tiếp cận/Thuật toán giải quyết

Bước 1 − Nhập boto3 và botocore exceptions để xử lý ngoại lệ.

Bước 2 − Dùng bucket_name làm tham số trong hàm.

Bước 3 − Tạo phiên AWS bằng boto3.

Bước 4 − Tạo client AWS cho S3.

Bước 5 − Dùng hàm get_bucket_notification_configuration và truyền tên bucket.

Bước 6 − Trả về dictionary chứa chi tiết về S3. Nếu không đặt thông báo, trả về NONE.

Bước 7 − Xử lý ngoại lệ chung nếu có lỗi khi xóa file.

Ví dụ

Sử dụng code sau để lấy chi tiết cấu hình thông báo:

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"))

Kết quả

{
    'TopicConfigurations': [
        {
            'Id': 'string',
            'TopicArn': 'string',
            'Events': [
                's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated:Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:CompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRestore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replication:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicateAfterThreshold',
            ],
            '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:CompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRestore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replication:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicateAfterThreshold',
            ],
            '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:CompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRestore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replication:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicateAfterThreshold',
            ],
            'Filter': {
                'Key': {
                    'FilterRules': [
                        {
                            'Name': 'prefix'|'suffix',
                            'Value': 'string'
                        },
                    ]
                }
            }
        },
    ]
}

Lưu ý: Kết quả này tùy thuộc vào cài đặt/quyền của bucket. Nó bỏ qua kết quả nếu một số cài đặt/quyền là mặc định hoặc không được đặt. Tương tự, nếu không đặt thông báo cho bucket thì trả về NONE.