

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SDK を使用してメッセージデータを保護するための Amazon SNS データ保護ポリシーを作成する
<a name="sns-message-data-protection-configure-sdk"></a>

**重要**  
Amazon SNS メッセージデータ保護は、新規顧客には利用できなくなりました。代替方法の詳細とガイダンスについては、[Amazon SNS メッセージデータ保護の可用性の変更](https://docs.aws.amazon.com/sns/latest/dg/sns-message-data-protection-availability-change.html)」を参照してください。

 AWS アカウント内の Amazon SNS リソースの数とサイズには制限があります。詳細については、「[Amazon Simple Notification Service のエンドポイントとクォータ](https://docs.aws.amazon.com/general/latest/gr/sns.html)」を参照してください。

## AWS SDK を使用したデータ保護ポリシーの作成
<a name="create-policies-sdk"></a>

 AWS SDK を使用して Amazon SNS データ保護ポリシーを作成します。

**Amazon SNS トピック (AWS SDK) とともにデータ保護ポリシーを作成するには**  
以下のオプションを使用して、標準の Amazon SNS トピックと一緒に新しいデータ保護ポリシーを作成します。

------
#### [ Java ]

```
/**
 * For information regarding CreateTopic see this documentation topic:
 *
 * https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-sns-src-main-java-com-example-sns-CreateTopic.java.html
 */

public static String createSNSTopicWithDataProtectionPolicy(SnsClient snsClient, String topicName, String dataProtectionPolicy) {

    try {
        CreateTopicRequest request = CreateTopicRequest.builder()
                .name(topicName)
                .dataProtectionPolicy(dataProtectionPolicy)
                .build();

        CreateTopicResponse result = snsClient.createTopic(request);
        return result.topicArn();
    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
```

------
#### [ JavaScript ]

```
// Import required AWS SDK clients and commands for Node.js
import {CreateTopicCommand } from "@aws-sdk/client-sns";
import {snsClient } from "./libs/snsClient.js";

// Set the parameters
const params = { Name: "TOPIC_NAME", DataProtectionPolicy: "DATA_PROTECTION_POLICY" };

const run = async () => {
  try {
    const data = await snsClient.send(new CreateTopicCommand(params));
    console.log("Success.",  data);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err.stack);
  }
};
run();
```

------

**既存の Amazon SNS トピック (SDK) のデータ保護ポリシーを作成または取得するにはAWS**  
以下のオプションを使用して、標準の Amazon SNS トピックと一緒に新しいデータ保護ポリシーを作成または取得します。

------
#### [ Java ]

```
public static void putDataProtectionPolicy(SnsClient snsClient, String topicName, String dataProtectionPolicy) {

    try {
        PutDataProtectionPolicyRequest request = PutDataProtectionPolicyRequest.builder()
                .resourceArn(topicName)
                .dataProtectionPolicy(dataProtectionPolicy)
                .build();

        PutDataProtectionPolicyResponse result = snsClient.putDataProtectionPolicy(request);
        System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() 
                + "\n\nTopic " + request.resourceArn()
                + " DataProtectionPolicy " + request.dataProtectionPolicy());
    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}


public static void getDataProtectionPolicy(SnsClient snsClient, String topicName) {

    try {
        GetDataProtectionPolicyRequest request = GetDataProtectionPolicyRequest.builder()
                .resourceArn(topicName)
                .build();
        
        GetDataProtectionPolicyResponse result = snsClient.getDataProtectionPolicy(request);
        
        System.out.println("\n\nStatus is " + result.sdkHttpResponse().statusCode() 
        + "\n\nDataProtectionPolicy: \n\n" + result.dataProtectionPolicy());
    } catch (SnsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
```

------
#### [ JavaScript ]

```
// Import required AWS SDK clients and commands for Node.js
import {PutDataProtectionPolicyCommand, GetDataProtectionPolicyCommand } from "@aws-sdk/client-sns";
import {snsClient } from "./libs/snsClient.js";

// Set the parameters
const putParams = { ResourceArn: "TOPIC_ARN", DataProtectionPolicy: "DATA_PROTECTION_POLICY" };

const runPut = async () => {
  try {
    const data = await snsClient.send(new PutDataProtectionPolicyCommand(putParams));
    console.log("Success.",  data);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err.stack);
  }
};
runPut();

// Set the parameters
const getParams = { ResourceArn: "TOPIC_ARN" };

const runGet = async () => {
  try {
    const data = await snsClient.send(new GetDataProtectionPolicyCommand(getParams));
    console.log("Success.",  data);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err.stack);
  }
};
runGet();
```

------