【问题标题】:How yo APPEND a AWS S3 bucket Notification Configuration如何追加 AWS S3 存储桶通知配置
【发布时间】:2019-11-02 12:22:15
【问题描述】:

尝试将新通知附加到存储桶。在互联网上找不到任何示例。

我需要将 ObjectCreated 事件发送到 SQS。我需要决定事件由前缀发送到哪个队列。因此,每个通知在同一个存储桶上都有不同的队列和前缀。

问题是我无法附加新通知。我只是覆盖了之前在存储桶中配置的通知。

这是我目前的代码:

    bucket_notifications_configuration = {
        'QueueConfigurations': [{
            'Events': ['s3:ObjectCreated:*'],
            'Id': f"Notif_{queue_name}",
            'QueueArn': queue.attributes['QueueArn'] ,
            "Filter": {
                "Key": {
                    "FilterRules": [
                        {
                        "Name": "suffix",
                        "Value": f"{prefix}"
                        }
                    ]
                }
            }               
        }]
    }
    qpolicy = {
        "Version": "2012-10-17",
        "Id": f"{queue_arn}/SQSDefaultPolicy",
        "Statement": [{
            "Sid": f"allow bucket {bucket} to notify queue {queue_name}",
            "Effect": "Allow",
            "Principal": {"AWS": "*"},
            "Action": "SQS:SendMessage",
            "Resource": queue_arn,
            "Condition": {
                "ArnLike": {
                    "aws:SourceArn": f"arn:aws:s3:*:*:{bucket}"
                }
            }
        }]
    }

    queue_attrs = queue.attributes
    queue_attrs = {"Policy": json.dumps(qpolicy), }


    queue.set_attributes(Attributes=queue_attrs)
    logger.debug(f"queue ready with attributes: {queue.attributes}")        

    previous_notif = client.get_bucket_notification_configuration(Bucket=bucket)
    previous_notif.pop('ResponseMetadata')

    try:
        print("apendado")
        previous_notif['QueueConfigurations'].append(bucket_notifications_configuration['QueueConfigurations'][0])            
        print(f"apendado {previous_notif} ")
    except KeyError:
        previous_notif['QueueConfigurations'] = bucket_notifications_configuration['QueueConfigurations'][0]
        print("cread")

    client.put_bucket_notification_configuration(
        Bucket=bucket,
        NotificationConfiguration=bucket_notifications_configuration)

我确保通知 ID 与其他任何 ID 不同,并且我确保前缀不同。

此代码用新通知覆盖先前的通知,而不是附加新通知。

【问题讨论】:

    标签: python amazon-web-services amazon-s3 amazon-sqs bucket


    【解决方案1】:

    我设法让它与下面的代码一起工作。

    它采用现有配置,添加新配置,然后将其保存回存储桶。该代码假定存在现有配置。

    import boto3
    
    s3_client = boto3.client('s3', region_name = 'ap-southeast-2')
    
    queue_name = 'queue2'
    queue_arn = 'arn:aws:sqs:ap-southeast-2:123456789012:queue2'
    bucket = 'my-bucket'
    prefix = 'folder2/'
    
    # Get the current notification configurations
    response = s3_client.get_bucket_notification_configuration(Bucket=bucket)
    configurations = response['QueueConfigurations']
    
    # New configuration to add
    new_configuration = { 
            'Id': f"Notif_{queue_name}",
            'QueueArn': queue_arn,
            'Events': [
                's3:ObjectCreated:*',
            ],
            'Filter': {
                'Key': {
                    'FilterRules': [
                        {
                            'Name': 'prefix',
                            'Value': prefix
                        },
                    ]
                }
            }
        }
    configurations.append(new_configuration)
    
    # Save combined configurations
    response = s3_client.put_bucket_notification_configuration(
        Bucket = bucket,
        NotificationConfiguration = {'QueueConfigurations' : configurations}
    )
    

    【讨论】:

    • 感谢您的解决方案。我正在寻找如何附加触发器,您的帖子对我帮助很大。
    猜你喜欢
    • 2017-09-21
    • 1970-01-01
    • 2015-04-30
    • 2016-03-02
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    相关资源
    最近更新 更多