使用 Amazon 软件开发工具包删除 Amazon SNS 订阅 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 软件开发工具包删除 Amazon SNS 订阅

以下代码示例显示如何删除 Amazon SNS 订阅。

.NET
Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅 GitHub

/// <summary> /// Given the ARN for an Amazon SNS subscription, this method deletes /// the subscription. /// </summary> /// <param name="client">The initialized Amazon SNS client object, used /// to delete an Amazon SNS subscription.</param> /// <param name="subscriptionArn">The ARN of the subscription to delete.</param> public static async Task TopicUnsubscribeAsync( IAmazonSimpleNotificationService client, string subscriptionArn) { var response = await client.UnsubscribeAsync(subscriptionArn); } }
  • 有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考 中的 Unsubscribe

C++
适用于 C++ 的 SDK
提示

要了解如何设置和运行此示例,请参阅 GitHub

Aws::SDKOptions options; Aws::InitAPI(options); { Aws::SNS::SNSClient sns; Aws::String subscription_arn = argv[1]; Aws::SNS::Model::UnsubscribeRequest s_req; s_req.SetSubscriptionArn(subscription_arn); auto s_out = sns.Unsubscribe(s_req); if (s_out.IsSuccess()) { std::cout << "Unsubscribed successfully " << std::endl; } else { std::cout << "Error while unsubscribing " << s_out.GetError().GetMessage() << std::endl; } } Aws::ShutdownAPI(options);
  • 有关 API 详细信息,请参阅 Amazon SDK for C++ API 参考 中的 Unsubscribe

Java
SDK for Java 2.x
提示

要了解如何设置和运行此示例,请参阅 GitHub

public static void unSub(SnsClient snsClient, String subscriptionArn) { try { UnsubscribeRequest request = UnsubscribeRequest.builder() .subscriptionArn(subscriptionArn) .build(); UnsubscribeResponse result = snsClient.unsubscribe(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription was removed for " + request.subscriptionArn()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考 中的 Unsubscribe

JavaScript
SDK for JavaScript V3
提示

要了解如何设置和运行此示例,请参阅 GitHub

在单独的模块中创建客户端并将其导出。

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import {UnsubscribeCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { SubscriptionArn: "TOPIC_SUBSCRIPTION_ARN" }; //TOPIC_SUBSCRIPTION_ARN const run = async () => { try { const data = await snsClient.send(new UnsubscribeCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

提示

要了解如何设置和运行此示例,请参阅 GitHub

suspend fun unSub(subscriptionArnVal: String) { val request = UnsubscribeRequest { subscriptionArn = subscriptionArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.unsubscribe(request) println("Subscription was removed for ${request.subscriptionArn}") } }
  • 有关 API 详细信息,请参阅 Amazon SDK for Kotlin API 参考中的 Unsubscribe

PHP
SDK for PHP
提示

要了解如何设置和运行此示例,请参阅 GitHub

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException; /** * Deletes a subscription 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' ]); $subscription = 'arn:aws:sns:us-east-1:111122223333:MySubscription'; try { $result = $SnSclient->unsubscribe([ 'SubscriptionArn' => $subscription, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
Python
适用于 Python (Boto3) 的 SDK
提示

要了解如何设置和运行此示例,请参阅 GitHub

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 delete_subscription(subscription): """ Unsubscribes and deletes a subscription. """ try: subscription.delete() logger.info("Deleted subscription %s.", subscription.arn) except ClientError: logger.exception("Couldn't delete subscription %s.", subscription.arn) raise
  • 有关 API 详细信息,请参阅《适用于 Python (Boto3) 的 Amazon SDK API 参考》中的 Unsubscribe

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon SNS 与 Amazon 开发工具包结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。