

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建 Amazon SNS 主题，并发布消息
创建主题并发布消息

本主题提供了管理 Amazon SNS 资源的基本步骤，特别侧重于主题、订阅和消息发布。首先，您将为 Amazon SNS 设置必要的访问权限，确保您拥有创建和管理 Amazon SNS 资源的正确权限。接下来，您将创建一个新的 Amazon SNS 主题，该主题将作为管理消息和向订阅用户发送消息的中央枢纽。创建主题后，您将继续创建对此主题的订阅，允许特定的端点接收发布到该主题的消息。

主题和订阅完成后，您将向该主题发布一条消息，观察 Amazon SNS 如何高效地将消息传送到所有已订阅的端点。最后，您将学习如何删除订阅和主题，从而完成您管理的 Amazon SNS 资源的生命周期。这种方法可以让您清楚地了解 Amazon SNS 中的基本操作，使您具备使用 Amazon SNS 控制台管理消息工作流程所需的实用技能。

# 设置 Amazon SNS 的访问权限
设置

在首次使用 Amazon SNS 之前，您必须完成以下步骤。

## 创建一个 Amazon Web Services 账户 和一个 IAM 用户
创建账户和 IAM 用户

要访问任何 Amazon 服务，必须先创建一个[Amazon Web Services 账户](https://www.amazonaws.cn/)。您可以使用 Amazon Web Services 账户 来查看您的活动和使用情况报告，以及管理身份验证和访问权限。

### 注册获取 Amazon Web Services 账户


如果您没有 Amazon Web Services 账户，请完成以下步骤来创建一个。

**报名参加 Amazon Web Services 账户**

1. 打开[https://portal.aws.amazon.com/billing/注册。](https://portal.amazonaws.cn/billing/signup)

1. 按照屏幕上的说明操作。

   在注册时，将接到电话或收到短信，要求使用电话键盘输入一个验证码。

   当您注册时 Amazon Web Services 账户，将创建一个 *Amazon Web Services 账户 root 用户*。根用户有权访问该账户中的所有 Amazon Web Services 服务 和资源。作为最佳安全实践，请为用户分配管理访问权限，并且只使用根用户来执行[需要根用户访问权限的任务](https://docs.amazonaws.cn/IAM/latest/UserGuide/id_root-user.html#root-user-tasks)。

Amazon 注册过程完成后会向您发送一封确认电子邮件。您可以随时前往 [https://aws.amazon.com/](https://www.amazonaws.cn/)并选择 “**我的账户”，查看您当前的账户活动并管理您的账户**。

### 保护 IAM 用户


注册后 Amazon Web Services 账户，请开启多重身份验证 (MFA)，保护您的管理用户。有关说明，请参阅《IAM 用户指南》**中的 [为 IAM 用户启用虚拟 MFA 设备（控制台）](https://docs.amazonaws.cn/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html#enable-virt-mfa-for-iam-user)。

要允许其他用户访问您的 Amazon Web Services 账户 资源，请创建 IAM 用户。为了保护您的 IAM 用户，请启用 MFA 并仅向 IAM 用户授予执行任务所需的权限。

有关创建和保护 IAM 用户的更多信息，请参阅《IAM 用户指南》中的以下主题：**
+ [在你的 IAM 用户中创建 Amazon Web Services 账户](https://docs.amazonaws.cn//IAM/latest/UserGuide/id_users_create.html)
+ [适用于 Amazon 资源的访问权限管理](https://docs.amazonaws.cn/IAM/latest/UserGuide/access.html)
+ [基于 IAM 身份的策略示例](https://docs.amazonaws.cn/IAM/latest/UserGuide/access_policies_examples.html)

## 后续步骤


现在，您已准备好使用 Amazon SNS，可以从以下入手：

1. [创建 Amazon SNS 主题](sns-create-topic.md)

1. [在 Amazon SNS 主题中创建订阅](sns-create-subscribe-endpoint-to-topic.md) 

1. [发布 Amazon SNS 消息](sns-publishing.md)

1. [删除 Amazon SNS 订阅和主题](sns-delete-subscription-topic.md)

# 创建 Amazon SNS 主题
步骤 1：创建主题

Amazon SNS 主题是一个逻辑接入点，可充当*通信通道*。主题允许您对多个*终端节点*（例如 Amazon SQS Amazon Lambda、HTTP/S 或电子邮件地址）进行分组。

要广播与需要其消息的多个其他服务（例如，结算和执行系统）协作的消息生产者系统（例如，电子商务网站）的消息，您可以为生产者系统创建主题。

第一个也是最常见的 Amazon SNS 任务是创建主题。本页显示了如何使用 Amazon Web Services 管理控制台 适用于 Java 的 Amazon SDK、和， 适用于 .NET 的 Amazon SDK 来创建主题。

在创建过程中，您可以选择主题类型（标准或 FIFO）并命名主题。创建主题后，无法更改主题类型或名称。在创建主题期间，所有其他配置选项都是可选的，您可以稍后对其进行编辑。

**重要**  
请勿在主题名称中添加个人身份信息 (PII) 或其他机密或敏感信息。其他 Amazon Web Services 可以访问主题名称，包括 CloudWatch 日志。主题名称不适合用于私有或敏感数据。

## 要使用创建主题 Amazon Web Services 管理控制台
Amazon Web Services 管理控制台

在 Amazon SNS 中创建主题为消息分发奠定了基础，使您能够发布可以扇出给多个订阅用户的消息。此步骤对于配置主题的类型、加密设置和访问策略至关重要，可确保主题符合组织的安全、合规性和运营要求。

1. 登录 [Amazon SNS 控制台](https://console.amazonaws.cn/sns/home)。

1. 请执行以下操作之一：
   + 如果 Amazon Web Services 账户 之前未在您的主题下创建过任何主题，请阅读主页上对 Amazon SNS 的描述。
   + 如果 Amazon Web Services 账户 之前已在您的下方创建过主题，请在导航面板上选择**主题**。

1. 在 **Topics**（主页）页面上，选择 **Create topic**（创建主题）。

1. 在 **Create topic**（创建主题）页面上，在 **Details**（详细信息）部分中，执行以下操作：

   1. 对于 **Type**（类型），选择主题类型（**标准**或者**FIFO**）。

   1. 输入主题的**名称**。对于 [FIFO 主题](sns-fifo-topics.md)，将 **.fifo** 添加到名称的末尾。

   1. （可选）输入主题的**显示名称**。
**重要**  
订阅电子邮件端点时，Amazon SNS 主题显示名称和发送电子邮件地址（例如 no-reply@sns.amazonaws.com）的组合字符计数不得超过 320 个 UTF-8 字符。在为 Amazon SNS 主题配置显示名称之前，您可以使用第三方编码工具验证发送地址的长度。

   1. （可选）对于 FIFO 主题，您可以选择**基于内容的消息重复数据删除**以启用默认的消息重复数据删除。有关更多信息，请参阅 [FIFO 主题的 Amazon SNS 消息重复数据删除](fifo-message-dedup.md)。

1. （可选）展开**加密**部分并执行以下操作。有关更多信息，请参阅 [使用服务器端加密保护 Amazon SNS 数据安全](sns-server-side-encryption.md)。

   1. 选择**启用加密**。

   1. 指定密 Amazon KMS 钥。有关更多信息，请参阅 [关键术语](sns-server-side-encryption.md#sse-key-terms)。

      对于每个 KMS 类型，都会显示 **Description**（描述）、**Account**（账户）和 **KMS ARN**。
**重要**  
如果您不是 KMS 的拥有者，或者您登录的账户没有 `kms:ListAliases` 和 `kms:DescribeKey` 权限，则无法在 Amazon SNS 控制台上查看有关 KMS 的信息。  
要求 KMS 的拥有者授予您这些权限。有关更多信息，请参阅《Amazon Key Management Service Developer Guide》**中的 [Amazon KMS API Permissions: Actions and Resources Reference](https://docs.amazonaws.cn/kms/latest/developerguide/kms-api-permissions-reference.html)。
      + 默认选择适用于 Amazon SNS 的 Amazon 托管 KMS**（默认）alias/aws/sns**。
**注意**  
记住以下内容：  
首次使用为主题指定适用于 Amazon SNS 的 Amazon 托管 KMS 时， Amazon KMS 会为亚马逊 SNS 创建 Amazon 托管 KMS。 Amazon Web Services 管理控制台 
或者，在启用 SSE 的情况下首次对主题使用`Publish`操作时， Amazon KMS 会为 Amazon SNS 创建 Amazon 托管 KMS。
      + 要使用 Amazon 账户中的自定义 KMS，请选择 **KMS 密钥**字段，然后从列表中选择自定义 KMS。
**注意**  
有关创建自定义密钥的说明 KMSs，请参阅《*Amazon Key Management Service 开发者指南》*中的[创建密钥](https://docs.amazonaws.cn/kms/latest/developerguide/create-keys.html)
      + 要使用来自您的 Amazon 账户或其他账户的自定义 KMS ARN，请将其输入到 **KMS 密钥**字段中。 Amazon 

1. （可选）默认情况下，只有主题拥有者才能发布或订阅主题。要配置其他访问权限，请展开**访问策略**部分。有关更多信息，请参阅 [Amazon SNS 中的 Identity and Access Management](security-iam.md)和[用于 Amazon SNS 访问控制的示例案例](sns-access-policy-use-cases.md)。
**注意**  
使用控制台创建主题时，默认策略使用 `aws:SourceOwner` 条件键。此密钥类似于。`aws:SourceAccount`

1. （可选）要配置 Amazon SNS 重试失败消息传输尝试的方式，请展开 **Delivery retry policy (HTTP/S)**（传输重试策略 (HTTP/S)）部分。有关更多信息，请参阅 [Amazon SNS 消息传输重试](sns-message-delivery-retries.md)。

1. （可选）要配置 Amazon SNS 如何记录向其发送的消息 CloudWatch，请展开**传送状态记录部分**。有关更多信息，请参阅 [Amazon SNS 消息传输状态](sns-topic-attributes.md)。

1. （可选）要将元数据标签添加到主题中，请展开**标签**部分，输入一个**键**和**值**（可选），然后选择**添加标签**。有关更多信息，请参阅 [Amazon SNS 主题标记](sns-tags.md)。

1. 选择**创建主题**。

   主题已创建并显示***MyTopic***页面。

   主题的**名称**、**ARN**、（可选）**显示名称**和**主题所有者的** Amazon 账户 ID 显示在**详细信息**部分中。

1. 将主题 ARN 复制到剪贴板，例如：

   ```
   arn:aws:sns:us-east-2:123456789012:MyTopic
   ```

## 使用 Amazon SDK 创建主题
Amazon SDKs

要使用 S Amazon DK，必须使用您的凭据对其进行配置。有关更多信息，请参阅[《工具参考指南》和《*工具参考指南》中的共享配置Amazon SDKs 和*凭据文件](https://docs.amazonaws.cn/sdkref/latest/guide/creds-config-files.html)。

以下代码示例演示如何使用 `CreateTopic`。

------
#### [ .NET ]

**适用于 .NET 的 Amazon SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/SNS#code-examples)中查找完整示例，了解如何进行设置和运行。
使用特定的名称创建主题。  

```
    using System;
    using System.Threading.Tasks;
    using Amazon.SimpleNotificationService;
    using Amazon.SimpleNotificationService.Model;

    /// <summary>
    /// This example shows how to use Amazon Simple Notification Service
    /// (Amazon SNS) to add a new Amazon SNS topic.
    /// </summary>
    public class CreateSNSTopic
    {
        public static async Task Main()
        {
            string topicName = "ExampleSNSTopic";

            IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();

            var topicArn = await CreateSNSTopicAsync(client, topicName);
            Console.WriteLine($"New topic ARN: {topicArn}");
        }

        /// <summary>
        /// Creates a new SNS topic using the supplied topic name.
        /// </summary>
        /// <param name="client">The initialized SNS client object used to
        /// create the new topic.</param>
        /// <param name="topicName">A string representing the topic name.</param>
        /// <returns>The Amazon Resource Name (ARN) of the created topic.</returns>
        public static async Task<string> CreateSNSTopicAsync(IAmazonSimpleNotificationService client, string topicName)
        {
            var request = new CreateTopicRequest
            {
                Name = topicName,
            };

            var response = await client.CreateTopicAsync(request);

            return response.TopicArn;
        }
    }
```
创建一个包含名称以及特定 FIFO 和重复数据消除属性的新主题。  

```
    /// <summary>
    /// Create a new topic with a name and specific FIFO and de-duplication attributes.
    /// </summary>
    /// <param name="topicName">The name for the topic.</param>
    /// <param name="useFifoTopic">True to use a FIFO topic.</param>
    /// <param name="useContentBasedDeduplication">True to use content-based de-duplication.</param>
    /// <returns>The ARN of the new topic.</returns>
    public async Task<string> CreateTopicWithName(string topicName, bool useFifoTopic, bool useContentBasedDeduplication)
    {
        var createTopicRequest = new CreateTopicRequest()
        {
            Name = topicName,
        };

        if (useFifoTopic)
        {
            // Update the name if it is not correct for a FIFO topic.
            if (!topicName.EndsWith(".fifo"))
            {
                createTopicRequest.Name = topicName + ".fifo";
            }

            // Add the attributes from the method parameters.
            createTopicRequest.Attributes = new Dictionary<string, string>
            {
                { "FifoTopic", "true" }
            };
            if (useContentBasedDeduplication)
            {
                createTopicRequest.Attributes.Add("ContentBasedDeduplication", "true");
            }
        }

        var createResponse = await _amazonSNSClient.CreateTopicAsync(createTopicRequest);
        return createResponse.TopicArn;
    }
```
+  有关 API 的详细信息，请参阅 *适用于 .NET 的 Amazon SDK API 参考[CreateTopic](https://docs.amazonaws.cn/goto/DotNetSDKV3/sns-2010-03-31/CreateTopic)*中的。

------
#### [ C\$1\$1 ]

**SDK for C\$1\$1**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
//! Create an Amazon Simple Notification Service (Amazon SNS) topic.
/*!
  \param topicName: An Amazon SNS topic name.
  \param topicARNResult: String to return the Amazon Resource Name (ARN) for the topic.
  \param clientConfiguration: AWS client configuration.
  \return bool: Function succeeded.
 */
bool AwsDoc::SNS::createTopic(const Aws::String &topicName,
                              Aws::String &topicARNResult,
                              const Aws::Client::ClientConfiguration &clientConfiguration) {
    Aws::SNS::SNSClient snsClient(clientConfiguration);

    Aws::SNS::Model::CreateTopicRequest request;
    request.SetName(topicName);

    const Aws::SNS::Model::CreateTopicOutcome outcome = snsClient.CreateTopic(request);

    if (outcome.IsSuccess()) {
        topicARNResult = outcome.GetResult().GetTopicArn();
        std::cout << "Successfully created an Amazon SNS topic " << topicName
                  << " with topic ARN '" << topicARNResult
                  << "'." << std::endl;

    }
    else {
        std::cerr << "Error creating topic " << topicName << ":" <<
                  outcome.GetError().GetMessage() << std::endl;
        topicARNResult.clear();
    }

    return outcome.IsSuccess();
}
```
+  有关 API 的详细信息，请参阅 *适用于 C\$1\$1 的 Amazon SDK API 参考[CreateTopic](https://docs.amazonaws.cn/goto/SdkForCpp/sns-2010-03-31/CreateTopic)*中的。

------
#### [ CLI ]

**Amazon CLI**  
**创建 SNS 主题**  
以下 `create-topic` 示例将创建名为 `my-topic` 的 SNS 主题。  

```
aws sns create-topic \
    --name my-topic
```
输出：  

```
{
    "ResponseMetadata": {
        "RequestId": "1469e8d7-1642-564e-b85d-a19b4b341f83"
    },
    "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic"
}
```
有关更多信息，请参阅[《 Amazon 命令行界面用户指南》中的在 Amazon SQS 和 Amazon SNS 中使用](https://docs.amazonaws.cn/cli/latest/userguide/cli-sqs-queue-sns-topic.html)*命令Amazon 行*界面。  
+  有关 API 的详细信息，请参阅*Amazon CLI 命令参考[CreateTopic](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/create-topic.html)*中的。

------
#### [ Go ]

**适用于 Go 的 SDK V2**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/workflows/topics_and_queues#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import (
	"context"
	"encoding/json"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/sns"
	"github.com/aws/aws-sdk-go-v2/service/sns/types"
)

// SnsActions encapsulates the Amazon Simple Notification Service (Amazon SNS) actions
// used in the examples.
type SnsActions struct {
	SnsClient *sns.Client
}



// CreateTopic creates an Amazon SNS topic with the specified name. You can optionally
// specify that the topic is created as a FIFO topic and whether it uses content-based
// deduplication instead of ID-based deduplication.
func (actor SnsActions) CreateTopic(ctx context.Context, topicName string, isFifoTopic bool, contentBasedDeduplication bool) (string, error) {
	var topicArn string
	topicAttributes := map[string]string{}
	if isFifoTopic {
		topicAttributes["FifoTopic"] = "true"
	}
	if contentBasedDeduplication {
		topicAttributes["ContentBasedDeduplication"] = "true"
	}
	topic, err := actor.SnsClient.CreateTopic(ctx, &sns.CreateTopicInput{
		Name:       aws.String(topicName),
		Attributes: topicAttributes,
	})
	if err != nil {
		log.Printf("Couldn't create topic %v. Here's why: %v\n", topicName, err)
	} else {
		topicArn = *topic.TopicArn
	}

	return topicArn, err
}
```
+  有关 API 的详细信息，请参阅 *适用于 Go 的 Amazon SDK API 参考[CreateTopic](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/sns#Client.CreateTopic)*中的。

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

**适用于 Java 的 SDK 2.x**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
import software.amazon.awssdk.services.sns.model.SnsException;

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class CreateTopic {
    public static void main(String[] args) {
        final String usage = """

                Usage:    <topicName>

                Where:
                   topicName - The name of the topic to create (for example, mytopic).

                """;

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String topicName = args[0];
        System.out.println("Creating a topic with name: " + topicName);
        SnsClient snsClient = SnsClient.builder()
                .region(Region.US_EAST_1)
                .build();

        String arnVal = createSNSTopic(snsClient, topicName);
        System.out.println("The topic ARN is" + arnVal);
        snsClient.close();
    }

    public static String createSNSTopic(SnsClient snsClient, String topicName) {
        CreateTopicResponse result;
        try {
            CreateTopicRequest request = CreateTopicRequest.builder()
                    .name(topicName)
                    .build();

            result = snsClient.createTopic(request);
            return result.topicArn();

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
}
```
+  有关 API 的详细信息，请参阅 *Amazon SDK for Java 2.x API 参考[CreateTopic](https://docs.amazonaws.cn/goto/SdkForJavaV2/sns-2010-03-31/CreateTopic)*中的。

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

**适用于 JavaScript (v3) 的软件开发工具包**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/sns#code-examples) 中查找完整示例，了解如何进行设置和运行。
在单独的模块中创建客户端并将其导出。  

```
import { SNSClient } from "@aws-sdk/client-sns";

// The AWS Region can be provided here using the `region` property. If you leave it blank
// the SDK will default to the region set in your AWS config.
export const snsClient = new SNSClient({});
```
导入 SDK 和客户端模块，然后调用 API。  

```
import { CreateTopicCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";

/**
 * @param {string} topicName - The name of the topic to create.
 */
export const createTopic = async (topicName = "TOPIC_NAME") => {
  const response = await snsClient.send(
    new CreateTopicCommand({ Name: topicName }),
  );
  console.log(response);
  // {
  //   '$metadata': {
  //     httpStatusCode: 200,
  //     requestId: '087b8ad2-4593-50c4-a496-d7e90b82cf3e',
  //     extendedRequestId: undefined,
  //     cfId: undefined,
  //     attempts: 1,
  //     totalRetryDelay: 0
  //   },
  //   TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:TOPIC_NAME'
  // }
  return response;
};
```
+  有关更多信息，请参阅[《适用于 JavaScript 的 Amazon SDK Developer Guide》](https://docs.amazonaws.cn/sdk-for-javascript/v3/developer-guide/sns-examples-managing-topics.html#sns-examples-managing-topics-createtopic)。
+  有关 API 的详细信息，请参阅 *适用于 JavaScript 的 Amazon SDK API 参考[CreateTopic](https://docs.amazonaws.cn/AWSJavaScriptSDK/v3/latest/client/sns/command/CreateTopicCommand)*中的。

------
#### [ Kotlin ]

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
suspend fun createSNSTopic(topicName: String): String {
    val request =
        CreateTopicRequest {
            name = topicName
        }

    SnsClient.fromEnvironment { region = "us-east-1" }.use { snsClient ->
        val result = snsClient.createTopic(request)
        return result.topicArn.toString()
    }
}
```
+  有关 API 的详细信息，请参阅适用[CreateTopic](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的Amazon SDK API 参考*。

------
#### [ PHP ]

**适用于 PHP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;


/**
 * Create a Simple Notification Service topics in your AWS account at the requested region.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

$SnSclient = new SnsClient([
    'profile' => 'default',
    'region' => 'us-east-1',
    'version' => '2010-03-31'
]);

$topicname = 'myTopic';

try {
    $result = $SnSclient->createTopic([
        'Name' => $topicname,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```
+  有关更多信息，请参阅《适用于 PHP 的 Amazon SDK 开发人员指南》[https://docs.amazonaws.cn/sdk-for-php/v3/developer-guide/sns-examples-managing-topics.html#create-a-topic](https://docs.amazonaws.cn/sdk-for-php/v3/developer-guide/sns-examples-managing-topics.html#create-a-topic)。
+  有关 API 的详细信息，请参阅 *适用于 PHP 的 Amazon SDK API 参考[CreateTopic](https://docs.amazonaws.cn/goto/SdkForPHPV3/sns-2010-03-31/CreateTopic)*中的。

------
#### [ Python ]

**适用于 Python 的 SDK（Boto3）**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
class SnsWrapper:
    """Encapsulates Amazon SNS topic and subscription functions."""

    def __init__(self, sns_resource):
        """
        :param sns_resource: A Boto3 Amazon SNS resource.
        """
        self.sns_resource = sns_resource


    def create_topic(self, name):
        """
        Creates a notification topic.

        :param name: The name of the topic to create.
        :return: The newly created topic.
        """
        try:
            topic = self.sns_resource.create_topic(Name=name)
            logger.info("Created topic %s with ARN %s.", name, topic.arn)
        except ClientError:
            logger.exception("Couldn't create topic %s.", name)
            raise
        else:
            return topic
```

```
class SnsWrapper:
    """Wrapper class for managing Amazon SNS operations."""

    def __init__(self, sns_client: Any) -> None:
        """
        Initialize the SnsWrapper.

        :param sns_client: A Boto3 Amazon SNS client.
        """
        self.sns_client = sns_client

    @classmethod
    def from_client(cls) -> 'SnsWrapper':
        """
        Create an SnsWrapper instance using a default boto3 client.

        :return: An instance of this class.
        """
        sns_client = boto3.client('sns')
        return cls(sns_client)


    def create_topic(
        self, 
        topic_name: str, 
        is_fifo: bool = False, 
        content_based_deduplication: bool = False
    ) -> str:
        """
        Create an SNS topic.

        :param topic_name: The name of the topic to create.
        :param is_fifo: Whether to create a FIFO topic.
        :param content_based_deduplication: Whether to use content-based deduplication for FIFO topics.
        :return: The ARN of the created topic.
        :raises ClientError: If the topic creation fails.
        """
        try:
            # Add .fifo suffix for FIFO topics
            if is_fifo and not topic_name.endswith('.fifo'):
                topic_name += '.fifo'

            attributes = {}
            if is_fifo:
                attributes['FifoTopic'] = 'true'
                if content_based_deduplication:
                    attributes['ContentBasedDeduplication'] = 'true'

            response = self.sns_client.create_topic(
                Name=topic_name,
                Attributes=attributes
            )

            topic_arn = response['TopicArn']
            logger.info(f"Created topic: {topic_name} with ARN: {topic_arn}")
            return topic_arn

        except ClientError as e:
            error_code = e.response.get('Error', {}).get('Code', 'Unknown')
            logger.error(f"Error creating topic {topic_name}: {error_code} - {e}")
            raise
```
+  有关 API 的详细信息，请参阅适用[CreateTopic](https://docs.amazonaws.cn/goto/boto3/sns-2010-03-31/CreateTopic)于 *Python 的Amazon SDK (Boto3) API 参考*。

------
#### [ Ruby ]

**适用于 Ruby 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/ruby/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
# This class demonstrates how to create an Amazon Simple Notification Service (SNS) topic.
class SNSTopicCreator
  # Initializes an SNS client.
  #
  # Utilizes the default AWS configuration for region and credentials.
  def initialize
    @sns_client = Aws::SNS::Client.new
  end

  # Attempts to create an SNS topic with the specified name.
  #
  # @param topic_name [String] The name of the SNS topic to create.
  # @return [Boolean] true if the topic was successfully created, false otherwise.
  def create_topic(topic_name)
    @sns_client.create_topic(name: topic_name)
    puts "The topic '#{topic_name}' was successfully created."
    true
  rescue Aws::SNS::Errors::ServiceError => e
    # Handles SNS service errors gracefully.
    puts "Error while creating the topic named '#{topic_name}': #{e.message}"
    false
  end
end

# Example usage:
if $PROGRAM_NAME == __FILE__
  topic_name = 'YourTopicName' # Replace with your topic name
  sns_topic_creator = SNSTopicCreator.new

  puts "Creating the topic '#{topic_name}'..."
  unless sns_topic_creator.create_topic(topic_name)
    puts 'The topic was not created. Stopping program.'
    exit 1
  end
end
```
+  有关更多信息，请参阅《适用于 Ruby 的 Amazon SDK 开发人员指南》[https://docs.amazonaws.cn/sdk-for-ruby/v3/developer-guide/sns-example-create-topic.html](https://docs.amazonaws.cn/sdk-for-ruby/v3/developer-guide/sns-example-create-topic.html)。
+  有关 API 的详细信息，请参阅 *适用于 Ruby 的 Amazon SDK API 参考[CreateTopic](https://docs.amazonaws.cn/goto/SdkForRubyV3/sns-2010-03-31/CreateTopic)*中的。

------
#### [ Rust ]

**适用于 Rust 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
async fn make_topic(client: &Client, topic_name: &str) -> Result<(), Error> {
    let resp = client.create_topic().name(topic_name).send().await?;

    println!(
        "Created topic with ARN: {}",
        resp.topic_arn().unwrap_or_default()
    );

    Ok(())
}
```
+  有关 API 的详细信息，请参阅适用[CreateTopic](https://docs.rs/aws-sdk-sns/latest/aws_sdk_sns/client/struct.Client.html#method.create_topic)于 *Rust 的Amazon SDK API 参考*。

------
#### [ SAP ABAP ]

**适用于 SAP ABAP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    TRY.
        oo_result = lo_sns->createtopic( iv_name = iv_topic_name ). " oo_result is returned for testing purposes. "
        MESSAGE 'SNS topic created' TYPE 'I'.
      CATCH /aws1/cx_snstopiclimitexcdex.
        MESSAGE 'Unable to create more topics. You have reached the maximum number of topics allowed.' TYPE 'E'.
    ENDTRY.
```
+  有关 API 的详细信息，请参阅适用[CreateTopic](https://docs.amazonaws.cn/sdk-for-sap-abap/v1/api/latest/index.html)于 S *AP 的Amazon SDK ABAP API 参考*。

------
#### [ Swift ]

**适用于 Swift 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import AWSSNS

        let config = try await SNSClient.SNSClientConfiguration(region: region)
        let snsClient = SNSClient(config: config)

        let output = try await snsClient.createTopic(
            input: CreateTopicInput(name: name)
        )

        guard let arn = output.topicArn else {
            print("No topic ARN returned by Amazon SNS.")
            return
        }
```
+  如需了解 API 的详细信息，请参阅适用[CreateTopic](https://sdk.amazonaws.com/swift/api/awssns/latest/documentation/awssns/snsclient/createtopic(input:))于 S *wift 的Amazon SDK API 参考*。

------

# 在 Amazon SNS 主题中创建订阅
步骤 2：创建主题订阅

要接收发布至[某个主题](sns-create-topic.md)的消息，您必须*订阅*一个到该主题的[终端节点](#sns-endpoints)。在为终端节点订阅主题后，此终端节点会开始接收发布到关联主题的消息。

**注意**  
HTTP (S) 端点、电子邮件地址和其他 Amazon 资源 Amazon Web Services 账户 需要确认订阅后才能接收消息。

## 要为终端节点订阅 Amazon SNS 主题


通过为端点订阅 Amazon SNS 主题，可以将消息传送到指定的端点，从而确保当向该主题发布消息时，正确的系统或用户会收到通知。此步骤对于将主题与使用者（无论他们是应用程序、电子邮件收件人还是其他服务）联系起来至关重要，从而实现跨系统的无缝通信。

1. 登录 [Amazon SNS 控制台](https://console.amazonaws.cn/sns/home)。

1. 在左侧导航窗格中，选择**订阅**。

1. 在**订阅**页面上，选择**创建订阅**。

1. 在**创建订阅**页上的**详细信息**部分，执行以下操作：

   1. 对于 **Topic ARN**（主题 ARN），选择主题的 Amazon Resource Name（ARN）。例如，此值是您创建 Amazon SNS 主题时生成的 Amazon ARN。`arn:aws:sns:us-east-2:123456789012:your_topic`

   1. 对于 **Protocol**（协议），选择终端节点类型。可用的终端节点类型包括：
      + [**HTTP/HTTPS**](sns-http-https-endpoint-as-subscriber.md)
      + [**电子邮件/电子邮件-JSON**](sns-email-notifications.md)
      + [****](sns-firehose-as-subscriber.md)
      + [**Amazon SQS**](sns-sqs-as-subscriber.md)
**注意**  
要订阅到 [SNS FIFO 主题](sns-fifo-topics.md)，请选择该选项。
      + [**Amazon Lambda**](sns-lambda-as-subscriber.md)
      + [**平台应用程序终端节点**](sns-mobile-application-as-subscriber.md)
      + [**短信**](sns-mobile-phone-number-as-subscriber.md) 

   1. 对于 **Endpoint**（终端节点），输入终端节点值，例如电子邮件地址或 Amazon SQS 队列的 ARN。

   1. 仅限于 Firehose 端点：对于**订阅角色 ARN**，指定您为写入到 Firehose 传输流创建的 IAM 角色的 ARN。有关更多信息，请参阅 [订阅 Firehose 传输流到 Amazon SNS 主题的先决条件](prereqs-kinesis-data-firehose.md)。

   1. （可选）对于 Firehose、Amazon SQS HTTP/S 、终端节点，您还可以启用原始消息传输。有关更多信息，请参阅 [Amazon SNS 原始消息传输](sns-large-payload-raw-message-delivery.md)。

   1. （可选）要配置筛选策略，请展开 **Subscription filter policy**（订阅筛选策略）部分。有关更多信息，请参阅 [Amazon SNS 订阅筛选策略](sns-subscription-filter-policies.md)。

   1. （可选）要启用基于有效负载的筛选，请将 `Filter Policy Scope` 配置为 `MessageBody`。有关更多信息，请参阅 [Amazon SNS 订阅筛选策略范围](sns-message-filtering-scope.md)。

   1. （可选）要为订阅配置死信队列，请展开 **Redrive policy (dead-letter queue)**（重新驱动策略（死信队列））部分。有关更多信息，请参阅 [Amazon SNS 死信队列](sns-dead-letter-queues.md)。

   1. 选择**创建订阅**。

      控制台将创建订阅并打开订阅的 **Details**（详细信息）页面。

# 发布 Amazon SNS 消息
步骤 3：发布消息

在[创建 Amazon SNS 主题](sns-create-topic.md)并为终端节点[订阅](sns-create-subscribe-endpoint-to-topic.md)主题后，可以将消息*发布*到主题。发布消息时，Amazon SNS 会尝试将消息传输给订阅的[终端节点](sns-create-subscribe-endpoint-to-topic.md#sns-endpoints)。

## 要使用 Amazon SNS 主题发布消息 Amazon Web Services 管理控制台
Amazon Web Services 管理控制台

1. 登录 [Amazon SNS 控制台](https://console.amazonaws.cn/sns/home)。

1. 在左侧导航窗格中，选择**主题**。

1. 在 **Topics**（主题）页上，选择一个主题，然后选择**Publish message**（发布主题）。

   控制台将打开 **Publish message to topic**（将消息发布到主题）页面。

1. 在 **Message details（消息详细信息）**部分中，执行以下操作：

   1. （可选）输入消息 **Subject**（主题）。

   1. 对于 [FIFO topic](sns-fifo-topics.md)（FIFO 主题），输入 **Message group ID**（消息组 ID）。FIFO 主题需要**消息组 ID**。同一消息组中的消息按消息的发布顺序传输。

   1. （可选）对于标准主题，请输入**消息组 ID**。此 ID 会转发给所有 Amazon SQS 标准订阅，不用于或发送到任何其他端点类型。

   1. 对于 FIFO 主题，请输入 **Message deduplication ID**（消息重复数据删除 ID）。如果您为主题启用了 **Content-based message deduplication**（基于内容的消息重复数据删除）设置，则此 ID 为可选项。

   1. （可选）对于 [mobile push notifications](sns-ttl.md)（移动推送通知），输入 **Time to Live (TTL)**（存活时间 (TTL)）值（以秒为单位）。这是推送通知服务（例如 Apple 推送通知服务 (APNs) 或 Firebase Cloud Messaging (FCM)）将消息传递到端点所需的时间。

1. 在 **Message body (消息正文)** 部分中，执行以下操作之一：

   1. 选择 **Identical payload for all delivery protocols（完全相同负载用于所有交付协议）**，然后输入消息。

   1. 选择 **Custom payload for each delivery protocol（对每个交付协议使用自定义负载）**，然后输入 JSON 对象定义要发送给每个协议的消息。

      有关更多信息，请参阅 [使用特定于平台的有效载荷发布 Amazon SNS 通知](sns-send-custom-platform-specific-payloads-mobile-devices.md)。

1. 在 **Message attributes（消息属性）**部分中，添加您希望 Amazon SNS 与订阅属性 `FilterPolicy` 相匹配的任何属性，以确定订阅的终端节点是否对发布的消息感兴趣。

   1. 对于 **Type**（类型），选择属性类型，例如 **String.Array**。
**注意**  
对于属性类型 **String.Array**，请将该数组放入方括号 (`[]`) 内。在该数组内，将字符串值加入双引号内。数字以及关键字 `true`、`false` 和 `null` 无需加引号。

   1. 输入属性**名称**，例如 `customer_interests`。

   1. 输入属性**值**，例如 `["soccer", "rugby", "hockey"]`。

   如果属性类型为 **String**、**String.Array** 或 **Number**，Amazon SNS 会首先依据订阅的[筛选策略](sns-message-filtering.md)（如果存在）来评估该消息属性，然后再将消息发送至该订阅，前提是筛选策略范围没有明确设置为 `MessageBody`。

   有关更多信息，请参阅 [Amazon SNS 消息属性](sns-message-attributes.md)。

1. 选择**发布消息**。

   消息将发布到主题，且控制台将打开主题的 **Details**（详细信息）页面。

## 使用 Amazon SDK 向主题发布消息
Amazon SDKs

要使用 S Amazon DK，必须使用您的凭据对其进行配置。有关更多信息，请参阅[和*工具参考指南中的共享配置Amazon SDKs 和*凭据文件](https://docs.amazonaws.cn/sdkref/latest/guide/creds-config-files.html)。

以下代码示例演示如何使用 `Publish`。

------
#### [ .NET ]

**适用于 .NET 的 Amazon SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/SNS#code-examples)中查找完整示例，了解如何进行设置和运行。
向主题发布消息。  

```
    using System;
    using System.Threading.Tasks;
    using Amazon.SimpleNotificationService;
    using Amazon.SimpleNotificationService.Model;

    /// <summary>
    /// This example publishes a message to an Amazon Simple Notification
    /// Service (Amazon SNS) topic.
    /// </summary>
    public class PublishToSNSTopic
    {
        public static async Task Main()
        {
            string topicArn = "arn:aws:sns:us-east-2:000000000000:ExampleSNSTopic";
            string messageText = "This is an example message to publish to the ExampleSNSTopic.";

            IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();

            await PublishToTopicAsync(client, topicArn, messageText);
        }

        /// <summary>
        /// Publishes a message to an Amazon SNS topic.
        /// </summary>
        /// <param name="client">The initialized client object used to publish
        /// to the Amazon SNS topic.</param>
        /// <param name="topicArn">The ARN of the topic.</param>
        /// <param name="messageText">The text of the message.</param>
        public static async Task PublishToTopicAsync(
            IAmazonSimpleNotificationService client,
            string topicArn,
            string messageText)
        {
            var request = new PublishRequest
            {
                TopicArn = topicArn,
                Message = messageText,
            };

            var response = await client.PublishAsync(request);

            Console.WriteLine($"Successfully published message ID: {response.MessageId}");
        }
    }
```
使用组、复制和属性选项向主题发布消息。  

```
    /// <summary>
    /// Publish messages using user settings.
    /// </summary>
    /// <returns>Async task.</returns>
    public static async Task PublishMessages()
    {
        Console.WriteLine("Now we can publish messages.");

        var keepSendingMessages = true;
        string? deduplicationId = null;
        string? toneAttribute = null;
        while (keepSendingMessages)
        {
            Console.WriteLine();
            var message = GetUserResponse("Enter a message to publish.", "This is a sample message");

            if (_useFifoTopic)
            {
                Console.WriteLine("Because you are using a FIFO topic, you must set a message group ID." +
                                  "\r\nAll messages within the same group will be received in the order " +
                                  "they were published.");

                Console.WriteLine();
                var messageGroupId = GetUserResponse("Enter a message group ID for this message:", "1");

                if (!_useContentBasedDeduplication)
                {
                    Console.WriteLine("Because you are not using content-based deduplication, " +
                                      "you must enter a deduplication ID.");

                    Console.WriteLine("Enter a deduplication ID for this message.");
                    deduplicationId = GetUserResponse("Enter a deduplication ID for this message.", "1");
                }

                if (GetYesNoResponse("Add an attribute to this message?"))
                {
                    Console.WriteLine("Enter a number for an attribute.");
                    for (int i = 0; i < _tones.Length; i++)
                    {
                        Console.WriteLine($"\t{i + 1}. {_tones[i]}");
                    }

                    var selection = GetUserResponse("", "1");
                    int.TryParse(selection, out var selectionNumber);

                    if (selectionNumber > 0 && selectionNumber < _tones.Length)
                    {
                        toneAttribute = _tones[selectionNumber - 1];
                    }
                }

                var messageID = await SnsWrapper.PublishToTopicWithAttribute(
                    _topicArn, message, "tone", toneAttribute, deduplicationId, messageGroupId);

                Console.WriteLine($"Message published with id {messageID}.");
            }

            keepSendingMessages = GetYesNoResponse("Send another message?", false);
        }
    }
```
将用户的选择应用于发布操作。  

```
    /// <summary>
    /// Publish a message to a topic with an attribute and optional deduplication and group IDs.
    /// </summary>
    /// <param name="topicArn">The ARN of the topic.</param>
    /// <param name="message">The message to publish.</param>
    /// <param name="attributeName">The optional attribute for the message.</param>
    /// <param name="attributeValue">The optional attribute value for the message.</param>
    /// <param name="deduplicationId">The optional deduplication ID for the message.</param>
    /// <param name="groupId">The optional group ID for the message.</param>
    /// <returns>The ID of the message published.</returns>
    public async Task<string> PublishToTopicWithAttribute(
        string topicArn,
        string message,
        string? attributeName = null,
        string? attributeValue = null,
        string? deduplicationId = null,
        string? groupId = null)
    {
        var publishRequest = new PublishRequest()
        {
            TopicArn = topicArn,
            Message = message,
            MessageDeduplicationId = deduplicationId,
            MessageGroupId = groupId
        };

        if (attributeValue != null)
        {
            // Add the string attribute if it exists.
            publishRequest.MessageAttributes =
                new Dictionary<string, MessageAttributeValue>
                {
                    { attributeName!, new MessageAttributeValue() { StringValue = attributeValue, DataType = "String"} }
                };
        }

        var publishResponse = await _amazonSNSClient.PublishAsync(publishRequest);
        return publishResponse.MessageId;
    }
```
+  有关 API 详细信息，请参阅《适用于 .NET 的 Amazon SDK API Reference》**中的 [Publish](https://docs.amazonaws.cn/goto/DotNetSDKV3/sns-2010-03-31/Publish)。

------
#### [ C\$1\$1 ]

**SDK for C\$1\$1**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
//! Send a message to an Amazon Simple Notification Service (Amazon SNS) topic.
/*!
  \param message: The message to publish.
  \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic.
  \param clientConfiguration: AWS client configuration.
  \return bool: Function succeeded.
 */
bool AwsDoc::SNS::publishToTopic(const Aws::String &message,
                                 const Aws::String &topicARN,
                                 const Aws::Client::ClientConfiguration &clientConfiguration) {
    Aws::SNS::SNSClient snsClient(clientConfiguration);

    Aws::SNS::Model::PublishRequest request;
    request.SetMessage(message);
    request.SetTopicArn(topicARN);

    const Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request);

    if (outcome.IsSuccess()) {
        std::cout << "Message published successfully with id '"
                  << outcome.GetResult().GetMessageId() << "'." << std::endl;
    }
    else {
        std::cerr << "Error while publishing message "
                  << outcome.GetError().GetMessage()
                  << std::endl;
    }

    return outcome.IsSuccess();
}
```
发布带有属性的消息。  

```
        static const Aws::String TONE_ATTRIBUTE("tone");
        static const Aws::Vector<Aws::String> TONES = {"cheerful", "funny", "serious",
                                                       "sincere"};

        Aws::Client::ClientConfiguration clientConfig;
        // Optional: Set to the AWS Region (overrides config file).
        // clientConfig.region = "us-east-1";

    Aws::SNS::SNSClient snsClient(clientConfiguration);

        Aws::SNS::Model::PublishRequest request;
        request.SetTopicArn(topicARN);
        Aws::String message = askQuestion("Enter a message text to publish.  ");
        request.SetMessage(message);

        if (filteringMessages && askYesNoQuestion(
                "Add an attribute to this message? (y/n) ")) {
            for (size_t i = 0; i < TONES.size(); ++i) {
                std::cout << "  " << (i + 1) << ". " << TONES[i] << std::endl;
            }
            int selection = askQuestionForIntRange(
                    "Enter a number for an attribute. ",
                    1, static_cast<int>(TONES.size()));
            Aws::SNS::Model::MessageAttributeValue messageAttributeValue;
            messageAttributeValue.SetDataType("String");
            messageAttributeValue.SetStringValue(TONES[selection - 1]);
            request.AddMessageAttributes(TONE_ATTRIBUTE, messageAttributeValue);
        }

        Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request);

        if (outcome.IsSuccess()) {
            std::cout << "Your message was successfully published." << std::endl;
        }
        else {
            std::cerr << "Error with TopicsAndQueues::Publish. "
                      << outcome.GetError().GetMessage()
                      << std::endl;

            cleanUp(topicARN,
                    queueURLS,
                    subscriptionARNS,
                    snsClient,
                    sqsClient);

            return false;
        }
```
+  有关 API 详细信息，请参阅《适用于 C\$1\$1 的 Amazon SDK API Reference》**中的 [Publish](https://docs.amazonaws.cn/goto/SdkForCpp/sns-2010-03-31/Publish)。

------
#### [ CLI ]

**Amazon CLI**  
**示例 1：向主题发布消息**  
以下 `publish` 示例将指定消息发布到指定 SNS 主题。该消息来自一个文本文件，您可以在该文件中包含换行符。  

```
aws sns publish \
    --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic" \
    --message file://message.txt
```
`message.txt` 的内容：  

```
Hello World
Second Line
```
输出：  

```
{
    "MessageId": "123a45b6-7890-12c3-45d6-111122223333"
}
```
**示例 2：向电话号码发布 SMS 消息**  
以下 `publish` 示例将消息 `Hello world!` 发布到电话号码 `+1-555-555-0100`。  

```
aws sns publish \
    --message "Hello world!" \
    --phone-number +1-555-555-0100
```
输出：  

```
{
    "MessageId": "123a45b6-7890-12c3-45d6-333322221111"
}
```
+  有关 API 详细信息，请参阅《Amazon CLI Command Reference》**中的 [Publish](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/publish.html)。

------
#### [ Go ]

**适用于 Go 的 SDK V2**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/workflows/topics_and_queues#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import (
	"context"
	"encoding/json"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/sns"
	"github.com/aws/aws-sdk-go-v2/service/sns/types"
)

// SnsActions encapsulates the Amazon Simple Notification Service (Amazon SNS) actions
// used in the examples.
type SnsActions struct {
	SnsClient *sns.Client
}



// Publish publishes a message to an Amazon SNS topic. The message is then sent to all
// subscribers. When the topic is a FIFO topic, the message must also contain a group ID
// and, when ID-based deduplication is used, a deduplication ID. An optional key-value
// filter attribute can be specified so that the message can be filtered according to
// a filter policy.
func (actor SnsActions) Publish(ctx context.Context, topicArn string, message string, groupId string, dedupId string, filterKey string, filterValue string) error {
	publishInput := sns.PublishInput{TopicArn: aws.String(topicArn), Message: aws.String(message)}
	if groupId != "" {
		publishInput.MessageGroupId = aws.String(groupId)
	}
	if dedupId != "" {
		publishInput.MessageDeduplicationId = aws.String(dedupId)
	}
	if filterKey != "" && filterValue != "" {
		publishInput.MessageAttributes = map[string]types.MessageAttributeValue{
			filterKey: {DataType: aws.String("String"), StringValue: aws.String(filterValue)},
		}
	}
	_, err := actor.SnsClient.Publish(ctx, &publishInput)
	if err != nil {
		log.Printf("Couldn't publish message to topic %v. Here's why: %v", topicArn, err)
	}
	return err
}
```
+  有关 API 详细信息，请参阅《适用于 Go 的 Amazon SDK API Reference》**中的 [Publish](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/sns#Client.Publish)。

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

**适用于 Java 的 SDK 2.x**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sns.model.PublishResponse;
import software.amazon.awssdk.services.sns.model.SnsException;

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class PublishTopic {
    public static void main(String[] args) {
        final String usage = """

                Usage:    <message> <topicArn>

                Where:
                   message - The message text to send.
                   topicArn - The ARN of the topic to publish.
                """;

        if (args.length != 2) {
            System.out.println(usage);
            System.exit(1);
        }

        String message = args[0];
        String topicArn = args[1];
        SnsClient snsClient = SnsClient.builder()
                .region(Region.US_EAST_1)
                .build();
        pubTopic(snsClient, message, topicArn);
        snsClient.close();
    }

    public static void pubTopic(SnsClient snsClient, String message, String topicArn) {
        try {
            PublishRequest request = PublishRequest.builder()
                    .message(message)
                    .topicArn(topicArn)
                    .build();

            PublishResponse result = snsClient.publish(request);
            System.out
                    .println(result.messageId() + " Message sent. Status is " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}
```
+  有关 API 详细信息，请参阅《Amazon SDK for Java 2.x API Reference》**中的 [Publish](https://docs.amazonaws.cn/goto/SdkForJavaV2/sns-2010-03-31/Publish)。

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

**适用于 JavaScript (v3) 的软件开发工具包**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/sns#code-examples) 中查找完整示例，了解如何进行设置和运行。
在单独的模块中创建客户端并将其导出。  

```
import { SNSClient } from "@aws-sdk/client-sns";

// The AWS Region can be provided here using the `region` property. If you leave it blank
// the SDK will default to the region set in your AWS config.
export const snsClient = new SNSClient({});
```
导入 SDK 和客户端模块，然后调用 API。  

```
import { PublishCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";

/**
 * @param {string | Record<string, any>} message - The message to send. Can be a plain string or an object
 *                                                 if you are using the `json` `MessageStructure`.
 * @param {string} topicArn - The ARN of the topic to which you would like to publish.
 */
export const publish = async (
  message = "Hello from SNS!",
  topicArn = "TOPIC_ARN",
) => {
  const response = await snsClient.send(
    new PublishCommand({
      Message: message,
      TopicArn: topicArn,
    }),
  );
  console.log(response);
  // {
  //   '$metadata': {
  //     httpStatusCode: 200,
  //     requestId: 'e7f77526-e295-5325-9ee4-281a43ad1f05',
  //     extendedRequestId: undefined,
  //     cfId: undefined,
  //     attempts: 1,
  //     totalRetryDelay: 0
  //   },
  //   MessageId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  // }
  return response;
};
```
使用组、复制和属性选项向主题发布消息。  

```
  async publishMessages() {
    const message = await this.prompter.input({
      message: MESSAGES.publishMessagePrompt,
    });

    let groupId;
    let deduplicationId;
    let choices;

    if (this.isFifo) {
      await this.logger.log(MESSAGES.groupIdNotice);
      groupId = await this.prompter.input({
        message: MESSAGES.groupIdPrompt,
      });

      if (this.autoDedup === false) {
        await this.logger.log(MESSAGES.deduplicationIdNotice);
        deduplicationId = await this.prompter.input({
          message: MESSAGES.deduplicationIdPrompt,
        });
      }

      choices = await this.prompter.checkbox({
        message: MESSAGES.messageAttributesPrompt,
        choices: toneChoices,
      });
    }

    await this.snsClient.send(
      new PublishCommand({
        TopicArn: this.topicArn,
        Message: message,
        ...(groupId
          ? {
              MessageGroupId: groupId,
            }
          : {}),
        ...(deduplicationId
          ? {
              MessageDeduplicationId: deduplicationId,
            }
          : {}),
        ...(choices
          ? {
              MessageAttributes: {
                tone: {
                  DataType: "String.Array",
                  StringValue: JSON.stringify(choices),
                },
              },
            }
          : {}),
      }),
    );

    const publishAnother = await this.prompter.confirm({
      message: MESSAGES.publishAnother,
    });

    if (publishAnother) {
      await this.publishMessages();
    }
  }
```
+  有关更多信息，请参阅《适用于 JavaScript 的 Amazon SDK 开发人员指南》[https://docs.amazonaws.cn/sdk-for-javascript/v3/developer-guide/sns-examples-publishing-messages.html](https://docs.amazonaws.cn/sdk-for-javascript/v3/developer-guide/sns-examples-publishing-messages.html)。
+  有关 API 详细信息，请参阅 *适用于 JavaScript 的 Amazon SDK API 参考*中的 [Publish](https://docs.amazonaws.cn/AWSJavaScriptSDK/v3/latest/client/sns/command/PublishCommand)。

------
#### [ Kotlin ]

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
suspend fun pubTopic(
    topicArnVal: String,
    messageVal: String,
) {
    val request =
        PublishRequest {
            message = messageVal
            topicArn = topicArnVal
        }

    SnsClient.fromEnvironment { region = "us-east-1" }.use { snsClient ->
        val result = snsClient.publish(request)
        println("${result.messageId} message sent.")
    }
}
```
+  有关 API 详细信息，请参阅《Amazon SDK for Kotlin API Reference》**中的 [Publish](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

------
#### [ PHP ]

**适用于 PHP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;


/**
 * Sends a message to an Amazon SNS topic.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

$SnSclient = new SnsClient([
    'profile' => 'default',
    'region' => 'us-east-1',
    'version' => '2010-03-31'
]);

$message = 'This message is sent from a Amazon SNS code sample.';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';

try {
    $result = $SnSclient->publish([
        'Message' => $message,
        'TopicArn' => $topic,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```
+  有关更多信息，请参阅《适用于 PHP 的 Amazon SDK 开发人员指南》[https://docs.amazonaws.cn/sdk-for-php/v3/developer-guide/sns-examples-subscribing-unsubscribing-topics.html#publish-a-message-to-an-sns-topic](https://docs.amazonaws.cn/sdk-for-php/v3/developer-guide/sns-examples-subscribing-unsubscribing-topics.html#publish-a-message-to-an-sns-topic)。
+  有关 API 的详细信息，请参阅 *适用于 PHP 的 Amazon SDK API 参考*中的 [Publish](https://docs.amazonaws.cn/goto/SdkForPHPV3/sns-2010-03-31/Publish)。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：此示例显示发布一条 MessageAttribute 声明为内联的消息。**  

```
Publish-SNSMessage -TopicArn "arn:aws:sns:us-west-2:123456789012:my-topic" -Message "Hello" -MessageAttribute @{'City'=[Amazon.SimpleNotificationService.Model.MessageAttributeValue]@{DataType='String'; StringValue ='AnyCity'}}
```
**示例 2：此示例显示发布一条事先 MessageAttributes 声明了多个消息的情况。**  

```
$cityAttributeValue = New-Object Amazon.SimpleNotificationService.Model.MessageAttributeValue
$cityAttributeValue.DataType = "String"
$cityAttributeValue.StringValue = "AnyCity"

$populationAttributeValue = New-Object Amazon.SimpleNotificationService.Model.MessageAttributeValue
$populationAttributeValue.DataType = "Number"
$populationAttributeValue.StringValue = "1250800"

$messageAttributes = New-Object System.Collections.Hashtable
$messageAttributes.Add("City", $cityAttributeValue)
$messageAttributes.Add("Population", $populationAttributeValue)

Publish-SNSMessage -TopicArn "arn:aws:sns:us-west-2:123456789012:my-topic" -Message "Hello" -MessageAttribute $messageAttributes
```
+  有关 API 详细信息，请参阅《Amazon Tools for PowerShell Cmdlet Reference (V4)》**中的 [Publish](https://docs.amazonaws.cn/powershell/v4/reference)。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例显示发布一条 MessageAttribute 声明为内联的消息。**  

```
Publish-SNSMessage -TopicArn "arn:aws:sns:us-west-2:123456789012:my-topic" -Message "Hello" -MessageAttribute @{'City'=[Amazon.SimpleNotificationService.Model.MessageAttributeValue]@{DataType='String'; StringValue ='AnyCity'}}
```
**示例 2：此示例显示发布一条事先 MessageAttributes 声明了多个消息的情况。**  

```
$cityAttributeValue = New-Object Amazon.SimpleNotificationService.Model.MessageAttributeValue
$cityAttributeValue.DataType = "String"
$cityAttributeValue.StringValue = "AnyCity"

$populationAttributeValue = New-Object Amazon.SimpleNotificationService.Model.MessageAttributeValue
$populationAttributeValue.DataType = "Number"
$populationAttributeValue.StringValue = "1250800"

$messageAttributes = New-Object System.Collections.Hashtable
$messageAttributes.Add("City", $cityAttributeValue)
$messageAttributes.Add("Population", $populationAttributeValue)

Publish-SNSMessage -TopicArn "arn:aws:sns:us-west-2:123456789012:my-topic" -Message "Hello" -MessageAttribute $messageAttributes
```
+  有关 API 详细信息，请参阅《Amazon Tools for PowerShell Cmdlet Reference (V5)》**中的 [Publish](https://docs.amazonaws.cn/powershell/v5/reference)。

------
#### [ Python ]

**适用于 Python 的 SDK（Boto3）**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。
发布包含属性的消息，以便订阅可以根据属性进行筛选。  

```
class SnsWrapper:
    """Encapsulates Amazon SNS topic and subscription functions."""

    def __init__(self, sns_resource):
        """
        :param sns_resource: A Boto3 Amazon SNS resource.
        """
        self.sns_resource = sns_resource


    @staticmethod
    def publish_message(topic, message, attributes):
        """
        Publishes a message, with attributes, to a topic. Subscriptions can be filtered
        based on message attributes so that a subscription receives messages only
        when specified attributes are present.

        :param topic: The topic to publish to.
        :param message: The message to publish.
        :param attributes: The key-value attributes to attach to the message. Values
                           must be either `str` or `bytes`.
        :return: The ID of the message.
        """
        try:
            att_dict = {}
            for key, value in attributes.items():
                if isinstance(value, str):
                    att_dict[key] = {"DataType": "String", "StringValue": value}
                elif isinstance(value, bytes):
                    att_dict[key] = {"DataType": "Binary", "BinaryValue": value}
            response = topic.publish(Message=message, MessageAttributes=att_dict)
            message_id = response["MessageId"]
            logger.info(
                "Published message with attributes %s to topic %s.",
                attributes,
                topic.arn,
            )
        except ClientError:
            logger.exception("Couldn't publish message to topic %s.", topic.arn)
            raise
        else:
            return message_id
```
发布基于订阅用户的协议采取不同形式的消息。  

```
class SnsWrapper:
    """Encapsulates Amazon SNS topic and subscription functions."""

    def __init__(self, sns_resource):
        """
        :param sns_resource: A Boto3 Amazon SNS resource.
        """
        self.sns_resource = sns_resource


    @staticmethod
    def publish_multi_message(
        topic, subject, default_message, sms_message, email_message
    ):
        """
        Publishes a multi-format message to a topic. A multi-format message takes
        different forms based on the protocol of the subscriber. For example,
        an SMS subscriber might receive a short version of the message
        while an email subscriber could receive a longer version.

        :param topic: The topic to publish to.
        :param subject: The subject of the message.
        :param default_message: The default version of the message. This version is
                                sent to subscribers that have protocols that are not
                                otherwise specified in the structured message.
        :param sms_message: The version of the message sent to SMS subscribers.
        :param email_message: The version of the message sent to email subscribers.
        :return: The ID of the message.
        """
        try:
            message = {
                "default": default_message,
                "sms": sms_message,
                "email": email_message,
            }
            response = topic.publish(
                Message=json.dumps(message), Subject=subject, MessageStructure="json"
            )
            message_id = response["MessageId"]
            logger.info("Published multi-format message to topic %s.", topic.arn)
        except ClientError:
            logger.exception("Couldn't publish message to topic %s.", topic.arn)
            raise
        else:
            return message_id
```

```
class SnsWrapper:
    """Wrapper class for managing Amazon SNS operations."""

    def __init__(self, sns_client: Any) -> None:
        """
        Initialize the SnsWrapper.

        :param sns_client: A Boto3 Amazon SNS client.
        """
        self.sns_client = sns_client

    @classmethod
    def from_client(cls) -> 'SnsWrapper':
        """
        Create an SnsWrapper instance using a default boto3 client.

        :return: An instance of this class.
        """
        sns_client = boto3.client('sns')
        return cls(sns_client)


    def publish_message(
        self,
        topic_arn: str,
        message: str,
        tone_attribute: Optional[str] = None,
        deduplication_id: Optional[str] = None,
        message_group_id: Optional[str] = None
    ) -> str:
        """
        Publish a message to an SNS topic.

        :param topic_arn: The ARN of the SNS topic.
        :param message: The message content to publish.
        :param tone_attribute: Optional tone attribute for message filtering.
        :param deduplication_id: Optional deduplication ID for FIFO topics.
        :param message_group_id: Optional message group ID for FIFO topics.
        :return: The message ID of the published message.
        :raises ClientError: If the message publication fails.
        """
        try:
            publish_args = {
                'TopicArn': topic_arn,
                'Message': message
            }

            # Add message attributes if tone is specified
            if tone_attribute:
                publish_args['MessageAttributes'] = {
                    'tone': {
                        'DataType': 'String',
                        'StringValue': tone_attribute
                    }
                }

            # Add FIFO-specific parameters
            if message_group_id:
                publish_args['MessageGroupId'] = message_group_id

            if deduplication_id:
                publish_args['MessageDeduplicationId'] = deduplication_id

            response = self.sns_client.publish(**publish_args)

            message_id = response['MessageId']
            logger.info(f"Published message to topic {topic_arn} with ID: {message_id}")
            return message_id

        except ClientError as e:
            error_code = e.response.get('Error', {}).get('Code', 'Unknown')
            logger.error(f"Error publishing message to topic: {error_code} - {e}")
            raise
```
+  有关 API 详细信息，请参阅《Amazon SDK for Python (Boto3) API Reference》**中的 [Publish](https://docs.amazonaws.cn/goto/boto3/sns-2010-03-31/Publish)。

------
#### [ Ruby ]

**适用于 Ruby 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/ruby/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
# Service class for sending messages using Amazon Simple Notification Service (SNS)
class SnsMessageSender
  # Initializes the SnsMessageSender with an SNS client
  #
  # @param sns_client [Aws::SNS::Client] The SNS client
  def initialize(sns_client)
    @sns_client = sns_client
    @logger = Logger.new($stdout)
  end

  # Sends a message to a specified SNS topic
  #
  # @param topic_arn [String] The ARN of the SNS topic
  # @param message [String] The message to send
  # @return [Boolean] true if message was successfully sent, false otherwise
  def send_message(topic_arn, message)
    @sns_client.publish(topic_arn: topic_arn, message: message)
    @logger.info("Message sent successfully to #{topic_arn}.")
    true
  rescue Aws::SNS::Errors::ServiceError => e
    @logger.error("Error while sending the message: #{e.message}")
    false
  end
end

# Example usage:
if $PROGRAM_NAME == __FILE__
  topic_arn = 'SNS_TOPIC_ARN' # Should be replaced with a real topic ARN
  message = 'MESSAGE'         # Should be replaced with the actual message content

  sns_client = Aws::SNS::Client.new
  message_sender = SnsMessageSender.new(sns_client)

  @logger.info('Sending message.')
  unless message_sender.send_message(topic_arn, message)
    @logger.error('Message sending failed. Stopping program.')
    exit 1
  end
end
```
+  有关更多信息，请参阅《适用于 Ruby 的 Amazon SDK 开发人员指南》[https://docs.amazonaws.cn/sdk-for-ruby/v3/developer-guide/sns-example-send-message.html](https://docs.amazonaws.cn/sdk-for-ruby/v3/developer-guide/sns-example-send-message.html)。
+  有关 API 详细信息，请参阅 *适用于 Ruby 的 Amazon SDK API 参考*中的 [Publish](https://docs.amazonaws.cn/goto/SdkForRubyV3/sns-2010-03-31/Publish)。

------
#### [ Rust ]

**SDK for Rust**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
async fn subscribe_and_publish(
    client: &Client,
    topic_arn: &str,
    email_address: &str,
) -> Result<(), Error> {
    println!("Receiving on topic with ARN: `{}`", topic_arn);

    let rsp = client
        .subscribe()
        .topic_arn(topic_arn)
        .protocol("email")
        .endpoint(email_address)
        .send()
        .await?;

    println!("Added a subscription: {:?}", rsp);

    let rsp = client
        .publish()
        .topic_arn(topic_arn)
        .message("hello sns!")
        .send()
        .await?;

    println!("Published message: {:?}", rsp);

    Ok(())
}
```
+  有关 API 详细信息，请参阅《Amazon SDK for Rust API Reference》**中的 [Publish](https://docs.rs/aws-sdk-sns/latest/aws_sdk_sns/client/struct.Client.html#method.publish)。

------
#### [ SAP ABAP ]

**适用于 SAP ABAP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    TRY.
        oo_result = lo_sns->publish(              " oo_result is returned for testing purposes. "
          iv_topicarn = iv_topic_arn
          iv_message = iv_message ).
        MESSAGE 'Message published to SNS topic.' TYPE 'I'.
      CATCH /aws1/cx_snsnotfoundexception.
        MESSAGE 'Topic does not exist.' TYPE 'E'.
    ENDTRY.
```
发布带有主题属性的消息。  

```
    TRY.
        oo_result = lo_sns->publish(              " oo_result is returned for testing purposes. "
          iv_topicarn = iv_topic_arn
          iv_message = iv_message
          it_messageattributes = it_msg_attrs ).
        MESSAGE 'Message with attributes published to SNS topic.' TYPE 'I'.
      CATCH /aws1/cx_snsnotfoundexception.
        MESSAGE 'Topic does not exist.' TYPE 'E'.
    ENDTRY.
```
向主题发布多格式消息。  

```
    " Build JSON message structure for multi-format message
    DATA(lv_json_message) = |\{ "default": "{ iv_default_message }", "sms": "{ iv_sms_message }", "email": "{ iv_email_message }" \}|.

    TRY.
        oo_result = lo_sns->publish(              " oo_result is returned for testing purposes. "
          iv_topicarn = iv_topic_arn
          iv_message = lv_json_message
          iv_subject = iv_subject
          iv_messagestructure = 'json' ).
        MESSAGE 'Multi-format message published to SNS topic.' TYPE 'I'.
      CATCH /aws1/cx_snsnotfoundexception.
        MESSAGE 'Topic does not exist.' TYPE 'E'.
    ENDTRY.
```
+  有关 API 详细信息，请参阅《Amazon SDK for SAP ABAP API Reference》**中的 [Publish](https://docs.amazonaws.cn/sdk-for-sap-abap/v1/api/latest/index.html)。

------
#### [ Swift ]

**适用于 Swift 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import AWSSNS

        let config = try await SNSClient.SNSClientConfiguration(region: region)
        let snsClient = SNSClient(config: config)

        let output = try await snsClient.publish(
            input: PublishInput(
                message: message,
                topicArn: arn
            )
        )

        guard let messageId = output.messageId else {
            print("No message ID received from Amazon SNS.")
            return
        }
        
        print("Published message with ID \(messageId)")
```
+  有关 API 详细信息，请参阅 *Amazon SDK for Swift API Reference* 中的 [Publish](https://sdk.amazonaws.com/swift/api/awssns/latest/documentation/awssns/snsclient/publish(input:))。

------

# 通过 Amazon SNS 和 Amazon S3 发布大型消息
大型消息负载

要发布很大的 Amazon SNS 消息，您可以使用[适用于 Java 的 Amazon SNS 扩展型客户端库](https://github.com/awslabs/amazon-sns-java-extended-client-lib/)或[适用于 Python 的 Amazon SNS 扩展型客户端库](https://github.com/awslabs/amazon-sns-python-extended-client-lib)。对于大于当前最大值 256KB（最大为 2GB）的消息，这些库非常有用。这两个库将实际有效负载保存到 Amazon S3 桶，并将存储的 Amazon S3 对象的引用发布到 Amazon SNS 主题。订阅的 Amazon SQS 队列可以使用[适用于 Java 的 Amazon SQS 扩展客户端库](https://github.com/awslabs/amazon-sqs-java-extended-client-lib)从 Amazon S3 中取消引用并检索负载。其他端点（如 Lambda）可以使用 [Amazon的有效负载卸载 Java 公共库](https://github.com/awslabs/payload-offloading-java-common-lib-for-aws)来取消引用并检索有效负载。

**注意**  
Amazon SNS 扩展型客户端库与标准主题和 FIFO 主题都兼容。

# 适用于 Java 的 Amazon SNS 扩展型客户端库


## 先决条件


以下是使用[适用于 Java 的 Amazon SNS 扩展型客户端库](https://github.com/awslabs/amazon-sns-java-extended-client-lib)的先决条件：
+ 一个 Amazon 软件开发工具包。本页上的示例使用 Amazon Java 开发工具包。要安装和设置 SDK，请参阅《*适用于 Java 的 Amazon SDK 开发者指南*[》中的 “设置 Amazon 适用于 Java 的 SDK](https://docs.amazonaws.cn/sdk-for-java/latest/developer-guide/setup-install.html)”。
+ 并 Amazon Web Services 账户 具有正确的凭据。要创建 Amazon Web Services 账户，请导航到[Amazon 主页](https://www.amazonaws.cn/)，然后选择**创建 Amazon 帐户**。按照说明进行操作。

  有关证书的信息，请参阅《*适用于 Java 的 Amazon SDK 开发人员指南》中的 “设置用于开发*[的 Amazon 凭证和区域](https://docs.amazonaws.cn/sdk-for-java/latest/developer-guide/setup-credentials.html)”。
+ Java 8 或更高版本。
+ 适用于 Java 的 Amazon SNS 扩展型客户端库（也可从 [Maven](https://maven.apache.org/) 中获得）。

## 配置消息存储


Amazon SNS 扩展客户端库使用负载卸载 Java 公共库 Amazon 进行消息存储和检索。您可以配置以下 Amazon S3 [消息存储选项](https://github.com/awslabs/amazon-sns-java-extended-client-lib/blob/main/src/main/java/software/amazon/sns/SNSExtendedClientConfiguration.java)：
+ **自定义消息大小阈值** – 具有超过此大小的有效载荷和属性的消息将自动存储在 Amazon S3 中。
+ **`alwaysThroughS3` 标志** – 将此值设置为 `true` 以强制将所有消息有效载荷存储在 Amazon S3 中。例如：

  ```
  SNSExtendedClientConfiguration snsExtendedClientConfiguration = new
  SNSExtendedClientConfiguration() .withPayloadSupportEnabled(s3Client, BUCKET_NAME).withAlwaysThroughS3(true);
  ```
+ **自定义 KMS 密钥** – 用于 Amazon S3 存储桶中的服务器端加密的密钥。
+ **存储桶名称** – 用于存储消息有效载荷的 Amazon S3 存储桶的名称。

## 示例：使用存储在 Amazon S3 中的负载将消息发布到 Amazon SNS


以下代码示例展示了如何：
+ 创建示例主题和队列。
+ 订阅队列以接收来自主题的消息。
+ 发布测试消息。

消息负载存储在 Amazon S3，以及发布到的引用中。Amazon SQS 扩展客户端用于接收消息。

**适用于 Java 的 SDK 1.x**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/java/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。
要发布大型消息，请使用适用于 Java 的 Amazon SNS 扩展客户端库。您发送的消息将引用包含实际消息内容的 Amazon S3 对象。  

```
import com.amazon.sqs.javamessaging.AmazonSQSExtendedClient;
import com.amazon.sqs.javamessaging.ExtendedClientConfiguration;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.SetSubscriptionAttributesRequest;
import com.amazonaws.services.sns.util.Topics;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import software.amazon.sns.AmazonSNSExtendedClient;
import software.amazon.sns.SNSExtendedClientConfiguration;

public class Example {

        public static void main(String[] args) {
                final String BUCKET_NAME = "extended-client-bucket";
                final String TOPIC_NAME = "extended-client-topic";
                final String QUEUE_NAME = "extended-client-queue";
                final Regions region = Regions.DEFAULT_REGION;

                // Message threshold controls the maximum message size that will be allowed to
                // be published
                // through SNS using the extended client. Payload of messages exceeding this
                // value will be stored in
                // S3. The default value of this parameter is 256 KB which is the maximum
                // message size in SNS (and SQS).
                final int EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD = 32;

                // Initialize SNS, SQS and S3 clients
                final AmazonSNS snsClient = AmazonSNSClientBuilder.standard().withRegion(region).build();
                final AmazonSQS sqsClient = AmazonSQSClientBuilder.standard().withRegion(region).build();
                final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build();

                // Create bucket, topic, queue and subscription
                s3Client.createBucket(BUCKET_NAME);
                final String topicArn = snsClient.createTopic(
                                new CreateTopicRequest().withName(TOPIC_NAME)).getTopicArn();
                final String queueUrl = sqsClient.createQueue(
                                new CreateQueueRequest().withQueueName(QUEUE_NAME)).getQueueUrl();
                final String subscriptionArn = Topics.subscribeQueue(
                                snsClient, sqsClient, topicArn, queueUrl);

                // To read message content stored in S3 transparently through SQS extended
                // client,
                // set the RawMessageDelivery subscription attribute to TRUE
                final SetSubscriptionAttributesRequest subscriptionAttributesRequest = new SetSubscriptionAttributesRequest();
                subscriptionAttributesRequest.setSubscriptionArn(subscriptionArn);
                subscriptionAttributesRequest.setAttributeName("RawMessageDelivery");
                subscriptionAttributesRequest.setAttributeValue("TRUE");
                snsClient.setSubscriptionAttributes(subscriptionAttributesRequest);

                // Initialize SNS extended client
                // PayloadSizeThreshold triggers message content storage in S3 when the
                // threshold is exceeded
                // To store all messages content in S3, use AlwaysThroughS3 flag
                final SNSExtendedClientConfiguration snsExtendedClientConfiguration = new SNSExtendedClientConfiguration()
                                .withPayloadSupportEnabled(s3Client, BUCKET_NAME)
                                .withPayloadSizeThreshold(EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD);
                final AmazonSNSExtendedClient snsExtendedClient = new AmazonSNSExtendedClient(snsClient,
                                snsExtendedClientConfiguration);

                // Publish message via SNS with storage in S3
                final String message = "This message is stored in S3 as it exceeds the threshold of 32 bytes set above.";
                snsExtendedClient.publish(topicArn, message);

                // Initialize SQS extended client
                final ExtendedClientConfiguration sqsExtendedClientConfiguration = new ExtendedClientConfiguration()
                                .withPayloadSupportEnabled(s3Client, BUCKET_NAME);
                final AmazonSQSExtendedClient sqsExtendedClient = new AmazonSQSExtendedClient(sqsClient,
                                sqsExtendedClientConfiguration);

                // Read the message from the queue
                final ReceiveMessageResult result = sqsExtendedClient.receiveMessage(queueUrl);
                System.out.println("Received message is " + result.getMessages().get(0).getBody());
        }
}
```

## 其他终端节点协议


Amazon SNS 和 Amazon SQS 库都使用 [Amazon的负载卸载 Java 公共库](https://github.com/awslabs/payload-offloading-java-common-lib-for-aws)通过 Amazon S3 存储和检索消息负载。任何启用 Java 的终端节点（例如，在 Java 中实施的 HTTPS 终端节点）都可以使用相同的库来取消引用消息内容。

无法使用有效负载卸载 Java 公共库的终端节点仍然 Amazon 可以发布存储在 Amazon S3 中的有效负载的消息。以下是由上面的代码示例发布的 Amazon S3 引用的示例：

```
[
  "software.amazon.payloadoffloading.PayloadS3Pointer",
  {
    "s3BucketName": "extended-client-bucket",
    "s3Key": "xxxx-xxxxx-xxxxx-xxxxxx"
  }
]
```

# 适用于 Python 的 Amazon SNS 扩展型客户端库


## 先决条件


以下是使用[适用于 Python 的 Amazon SNS 扩展型客户端库](https://github.com/awslabs/amazon-sns-python-extended-client-lib)的先决条件：
+ 一个 Amazon 软件开发工具包。本页上的示例使用 Amazon Python SDK Boto3。要安装和设置 SDK，请参阅 [https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html) 文档。
+ 并 Amazon Web Services 账户 具有正确的凭据。要创建 Amazon Web Services 账户，请导航到[Amazon 主页](https://www.amazonaws.cn/)，然后选择**创建 Amazon 帐户**。按照说明进行操作。

  有关凭证的信息，请参阅《Amazon SDK for Python 开发人员指南》**中的[凭证](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html)。
+ Python 3.x（或更高版本）和 pip。
+ 适用于 Python 的 Amazon SNS 扩展型客户端库（也可从 [PyPI](https://pypi.org/project/amazon-sns-extended-client/) 中获得）。

## 配置消息存储


Boto3 Amazon [SN](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html#client) S 客户端[、主题[PlatformEndpoint](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns/platformendpoint/index.html)和对象上提供了以下属性，](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns/topic/index.html)用于配置 Amazon S3 消息存储选项。
+ **`large_payload_support`** – 用于存储大型消息的 Amazon S3 存储桶名称。
+ **`use_legacy_attribute`** – 如果设置为 `True`，则所有已发布的消息都使用旧版保留消息属性（`SQSLargePayloadSize`），而不是当前的保留消息属性（`ExtendedPayloadSize`）。
+ **`message_size_threshold`** – 在大型消息桶中存储消息的阈值。不能低于 `0` 或超过 `262144`。默认值为 `262144`。
+ **`always_through_s3`** – 如果为 `True`，则所有消息都存储在 Amazon S3 中。默认值为 `False`。
+ **`s3_client`** – 用于在 Amazon S3 中存储对象的 Boto3 Amazon S3 `client` 对象。如果您想要控制 Amazon S3 客户端（例如自定义 Amazon S3 配置或凭证），请使用此选项。首次使用时，若未预先设置，默认值为 `boto3.client("s3")`。

## 示例：使用存储在 Amazon S3 中的有效负载将消息发布到 Amazon SNS


以下代码示例展示了如何：
+ 创建示例 Amazon SNS 主题和 Amazon SQS 队列。
+ 为 Amazon SQS 队列附加策略以接收来自 Amazon SNS 主题的消息。
+ 订阅队列以接收来自主题的消息。
+ 使用 Amazon SNS 扩展客户端、主题资源和 PlatformEndpoint 资源发布测试消息。
+ 消息有效负载存储在 Amazon S3 中，并发布对它的引用。
+ 打印队列中已发布的消息以及从 Amazon S3 检索到的原始消息。

要发布大型消息，请使用适用于 Python 的 Amazon SNS 扩展型客户端库。您发送的消息将引用包含实际消息内容的 Amazon S3 对象。

```
import boto3
from sns_extended_client import SNSExtendedClientSession
from json import loads

s3_extended_payload_bucket = "extended-client-bucket-store"  # S3 bucket with the given bucket name is a resource which is created and accessible with the given AWS credentials
TOPIC_NAME = "---TOPIC-NAME---"
QUEUE_NAME = "---QUEUE-NAME---"

def allow_sns_to_write_to_sqs(topicarn, queuearn):
    policy_document = """{{
        "Version": "2012-10-17",		 	 	 
        "Statement":[
            {{
            "Sid":"MyPolicy",
            "Effect":"Allow",
            "Principal" : {{"AWS" : "*"}},
            "Action":"SQS:SendMessage",
            "Resource": "{}",
            "Condition":{{
                "ArnEquals":{{
                "aws:SourceArn": "{}"
                }}
            }}
            }}
        ]
        }}""".format(queuearn, topicarn)

    return policy_document

def get_msg_from_s3(body,sns_extended_client):
    """Handy Helper to fetch message from S3"""
    json_msg = loads(body)
    s3_object = sns_extended_client.s3_client.get_object(
        Bucket=json_msg[1].get("s3BucketName"), Key=json_msg[1].get("s3Key")
    )
    msg = s3_object.get("Body").read().decode()
    return msg


def fetch_and_print_from_sqs(sqs, queue_url,sns_extended_client):
    sqs_msg = sqs.receive_message(
        QueueUrl=queue_url,
        AttributeNames=['All'],
        MessageAttributeNames=['All'],
        VisibilityTimeout=0,
        WaitTimeSeconds=0,
        MaxNumberOfMessages=1
    ).get("Messages")[0]
    
    message_body = sqs_msg.get("Body")
    print("Published Message: {}".format(message_body))
    print("Message Stored in S3 Bucket is: {}\n".format(get_msg_from_s3(message_body,sns_extended_client)))

    # Delete the Processed Message
    sqs.delete_message(
        QueueUrl=queue_url,
        ReceiptHandle=sqs_msg['ReceiptHandle']
    )


sns_extended_client = boto3.client("sns", region_name="us-east-1")
create_topic_response = sns_extended_client.create_topic(Name=TOPIC_NAME)
sns_topic_arn = create_topic_response.get("TopicArn")

# create and subscribe an sqs queue to the sns client
sqs = boto3.client("sqs",region_name="us-east-1")
demo_queue_url = sqs.create_queue(QueueName=QUEUE_NAME).get("QueueUrl")
sqs_queue_arn = sqs.get_queue_attributes(
    QueueUrl=demo_queue_url, AttributeNames=["QueueArn"]
)["Attributes"].get("QueueArn")

# Adding policy to SQS queue such that SNS topic can send msg to SQS queue
policy_json = allow_sns_to_write_to_sqs(sns_topic_arn, sqs_queue_arn)
response = sqs.set_queue_attributes(
    QueueUrl = demo_queue_url,
    Attributes = {
        'Policy' : policy_json
    }
)

# Set the RawMessageDelivery subscription attribute to TRUE if you want to use
# SQSExtendedClient to help with retrieving msg from S3
sns_extended_client.subscribe(TopicArn=sns_topic_arn, Protocol="sqs", 
Endpoint=sqs_queue_arn
, Attributes={"RawMessageDelivery":"true"}
)

sns_extended_client.large_payload_support = s3_extended_payload_bucket

# Change default s3_client attribute of sns_extended_client to use 'us-east-1' region
sns_extended_client.s3_client = boto3.client("s3", region_name="us-east-1")


# Below is the example that all the messages will be sent to the S3 bucket
sns_extended_client.always_through_s3 = True
sns_extended_client.publish(
    TopicArn=sns_topic_arn, Message="This message should be published to S3"
)
print("\n\nPublished using SNS extended client:")
fetch_and_print_from_sqs(sqs, demo_queue_url,sns_extended_client)  # Prints message stored in s3

# Below is the example that all the messages larger than 32 bytes will be sent to the S3 bucket
print("\nUsing decreased message size threshold:")

sns_extended_client.always_through_s3 = False
sns_extended_client.message_size_threshold = 32
sns_extended_client.publish(
    TopicArn=sns_topic_arn,
    Message="This message should be published to S3 as it exceeds the limit of the 32 bytes",
)

fetch_and_print_from_sqs(sqs, demo_queue_url,sns_extended_client)  # Prints message stored in s3


# Below is the example to publish message using the SNS.Topic resource
sns_extended_client_resource = SNSExtendedClientSession().resource(
    "sns", region_name="us-east-1"
)

topic = sns_extended_client_resource.Topic(sns_topic_arn)
topic.large_payload_support = s3_extended_payload_bucket

# Change default s3_client attribute of topic to use 'us-east-1' region
topic.s3_client = boto3.client("s3", region_name="us-east-1")

topic.always_through_s3 = True
# Can Set custom S3 Keys to be used to store objects in S3
topic.publish(
    Message="This message should be published to S3 using the topic resource",
    MessageAttributes={
        "S3Key": {
            "DataType": "String",
            "StringValue": "347c11c4-a22c-42e4-a6a2-9b5af5b76587",
        }
    },
)
print("\nPublished using Topic Resource:")
fetch_and_print_from_sqs(sqs, demo_queue_url,topic)

# Below is the example to publish message using the SNS.PlatformEndpoint resource
sns_extended_client_resource = SNSExtendedClientSession().resource(
    "sns", region_name="us-east-1"
)

platform_endpoint = sns_extended_client_resource.PlatformEndpoint(sns_topic_arn)
platform_endpoint.large_payload_support = s3_extended_payload_bucket

# Change default s3_client attribute of platform_endpoint to use 'us-east-1' region
platform_endpoint.s3_client = boto3.client("s3", region_name="us-east-1")

platform_endpoint.always_through_s3 = True
# Can Set custom S3 Keys to be used to store objects in S3
platform_endpoint.publish(
    Message="This message should be published to S3 using the PlatformEndpoint resource",
    MessageAttributes={
        "S3Key": {
            "DataType": "String",
            "StringValue": "247c11c4-a22c-42e4-a6a2-9b5af5b76587",
        }
    },
)
print("\nPublished using PlatformEndpoint Resource:")
fetch_and_print_from_sqs(sqs, demo_queue_url,platform_endpoint)
```

**输出**

```
Published using SNS extended client:
Published Message: ["software.amazon.payloadoffloading.PayloadS3Pointer", {"s3BucketName": "extended-client-bucket-store", "s3Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}]
Message Stored in S3 Bucket is: This message should be published to S3

Using decreased message size threshold:
Published Message: ["software.amazon.payloadoffloading.PayloadS3Pointer", {"s3BucketName": "extended-client-bucket-store", "s3Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}]
Message Stored in S3 Bucket is: This message should be published to S3 as it exceeds the limit of the 32 bytes

Published using Topic Resource:
Published Message: ["software.amazon.payloadoffloading.PayloadS3Pointer", {"s3BucketName": "extended-client-bucket-store", "s3Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}]
Message Stored in S3 Bucket is: This message should be published to S3 using the topic resource

Published using PlatformEndpoint Resource:
Published Message: ["software.amazon.payloadoffloading.PayloadS3Pointer", {"s3BucketName": "extended-client-bucket-store", "s3Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}]
Message Stored in S3 Bucket is: This message should be published to S3 using the PlatformEndpoint resource
```

# Amazon SNS 消息属性
消息属性

Amazon SNS 支持传输消息属性，这些属性支持您提供与消息相关的结构化元数据项目（如时间戳、地理空间数据、签名和标识符）。对于 SQS 订阅，在启用 [Raw Message Delivery](sns-large-payload-raw-message-delivery.md)（原始消息传输）时，最多可以发送 10 个消息属性。要发送 10 个以上的消息属性，必须禁用 Raw Message Delivery（原始消息传输）。所具有的消息属性（定向到启用了原始消息传输的 Amazon SQS 订阅）超过 10 个的消息将作为客户端错误被丢弃。

消息属性是可选的，并独立于消息正文（但随之一起发送）。接收方可以使用此信息来决定如何处理消息，而不必先处理消息正文。

有关使用 Amazon Web Services 管理控制台 或发送带有属性的消息的信息 适用于 Java 的 Amazon SDK，请参阅[要使用 Amazon SNS 主题发布消息 Amazon Web Services 管理控制台](sns-publishing.md#sns-publishing-messages)教程。

**注意**  
当消息结构为 String 而不是 JSON 时，仅发送消息属性。

您还可以使用消息属性，帮助构造移动终端节点的推送通知消息。在这种情况下，消息属性仅用于帮助构造推送通知消息。这些属性不会被传递到终端节点，就像在发送带有消息属性的消息到 Amazon SQS 终端节点时一样。

您还可以使用消息属性来让消息变为可通过订阅筛选策略进行筛选。可以将筛选策略应用于主题订阅。应用了筛选策略且筛选策略范围设置为 `MessageAttributes`（默认值）时，订阅将只接收具有策略接受的属性的那些消息。有关更多信息，请参阅 [Amazon SNS 消息筛选](sns-message-filtering.md)。

**注意**  
使用消息属性进行筛选时，该值必须是有效的 JSON 字符串。这样做可以确保将消息传送到启用了消息属性筛选的订阅。

## 消息属性项目和验证


每个消息属性包含以下项目：
+ **Name** – 消息属性的名称可以包含以下字符：A-Z、a-z、0-9、下划线 (\$1)、连字符 (-) 和句点 (.)。名称不得以句点开头或结尾，并且不应包含连续句点。名称区分大小写，且必须在消息的所有属性名称中是唯一的。名称最多可以有 256 个字符。名称不能以 `AWS.` 或 `Amazon.` （或任何大小写变化形式）开头，因为这些前缀已预留以供 Amazon Web Services 使用。
+ **Type** – 受支持的消息属性数据类型有 `String`、`String.Array`、`Number` 和 `Binary`。数据类型在内容方面具有与消息正文相同的限制。想要了解更多信息，请参阅[消息属性数据类型和验证](#SNSMessageAttributes.DataTypes)部分。
+ **Value** – 用户指定的消息属性值。对于字符串数据类型，值属性的内容必须遵循与消息正文相同的内容限制。不过，如果消息属性用于过滤，则该值必须是有效的 JSON 字符串，以确保与 Amazon SNS 订阅筛选策略兼容。有关更多信息，请参阅 *Amazon Simple Notification Service API 参考*中的[发布](https://docs.amazonaws.cn/sns/latest/api/API_Publish.html)操作。

名称、类型和值都不得为空或 null。此外，消息正文也不应为空或 null。消息属性的所有部分 (包括名称、类型和值) 都包含在消息大小限制中，该限制当前是 256 KB。

## 消息属性数据类型和验证
数据类型

消息属性数据类型指示 Amazon SNS 处理消息属性值的方式。例如，如果类型是数字，则 Amazon SNS 会验证它是否为数字。

除非另有说明，否则 Amazon SNS 支持所有终端节点的以下逻辑数据类型：
+ **String** – 字符串是使用 UTF-8 二进制编码的 Unicode。有关代码值的列表，请参阅 [http://en.wikipedia。 org/wiki/ASCII](http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters)\$1ASCII\$1printable\$1characters。
**注意**  
消息属性中不支持代理值。例如，使用代理值来表示表情符号将收到以下错误：`Invalid attribute value was passed in for message attribute`。
+ **String.Array** – 格式为字符串的阵列，可以包含多个值。这些值可以是字符串、数字或关键字 `true`、`false` 和 `null`。数字或布尔类型的 String.Array 不需要引号。多个 String.Array 值用逗号分隔。

   Amazon Lambda 订阅不支持此数据类型。如果您为 Lambda 终端节点指定此数据类型，它会作为 Amazon SNS 传输给 Lambda 的 JSON 负载中的 `String` 数据类型传递。
+ **Number** – 数字是正或负整数或是浮点数。数字具有足够的范围和精度，以便包含整数、浮点数和双精度数通常支持的大多数可能值。数字的值可以介于 -109 到 109 之间，精确至小数点后 5 位数。系统会删减开头和结尾的 0。

   Amazon Lambda 订阅不支持此数据类型。如果您为 Lambda 终端节点指定此数据类型，它会作为 Amazon SNS 传输给 Lambda 的 JSON 负载中的 `String` 数据类型传递。
+ **Binary** – 二进制类型属性可以存储任何二进制数据，例如压缩数据、加密数据或图像。

## 为移动推送通知预留的消息属性


下表列出了可用于构造推送通知消息的移动推送通知服务的预留消息属性：

[\[See the AWS documentation website for more details\]](http://docs.amazonaws.cn/sns/latest/dg/sns-message-attributes.html)

1 如果消息属性不符合要求，Apple 将拒绝 Amazon SNS 通知。如需了解更多详情，请参阅 Apple 开发者网站上的 “[向发送通知请求](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns)”。 APNs

# Amazon SNS 消息批处理
消息批处理

## 什么是消息批处理？


在各个 `Publish` API 请求中向标准或 FIFO 主题发布消息的替代方法，该方法使用 Amazon SNS `PublishBatch` API 在单个 API 请求中最多发布 10 条消息。批量发送消息可以帮助您利用 Amazon SNS 将与连接分布式应用程序（[A2A 消息收发](sns-system-to-system-messaging.md)）或者向人们发送通知（[A2P 消息收发](sns-user-notifications.md)）相关的成本最高降低 10 倍。根据您运营所在区域，Amazon SNS 对每秒可以向某个主题发布的消息数量设定了配额。有关 API 限额的更多信息，请参阅《Amazon Web Services 一般参考》**中的 [Amazon SNS 端点和限额](https://docs.amazonaws.cn/general/latest/gr/sns.html)页面。

**注意**  
您在单个 `PublishBatch` API 请求中发送的所有消息的总合计大小不能超过 262144 字节（256 KiB）。  
`PublishBatch` API 将相同的 `Publish` API 操作用于 IAM 策略。

## 消息批处理是如何工作的？


使用 `PublishBatch` API 发布消息与使用 `Publish` API 发布消息类似。主要的区别在于，需要给 `PublishBatch` API 请求中的每条消息分配一个唯一的批处理 ID（最多 80 个字符）。这样，Amazon SNS 可以为批处理中的每条消息返回单独的 API 响应，以确认每条消息均已发布或发生故障。对于发布到 FIFO 主题的消息，除了包括分配唯一的批处理 ID 之外，还需要为每条单独的消息包括 `MessageDeduplicationID` 和 `MessageGroupId`。

## 示例


**向标准主题发布一批 10 条的消息**

```
// Imports
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.PublishBatchRequest;
import software.amazon.awssdk.services.sns.model.PublishBatchRequestEntry;
import software.amazon.awssdk.services.sns.model.PublishBatchResponse;
import software.amazon.awssdk.services.sns.model.SnsException;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

// Code
private static final int MAX_BATCH_SIZE = 10;

public static void publishBatchToTopic(SnsClient snsClient, String topicArn, int batchSize) {
    try {
        // Validate the batch size
        if (batchSize > MAX_BATCH_SIZE) {
            throw new IllegalArgumentException("Batch size cannot exceed " + MAX_BATCH_SIZE);
        }

        // Create the batch entries
        List<PublishBatchRequestEntry> entries = IntStream.range(0, batchSize)
                .mapToObj(i -> PublishBatchRequestEntry.builder()
                        .id("id" + i)
                        .message("message" + i)
                        .build())
                .collect(Collectors.toList());

        // Build the batch request
        PublishBatchRequest request = PublishBatchRequest.builder()
                .topicArn(topicArn)
                .publishBatchRequestEntries(entries)
                .build();

        // Publish the batch request
        PublishBatchResponse response = snsClient.publishBatch(request);

        // Handle successful messages
        response.successful().forEach(success -> {
            System.out.println("Successful Batch Id: " + success.id());
            System.out.println("Message Id: " + success.messageId());
        });

        // Handle failed messages
        response.failed().forEach(failure -> {
            System.err.println("Failed Batch Id: " + failure.id());
            System.err.println("Error Code: " + failure.code());
            System.err.println("Sender Fault: " + failure.senderFault());
            System.err.println("Error Message: " + failure.message());
        });

    } catch (SnsException e) {
        // Log and handle exceptions
        System.err.println("SNS Exception: " + e.awsErrorDetails().errorMessage());
    } catch (IllegalArgumentException e) {
        System.err.println("Validation Error: " + e.getMessage());
    }
}
```

**向 FIFO 主题发布一批 10 条的消息**

```
// Imports
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.PublishBatchRequest;
import software.amazon.awssdk.services.sns.model.PublishBatchRequestEntry;
import software.amazon.awssdk.services.sns.model.PublishBatchResponse;
import software.amazon.awssdk.services.sns.model.BatchResultErrorEntry;
import software.amazon.awssdk.services.sns.model.SnsException;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

// Code
private static final int MAX_BATCH_SIZE = 10;

public static void publishBatchToFifoTopic(SnsClient snsClient, String topicArn) {
    try {
        // Create the batch entries to send
        List<PublishBatchRequestEntry> entries = IntStream.range(0, MAX_BATCH_SIZE)
                .mapToObj(i -> PublishBatchRequestEntry.builder()
                        .id("id" + i)
                        .message("message" + i)
                        .messageGroupId("groupId")
                        .messageDeduplicationId("deduplicationId" + i)
                        .build())
                .collect(Collectors.toList());

        // Create the batch request
        PublishBatchRequest request = PublishBatchRequest.builder()
                .topicArn(topicArn)
                .publishBatchRequestEntries(entries)
                .build();

        // Publish the batch request
        PublishBatchResponse response = snsClient.publishBatch(request);

        // Handle the successfully sent messages
        response.successful().forEach(success -> {
            System.out.println("Batch Id for successful message: " + success.id());
            System.out.println("Message Id for successful message: " + success.messageId());
            System.out.println("Sequence Number for successful message: " + success.sequenceNumber());
        });

        // Handle the failed messages
        response.failed().forEach(failure -> {
            System.err.println("Batch Id for failed message: " + failure.id());
            System.err.println("Error Code for failed message: " + failure.code());
            System.err.println("Sender Fault for failed message: " + failure.senderFault());
            System.err.println("Failure Message for failed message: " + failure.message());
        });

    } catch (SnsException e) {
        // Handle any exceptions from the request
        System.err.println("SNS Exception: " + e.awsErrorDetails().errorMessage());
    }
}
```

# 删除 Amazon SNS 订阅和主题
第 4 步：删除订阅和主题

删除主题后，其关联订阅会异步删除。虽然客户仍然可以访问这些订阅，但即使您使用相同的名称重新创建主题，这些订阅也不再与该主题相关联。如果发布者尝试向已删除的主题发布消息，发布者将收到一条错误消息，指出该主题不存在。同样，任何订阅已删除主题的尝试也会导致错误消息。您无法删除正在等待确认的订阅。Amazon SNS 会在 48 小时后自动删除未经确认的订阅，但电子邮件订阅除外，这些订阅会在 30 天后删除。

**重要**  
 已删除的 Amazon SNS 主题无法恢复。我们建议您在删除主题之前先删除订阅。  
 订阅删除后将无法恢复。已发布的 Amazon SNS 主题的通知将不会发送到该已删除的订阅。您必须重新创建订阅才能接收通知。

## 要删除 Amazon SNS 主题或订阅，请使用 Amazon Web Services 管理控制台
Amazon Web Services 管理控制台

删除 Amazon SNS 主题或订阅可确保高效的资源管理、防止不必要的使用并保持 Amazon SNS 控制台井然有序。此步骤有助于避免闲置资源带来的潜在成本，并通过移除不再需要的主题或订阅来简化管理。

**要使用删除主题 Amazon Web Services 管理控制台**

1. 登录 [Amazon SNS 控制台](https://console.amazonaws.cn/sns/home)。

1. 在左侧导航窗格中，选择**主题**。

1. 在 **Topics**（主题）页面上，选择一个主题，然后选择 **Delete**（删除）。

1. 在 **Delete topic**（删除主题）对话框中，输入 `delete me`，然后选择 **Delete**（删除）。

   控制台将删除该主题。

**要删除订阅，请使用 Amazon Web Services 管理控制台**

1. 登录 [Amazon SNS 控制台](https://console.amazonaws.cn/sns/home)。

1. 在左侧导航窗格中，选择**订阅**。

1. 在**订阅**页面上，选择状态为**已确认**的订阅，然后选择**删除**。

1. 在 **Delete subscription**（删除订阅）对话框中，选择 **Delete**（删除）。

   控制台将删除该订阅。

## 使用 Amazon SDK 删除订阅和主题
Amazon SDKs

要使用 S Amazon DK，必须使用您的凭据对其进行配置。有关更多信息，请参阅[和*工具参考指南中的共享配置Amazon SDKs 和*凭据文件](https://docs.amazonaws.cn/sdkref/latest/guide/creds-config-files.html)。

以下代码示例演示如何使用 `DeleteTopic`。

------
#### [ .NET ]

**适用于 .NET 的 Amazon SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/cross-service/TopicsAndQueues#code-examples)中查找完整示例，了解如何进行设置和运行。
按主题 ARN 删除主题。  

```
    /// <summary>
    /// Delete a topic by its topic ARN.
    /// </summary>
    /// <param name="topicArn">The ARN of the topic.</param>
    /// <returns>True if successful.</returns>
    public async Task<bool> DeleteTopicByArn(string topicArn)
    {
        var deleteResponse = await _amazonSNSClient.DeleteTopicAsync(
            new DeleteTopicRequest()
            {
                TopicArn = topicArn
            });
        return deleteResponse.HttpStatusCode == HttpStatusCode.OK;
    }
```
+  有关 API 的详细信息，请参阅 *适用于 .NET 的 Amazon SDK API 参考[DeleteTopic](https://docs.amazonaws.cn/goto/DotNetSDKV3/sns-2010-03-31/DeleteTopic)*中的。

------
#### [ C\$1\$1 ]

**SDK for C\$1\$1**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
//! Delete an Amazon Simple Notification Service (Amazon SNS) topic.
/*!
  \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic.
  \param clientConfiguration: AWS client configuration.
  \return bool: Function succeeded.
 */
bool AwsDoc::SNS::deleteTopic(const Aws::String &topicARN,
                              const Aws::Client::ClientConfiguration &clientConfiguration) {
    Aws::SNS::SNSClient snsClient(clientConfiguration);

    Aws::SNS::Model::DeleteTopicRequest request;
    request.SetTopicArn(topicARN);

    const Aws::SNS::Model::DeleteTopicOutcome outcome = snsClient.DeleteTopic(request);

    if (outcome.IsSuccess()) {
        std::cout << "Successfully deleted the Amazon SNS topic " << topicARN << std::endl;
    }
    else {
        std::cerr << "Error deleting topic " << topicARN << ":" <<
                  outcome.GetError().GetMessage() << std::endl;
    }

    return outcome.IsSuccess();
}
```
+  有关 API 的详细信息，请参阅 *适用于 C\$1\$1 的 Amazon SDK API 参考[DeleteTopic](https://docs.amazonaws.cn/goto/SdkForCpp/sns-2010-03-31/DeleteTopic)*中的。

------
#### [ CLI ]

**Amazon CLI**  
**删除 SNS 主题**  
以下 `delete-topic` 示例将删除指定的 SNS 主题。  

```
aws sns delete-topic \
    --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"
```
此命令不生成任何输出。  
+  有关 API 的详细信息，请参阅*Amazon CLI 命令参考[DeleteTopic](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/delete-topic.html)*中的。

------
#### [ Go ]

**适用于 Go 的 SDK V2**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/workflows/topics_and_queues#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import (
	"context"
	"encoding/json"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/sns"
	"github.com/aws/aws-sdk-go-v2/service/sns/types"
)

// SnsActions encapsulates the Amazon Simple Notification Service (Amazon SNS) actions
// used in the examples.
type SnsActions struct {
	SnsClient *sns.Client
}



// DeleteTopic delete an Amazon SNS topic.
func (actor SnsActions) DeleteTopic(ctx context.Context, topicArn string) error {
	_, err := actor.SnsClient.DeleteTopic(ctx, &sns.DeleteTopicInput{
		TopicArn: aws.String(topicArn)})
	if err != nil {
		log.Printf("Couldn't delete topic %v. Here's why: %v\n", topicArn, err)
	}
	return err
}
```
+  有关 API 的详细信息，请参阅 *适用于 Go 的 Amazon SDK API 参考[DeleteTopic](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/sns#Client.DeleteTopic)*中的。

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

**适用于 Java 的 SDK 2.x**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.DeleteTopicRequest;
import software.amazon.awssdk.services.sns.model.DeleteTopicResponse;
import software.amazon.awssdk.services.sns.model.SnsException;

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class DeleteTopic {
    public static void main(String[] args) {
        final String usage = """

                Usage:     <topicArn>

                Where:
                   topicArn - The ARN of the topic to delete.
                """;

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String topicArn = args[0];
        SnsClient snsClient = SnsClient.builder()
                .region(Region.US_EAST_1)
                .build();

        System.out.println("Deleting a topic with name: " + topicArn);
        deleteSNSTopic(snsClient, topicArn);
        snsClient.close();
    }

    public static void deleteSNSTopic(SnsClient snsClient, String topicArn) {
        try {
            DeleteTopicRequest request = DeleteTopicRequest.builder()
                    .topicArn(topicArn)
                    .build();

            DeleteTopicResponse result = snsClient.deleteTopic(request);
            System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode());

        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}
```
+  有关 API 的详细信息，请参阅 *Amazon SDK for Java 2.x API 参考[DeleteTopic](https://docs.amazonaws.cn/goto/SdkForJavaV2/sns-2010-03-31/DeleteTopic)*中的。

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

**适用于 JavaScript (v3) 的软件开发工具包**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/sns#code-examples) 中查找完整示例，了解如何进行设置和运行。
在单独的模块中创建客户端并将其导出。  

```
import { SNSClient } from "@aws-sdk/client-sns";

// The AWS Region can be provided here using the `region` property. If you leave it blank
// the SDK will default to the region set in your AWS config.
export const snsClient = new SNSClient({});
```
导入 SDK 和客户端模块，然后调用 API。  

```
import { DeleteTopicCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";

/**
 * @param {string} topicArn - The ARN of the topic to delete.
 */
export const deleteTopic = async (topicArn = "TOPIC_ARN") => {
  const response = await snsClient.send(
    new DeleteTopicCommand({ TopicArn: topicArn }),
  );
  console.log(response);
  // {
  //   '$metadata': {
  //     httpStatusCode: 200,
  //     requestId: 'a10e2886-5a8f-5114-af36-75bd39498332',
  //     extendedRequestId: undefined,
  //     cfId: undefined,
  //     attempts: 1,
  //     totalRetryDelay: 0
  //   }
  // }
};
```
+  有关更多信息，请参阅[《适用于 JavaScript 的 Amazon SDK Developer Guide》](https://docs.amazonaws.cn/sdk-for-javascript/v3/developer-guide/sns-examples-managing-topics.html#sns-examples-managing-topics-deletetopic)。
+  有关 API 的详细信息，请参阅 *适用于 JavaScript 的 Amazon SDK API 参考[DeleteTopic](https://docs.amazonaws.cn/AWSJavaScriptSDK/v3/latest/client/sns/command/DeleteTopicCommand)*中的。

------
#### [ Kotlin ]

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
suspend fun deleteSNSTopic(topicArnVal: String) {
    val request =
        DeleteTopicRequest {
            topicArn = topicArnVal
        }

    SnsClient.fromEnvironment { region = "us-east-1" }.use { snsClient ->
        snsClient.deleteTopic(request)
        println("$topicArnVal was successfully deleted.")
    }
}
```
+  有关 API 的详细信息，请参阅适用[DeleteTopic](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的Amazon SDK API 参考*。

------
#### [ PHP ]

**适用于 PHP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;


/**
 * Deletes an SNS topic and all its subscriptions.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

$SnSclient = new SnsClient([
    'profile' => 'default',
    'region' => 'us-east-1',
    'version' => '2010-03-31'
]);

$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';

try {
    $result = $SnSclient->deleteTopic([
        'TopicArn' => $topic,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```
+  有关 API 的详细信息，请参阅 *适用于 PHP 的 Amazon SDK API 参考[DeleteTopic](https://docs.amazonaws.cn/goto/SdkForPHPV3/sns-2010-03-31/DeleteTopic)*中的。

------
#### [ Python ]

**适用于 Python 的 SDK（Boto3）**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
class SnsWrapper:
    """Encapsulates Amazon SNS topic and subscription functions."""

    def __init__(self, sns_resource):
        """
        :param sns_resource: A Boto3 Amazon SNS resource.
        """
        self.sns_resource = sns_resource


    @staticmethod
    def delete_topic(topic):
        """
        Deletes a topic. All subscriptions to the topic are also deleted.
        """
        try:
            topic.delete()
            logger.info("Deleted topic %s.", topic.arn)
        except ClientError:
            logger.exception("Couldn't delete topic %s.", topic.arn)
            raise
```

```
class SnsWrapper:
    """Wrapper class for managing Amazon SNS operations."""

    def __init__(self, sns_client: Any) -> None:
        """
        Initialize the SnsWrapper.

        :param sns_client: A Boto3 Amazon SNS client.
        """
        self.sns_client = sns_client

    @classmethod
    def from_client(cls) -> 'SnsWrapper':
        """
        Create an SnsWrapper instance using a default boto3 client.

        :return: An instance of this class.
        """
        sns_client = boto3.client('sns')
        return cls(sns_client)


    def delete_topic(self, topic_arn: str) -> bool:
        """
        Delete an SNS topic.

        :param topic_arn: The ARN of the topic to delete.
        :return: True if successful.
        :raises ClientError: If the topic deletion fails.
        """
        try:
            self.sns_client.delete_topic(TopicArn=topic_arn)
            
            logger.info(f"Deleted topic: {topic_arn}")
            return True

        except ClientError as e:
            error_code = e.response.get('Error', {}).get('Code', 'Unknown')
            
            if error_code == 'NotFound':
                logger.warning(f"Topic not found: {topic_arn}")
                return True  # Already deleted
            else:
                logger.error(f"Error deleting topic: {error_code} - {e}")
                raise
```
+  有关 API 的详细信息，请参阅适用[DeleteTopic](https://docs.amazonaws.cn/goto/boto3/sns-2010-03-31/DeleteTopic)于 *Python 的Amazon SDK (Boto3) API 参考*。

------
#### [ SAP ABAP ]

**适用于 SAP ABAP 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    TRY.
        lo_sns->deletetopic( iv_topicarn = iv_topic_arn ).
        MESSAGE 'SNS topic deleted.' TYPE 'I'.
      CATCH /aws1/cx_snsnotfoundexception.
        MESSAGE 'Topic does not exist.' TYPE 'E'.
    ENDTRY.
```
+  有关 API 的详细信息，请参阅适用[DeleteTopic](https://docs.amazonaws.cn/sdk-for-sap-abap/v1/api/latest/index.html)于 S *AP 的Amazon SDK ABAP API 参考*。

------
#### [ Swift ]

**适用于 Swift 的 SDK**  
 还有更多相关信息 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/sns#code-examples)中查找完整示例，了解如何进行设置和运行。

```
import AWSSNS

        let config = try await SNSClient.SNSClientConfiguration(region: region)
        let snsClient = SNSClient(config: config)

        _ = try await snsClient.deleteTopic(
            input: DeleteTopicInput(topicArn: arn)
        )
```
+  有关 API 的详细信息，请参阅适用于 S *wift 的Amazon SDK API 参考[DeleteTopic](https://sdk.amazonaws.com/swift/api/awssns/latest/documentation/awssns/snsclient/deletetopic(input:))*中。

------

# 后续步骤


现在，您已经使用订阅创建了一个主题，并向该主题发送了消息，您可能希望尝试以下操作：
+ 浏览 [Amazon 开发人员中心](https://www.amazonaws.cn/developer/)。
+ 在 [Security](security.md)（安全性）部分中了解如何保护您的数据。
+ 为主题启用[服务器端加密](sns-enable-encryption-for-topic.md)。
+ 使用已订阅的[加密 Amazon Simple Queue Service (Amazon SQS) 队列](sns-enable-encryption-for-topic-sqs-queue-subscriptions.md)为主题启用服务器端加密。
+ 将 [Amazon Event Fork Pipelines](sns-subscribe-event-fork-pipelines.md) 订阅到主题。