本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用服务器端加密 (SSE)
您可以使用Amazon SDK for Java将服务器端加密 (SSE) 添加到 Amazon SQS 队列。每个队列都使用Amazon Key Management Service(Amazon KMS) KMS 密钥用于生成数据加密密钥。此示例使用AmazonAmazon SQS 的托管 KMS 密钥。有关使用 SSE 和 KMS 密钥角色的更多信息,请参阅静态加密.
将 SSE 添加到现有队列
要为现有队列启用服务器端加密,请使用SetQueueAttributes方法来设置KmsMasterKeyId属性。
以下代码示例设置Amazon KMS key作为AmazonAmazon SQS 的托管 KMS 密钥。此示例还将设置为Amazon KMS key重用期到 140 秒。
运行示例代码之前,请确保您已设置您的Amazon凭证。有关更多信息,请参阅 。设置Amazon发展凭证和区域中的Amazon SDK for Java 2.x开发人员指南.
// 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
要为现有队列禁用服务器端加密,请将KmsMasterKeyId使用属性为空字符串SetQueueAttributes方法。
null 对于 KmsMasterKeyId 是无效值。
使用 SSE 创建队列
要在创建队列时启用 SSE,请添加KmsMasterKeyId属性到CreateQueueAPI 方法。
以下示例创建启用 SSE 的新队列。队列使用AmazonAmazon SQS 的托管 KMS 密钥。此示例还将设置为Amazon KMS key重用期到 160 秒。
运行示例代码之前,请确保您已设置您的Amazon凭证。有关更多信息,请参阅 。设置Amazon发展凭证和区域中的Amazon SDK for Java 2.x开发人员指南.
// 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 属性
有关检索队列属性的信息,请参阅示例中的亚马逊简单队列服务 API 参考.
要检索特定队列的 KMS 密钥 ID 或数据密钥重用期限,请运行GetQueueAttributes方法然后检索KmsMasterKeyId和KmsDataKeyReusePeriodSeconds价值。