要根据 aws:SourceArn 限制谁可以向 SQS 发送消息,需要确保以下内容:
在创建 SQS 队列时,需要将 Enable access policy 选项设为 Yes,以启用访问策略功能。
确保访问策略中包含了允许发送消息的资源的 ARN。例如,如果要限制只有 S3 存储桶中的内容才能发送消息到 SQS,则需要添加以下策略:
{
"Version": "2012-10-17",
"Id": "example-ID",
"Statement": [
{
"Sid": "example-statement-ID",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"sqs:SendMessage"
],
"Resource": "arn:aws:sqs:us-west-2:123456789012:example_queue",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:s3:::example_bucket"
}
}
}
]
}
这个策略中, "Resource" 字段指定了要保护的 SQS 队列的 ARN,而 "Condition" 字段则指定了在什么情况下允许发送消息。在这个例子中,只有来自 S3 存储桶 example_bucket 的消息才被允许发送。
s3 = boto3.client('s3')
s3.put_object(
Body='Hello, world!',
Bucket='example_bucket',
Key='example_key',
Metadata={
'x-amz-sns-topic-arn