

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 搭配 Amazon SQS 佇列使用伺服器端加密
<a name="sqs-java-configure-sse"></a>

使用 適用於 Java 的 AWS SDK 將伺服器端加密 (SSE) 新增至 Amazon SQS 佇列。每個佇列都使用 AWS Key Management Service (AWS KMS) KMS 金鑰來產生資料加密金鑰。此範例使用 Amazon SQS 的 AWS 受管 KMS 金鑰。

如需使用 SSE 和 KMS 金鑰之角色的相關資訊，請參閱 [Amazon SQS 中的靜態加密](sqs-server-side-encryption.md)。

## 將 SSE 新增到現有佇列
<a name="sqs-java-configure-sse-existing-queue"></a>

若要啟用現有佇列的伺服器端加密，請使用 `[SetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html)` 方法來設定 `KmsMasterKeyId` 屬性。

下列程式碼範例會將 設定為 AWS KMS key Amazon SQS 的 AWS 受管 KMS 金鑰。此範例也會將 [AWS KMS key 重複使用期間](sqs-server-side-encryption.md#sqs-sse-key-terms)設定為 140 秒。

 執行範例程式碼之前，請確定您已設定 AWS 登入資料。如需詳細資訊，請參閱《 *AWS SDK for Java 2.x 開發人員指南*》中的[設定開發的 AWS 登入資料和區域](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials)。

```
    public static void addEncryption(String queueName, String kmsMasterKeyAlias) {
        SqsClient sqsClient = SqsClient.create();

        GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder()
                .queueName(queueName)
                .build();

        GetQueueUrlResponse getQueueUrlResponse;
        try {
            getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
        } catch (QueueDoesNotExistException e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
        String queueUrl = getQueueUrlResponse.queueUrl();


        Map<QueueAttributeName, String> attributes = Map.of(
                QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias,
                QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140" // Set the data key reuse period to 140 seconds.
        );                                                                  // This is how long SQS can reuse the data key before requesting a new one from KMS.

        SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder()
                .queueUrl(queueUrl)
                .attributes(attributes)
                .build();
        try {
            sqsClient.setQueueAttributes(attRequest);
            LOGGER.info("The attributes have been applied to {}", queueName);
        } catch (InvalidAttributeNameException | InvalidAttributeValueException e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            sqsClient.close();
        }
    }
```

## 停用佇列的 SSE
<a name="sqs-java-disable-sse"></a>

若要停用現有佇列的伺服器端加密，請使用 `SetQueueAttributes` 方法將 `KmsMasterKeyId` 屬性設定為空白字串。

**重要**  
`null` 不是 `KmsMasterKeyId` 的有效值。

## 使用 SSE 建立佇列
<a name="sqs-java-configure-sse-create-queue"></a>

若要在建立佇列時啟用 SSE，請將 `KmsMasterKeyId` 屬性新增至 `[CreateQueue](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html)` API 方法。

以下範例顯示如何建立啟用 SSE 的新佇列。佇列使用 Amazon SQS 的 AWS 受管 KMS 金鑰。此範例也會將 [AWS KMS key 重複使用期間](sqs-server-side-encryption.md#sqs-sse-key-terms)設定為 160 秒。

 執行範例程式碼之前，請確定您已設定 AWS 登入資料。如需詳細資訊，請參閱《 *AWS SDK for Java 2.x 開發人員指南*》中的[設定用於開發的 AWS 登入資料和區域](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials)。

```
// 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 AWS 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 屬性
<a name="sqs-java-get-sse-attributes"></a>

如需擷取佇列屬性的相關資訊，請參閱《Amazon Simple Queue Service API 參考》**中的[範例](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html#API_GetQueueAttributes_Examples)。

若要擷取特定佇列的 KMS 金鑰 ID 或資料金鑰重複使用期間，請執行 `[GetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html)` 方法並擷取 `KmsMasterKeyId` 和 `KmsDataKeyReusePeriodSeconds` 值。