使用服务器端加密 (SSE) - Amazon Simple Queue Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用服务器端加密 (SSE)

您可以使用Amazon SDK for Java 向 Amazon SQS 队列添加服务器端加密 (SSE)。每个队列都使用 Amazon Key Management Service (Amazon KMS) KMS 密钥生成数据加密密钥。此示例设置 Amazon 托管 KMS 密钥用于 Amazon SQS。有关使用 SSE 和 KMS 密钥角色的更多信息,请参阅静态加密

向现有队列添加 SSE

要为现有队列启用服务器端加密,请使用 SetQueueAttributes 方法设置 KmsMasterKeyId 属性。

以下代码示例将 Amazon KMS key设置为 Amazon 托管 KMS 密钥,用于 Amazon SQS。该示例还会将 Amazon KMS key重用周期设置为 140 秒。

在运行示例代码之前,请确保您设置了 Amazon 凭据。有关更多信息,请参阅 Amazon SDK for Java 2.x 开发人员指南中的设置 Amazon 凭据和开发区域

// Create an SqsClient for the specified Region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Get the URL of your queue. String myQueueName = "my queue"; GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(myQueueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); // Create a hashmap for the attributes. Add the key alias and reuse period to the hashmap. HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>(); final String kmsMasterKeyAlias = "alias/aws/sqs"; // the alias of the Amazon managed KMS key for Amazon SQS. attributes.put(QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias); attributes.put(QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140"); // Create the SetQueueAttributesRequest. SetQueueAttributesRequest set_attrs_request = SetQueueAttributesRequest.builder() .queueUrl(queueUrl) .attributes(attributes) .build(); sqsClient.setQueueAttributes(set_attrs_request);

为队列禁用 SSE

要为现有队列禁用服务器端加密,请使用 SetQueueAttributes 方法将 KmsMasterKeyId 属性设置为空字符串。

重要

null 对于 KmsMasterKeyId 是无效值。

使用 SSE 创建队列

要在创建队列时启用 SSE,请将 KmsMasterKeyId 属性添加到 CreateQueue API 方法中。

以下示例将在启用了 SSE 的情况下创建新队列。队列将 Amazon 托管 KMS 密钥用于 Amazon SQS。该示例还会将 Amazon KMS key重用周期设置为 160 秒。

在运行示例代码之前,请确保您设置了 Amazon 凭据。有关更多信息,请参阅 Amazon SDK for Java 2.x 开发人员指南中的设置 Amazon 凭据和开发区域

// Create an SqsClient for the specified Region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Create a hashmap for the attributes. Add the key alias and reuse period to the hashmap. HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>(); final String kmsMasterKeyAlias = "alias/aws/sqs"; // the alias of the Amazon managed KMS key for Amazon SQS. attributes.put(QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias); attributes.put(QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140"); // Add the attributes to the CreateQueueRequest. CreateQueueRequest createQueueRequest = CreateQueueRequest.builder() .queueName(queueName) .attributes(attributes) .build(); sqsClient.createQueue(createQueueRequest);

检索 SSE 属性

有关检索队列属性的信息,请参阅 Amazon Simple Queue Service API 参考中的示例

要检索特定队列的 KMS 密钥 ID 或数据密钥重用周期,请运行 GetQueueAttributes 方法并检索 KmsMasterKeyIdKmsDataKeyReusePeriodSeconds 值。