发布大消息 Amazon SNS 和 Amazon S3 - Amazon Simple Notification Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

发布大消息 Amazon SNS 和 Amazon S3

要发布大型 Amazon SNS 信息,您可以使用 的 Amazon SNS 扩展客户 库(用于java). 此库对于大于当前最大 256KB,最多2GB。自由将实际有效负载保存到 Amazon S3 存储桶 并公布所存储 Amazon S3 反对主题。已订阅 Amazon SQS 队列可以使用 Amazon SQS 扩展 用于java的客户端库 取消参考并从以下位置检索有效负载 Amazon S3. 其他终点,例如 Lambda,可以使用 载重 正在卸载的的的普通java库 AWS 取消参考并检索 有效负载。

Prerequisites

以下是使用 Amazon SNS 为vacia扩充的客户端库:

配置邮件存储

的 Amazon SNS 扩展的负载库用于负载卸载通用的 库 AWS 用于邮件存储和检索。您可以配置以下内容 Amazon S3 信息 存储选项:

  • 自定义消息大小阈值 – 包含超过此大小的负载和属性的邮件 将自动存储在 Amazon S3.

  • alwaysThroughS3 标记 – 将该值设置为 true 强制所有信息 待存储的有效负载 Amazon S3. 例如:

    SNSExtendedClientConfiguration snsExtendedClientConfiguration = new SNSExtendedClientConfiguration() .withPayloadSupportEnabled(s3Client, BUCKET_NAME).withAlwaysThroughS3(true);
  • 自定义KMS键 – 在中使用的服务器端加密密钥 Amazon S3 桶。

  • 区间名称 – 的 Amazon S3 用于存储消息有效负载的存储桶。

示例: 将消息发布到 Amazon SNS 有效负载储存在 Amazon S3

以下显示了如何进行以下操作的示例:

  • 创建主题和队列示例。

  • 订阅队列以接收主题的消息。

  • 发布测试消息。

消息有效负载存储在 Amazon S3 并公布了对它的引用。的 Amazon SQS 扩展客户端用于接收消息。

import com.amazon.sqs.javamessaging.AmazonSQSExtendedClient; import com.amazon.sqs.javamessaging.ExtendedClientConfiguration; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.sns.AmazonSNS; import com.amazonaws.services.sns.AmazonSNSClientBuilder; import com.amazonaws.services.sns.model.CreateTopicRequest; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.services.sns.model.SetSubscriptionAttributesRequest; import com.amazonaws.services.sns.util.Topics; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.CreateQueueRequest; import com.amazonaws.services.sqs.model.ReceiveMessageResult; import software.amazon.sns.AmazonSNSExtendedClient; import software.amazon.sns.SNSExtendedClientConfiguration; public class Example { public static void main(String[] args) { final String BUCKET_NAME = "extended-client-bucket"; final String TOPIC_NAME = "extended-client-topic"; final String QUEUE_NAME = "extended-client-queue"; final Regions region = Regions.DEFAULT_REGION; //Message threshold controls the maximum message size that will be allowed to be published //through SNS using the extended client. Payload of messages exceeding this value will be stored in //S3. The default value of this parameter is 256 KB which is the maximum message size in SNS (and SQS). final int EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD = 32; //Initialize SNS, SQS and S3 clients final AmazonSNS snsClient = AmazonSNSClientBuilder.standard().withRegion(region).build(); final AmazonSQS sqsClient = AmazonSQSClientBuilder.standard().withRegion(region).build(); final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build(); //Create bucket, topic, queue and subscription s3Client.createBucket(BUCKET_NAME); final String topicArn = snsClient.createTopic( new CreateTopicRequest().withName(TOPIC_NAME) ).getTopicArn(); final String queueUrl = sqsClient.createQueue( new CreateQueueRequest().withQueueName(QUEUE_NAME) ).getQueueUrl(); final String subscriptionArn = Topics.subscribeQueue( snsClient, sqsClient, topicArn, queueUrl ); //To read message content stored in S3 transparently through SQS extended client, //set the RawMessageDelivery subscription attribute to TRUE final SetSubscriptionAttributesRequest subscriptionAttributesRequest = new SetSubscriptionAttributesRequest(); subscriptionAttributesRequest.setSubscriptionArn(subscriptionArn); subscriptionAttributesRequest.setAttributeName("RawMessageDelivery"); subscriptionAttributesRequest.setAttributeValue("TRUE"); snsClient.setSubscriptionAttributes(subscriptionAttributesRequest); //Initialize SNS extended client //PayloadSizeThreshold triggers message content storage in S3 when the threshold is exceeded //To store all messages content in S3, use AlwaysThroughS3 flag final SNSExtendedClientConfiguration snsExtendedClientConfiguration = new SNSExtendedClientConfiguration() .withPayloadSupportEnabled(s3Client, BUCKET_NAME) .withPayloadSizeThreshold(EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD); final AmazonSNSExtendedClient snsExtendedClient = new AmazonSNSExtendedClient(snsClient, snsExtendedClientConfiguration); //Publish message via SNS with storage in S3 final String message = "This message is stored in S3 as it exceeds the threshold of 32 bytes set above."; snsExtendedClient.publish(topicArn, message); //Initialize SQS extended client final ExtendedClientConfiguration sqsExtendedClientConfiguration = new ExtendedClientConfiguration() .withPayloadSupportEnabled(s3Client, BUCKET_NAME); final AmazonSQSExtendedClient sqsExtendedClient = new AmazonSQSExtendedClient(sqsClient, sqsExtendedClientConfiguration); //Read the message from the queue final ReceiveMessageResult result = sqsExtendedClient.receiveMessage(queueUrl); System.out.println("Received message is " + result.getMessages().get(0).getBody()); } }

其他终点方案

同时 Amazon SNS 和 Amazon SQS 库使用 卸载的负载为 AWS 存储和检索 消息有效负载 Amazon S3. 任何启用java的端点(例如,HTTPS 可以在相同的库中取消引用 信息内容。

无法使用卸载Payload的dominalapplicable和Common 库 AWS 仍然可以发布包含有效负载的邮件,存储在 Amazon S3. 以下是 Amazon S3 参考 由上述代码示例发布的代码:

[ "software.amazon.payloadoffloading.PayloadS3Pointer", { "s3BucketName": "extended-client-bucket", "s3Key": "xxxx-xxxxx-xxxxx-xxxxxx" } ]