本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 SDK 建立 Amazon SNS 資料保護政策以保護訊息資料
重要
Amazon SNS 訊息資料保護不再提供給新客戶。如需替代方案的詳細資訊和指引,請參閱 Amazon SNS 訊息資料保護可用性變更。
AWS 帳戶中 Amazon SNS 資源的數量和大小有限。如需詳細資訊,請參閱 Amazon Simple Notification Service 端點和配額。
使用 AWS SDK 建立資料保護政策
使用 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 主題 (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 管理主控台
刪除資料保護政策