Amazon S3允许在一个TopicConfigurations下配置多个事件,这需要使用JSON格式的配置,并将其作为请求体发送给S3 API。以下是一个示例代码:
import boto3
import json
client = boto3.client('s3')
bucket_name = 'example-bucket'
topic_arn = 'arn:aws:sns:us-east-1:123456789012:my-sns-topic'
event_prefix = 'prefix1'
event_suffix = 'suffix1'
event_types = ['s3:ObjectCreated:*', 's3:ObjectRemoved:*']
response = client.put_bucket_notification_configuration(
Bucket=bucket_name,
NotificationConfiguration={
'TopicConfigurations': [
{
'TopicArn': topic_arn,
'Events': event_types,
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix',
'Value': event_prefix
},
{
'Name': 'suffix',
'Value': event_suffix
}
]
}
}
},
{
'TopicArn': topic_arn,
'Events': ['s3:ObjectDeleted:*']
}
]
}
)
print(response)
在上面的示例中,我们为同一个主题(topic_arn)配置了两个事件,其中一个有一个键过滤器,另一个没有。在这种情况下,S3将向SNS主题发送包含所述事件的通知。
可以根据需要向此示例添加更多事件,并在TopicConfigurations下添加相应的JSON元素。