本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 SDK 创建 Amazon SNS 数据保护策略以保护消息数据
重要
Amazon SNS 消息数据保护不再向新客户提供。有关替代方案的更多信息和指导,请参阅 Amazon SNS 消息数据保护可用性变更。
一个 AWS 账户中 Amazon SNS 资源的数量和大小是有限的。有关更多信息,请参阅 Amazon Simple Notification Service endpoints and quotas。
使用 AWS SDK 创建数据保护策略
使用软件开发工具包创建 Amazon SNS 数据保护政策。 AWS
将数据保护策略与 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 主题 (AWS SDK) 创建或检索数据保护策略
使用以下选项,与标准 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();
使用 AWS 管理控制台
删除数据保护策略