当您在AWS S3中创建通知时,可能会遇到以下错误消息:“通知不支持该事件(服务:Amazon S3;状态码:400;错误码:InvalidArgument)”。这个错误通常是由于指定的事件类型不受支持引起的。以下是解决此问题的代码示例:
import boto3
def create_s3_notification(bucket_name, topic_arn):
s3 = boto3.client('s3')
# 指定要创建通知的S3存储桶
bucket_notification_configuration = {
'TopicConfigurations': [
{
'Id': 'example-notification',
'TopicArn': topic_arn,
'Events': ['s3:ObjectCreated:*']
}
]
}
# 创建S3通知
response = s3.put_bucket_notification_configuration(
Bucket=bucket_name,
NotificationConfiguration=bucket_notification_configuration
)
print(response)
# 指定要创建通知的S3存储桶名称和SNS主题ARN
bucket_name = 'your-bucket-name'
topic_arn = 'your-topic-arn'
# 调用函数创建S3通知
create_s3_notification(bucket_name, topic_arn)
请注意,上述示例假设您已经正确配置了AWS凭证,并且已经安装了Boto3库。在代码中,我们使用put_bucket_notification_configuration
方法来创建S3通知。在Events
参数中,我们指定了要监听的事件类型,例如s3:ObjectCreated:*
表示监听对象创建事件。根据您的需求,您可以更改事件类型以满足您的需求。
确保将bucket_name
和topic_arn
替换为您自己的存储桶名称和SNS主题ARN。
如果您仍然遇到相同的错误,请检查您指定的事件类型是否受到支持,并确保您具有正确的权限来创建通知。