gpt4 book ai didi

c# - 亚马逊简单通知服务 AWSSDK C# - S.O.S

转载 作者:IT王子 更新时间:2023-10-29 04:27:26 27 4
gpt4 key购买 nike

我正在尝试使用 Amazon 的 AWSSDK for C# 和 Simple Notification Service 进行发布。

在谷歌搜索 2 小时后,SDK 没有附带任何示例,而且我在网上找不到任何示例。我想到了这个,但它抛出了一个异常,它产生的信息不超过单个字符串“TopicARN”——没有内部异常——nuffin!
如果有人使用 AWSSDK 通过 C# 使用 SNS 成功发送消息,我很乐意看到即使是最基本的工作示例。我正在使用最新的 SDK 1.5x

代码如下:

string resourceName = "arn:aws:sns:us-east-1:xxxxxxxxxxxx:StackOverFlowStub";
AmazonSimpleNotificationServiceClient snsclient = new AmazonSimpleNotificationServiceClient(accesskey,secretkey);
AddPermissionRequest permissionRequest = new AddPermissionRequest()
.WithActionNames("Publish")
.WithActionNames(accesskey)
.WithActionNames("PrincipleAllowControl")
.WithActionNames(resourceName);
snsclient.AddPermission(permissionRequest);

PublishRequest pr = new PublishRequest();
pr.WithMessage("Test Msg");
pr.WithTopicArn(resourceName);
pr.WithSubject("Test Subject");
snsclient.Publish(pr);

最佳答案

下面是创建主题、设置主题显示名称、订阅主题的电子邮件地址、发送消息和删除主题的示例。请注意,在继续之前,您应该在两个地方等待/检查您的电子邮件。 Client 是客户端实例,topicName 是任意主题名称。

// Create topic
string topicArn = client.CreateTopic(new CreateTopicRequest
{
Name = topicName
}).CreateTopicResult.TopicArn;

// Set display name to a friendly value
client.SetTopicAttributes(new SetTopicAttributesRequest
{
TopicArn = topicArn,
AttributeName = "DisplayName",
AttributeValue = "StackOverflow Sample Notifications"
});

// Subscribe an endpoint - in this case, an email address
client.Subscribe(new SubscribeRequest
{
TopicArn = topicArn,
Protocol = "email",
Endpoint = "sample@example.com"
});

// When using email, recipient must confirm subscription
Console.WriteLine("Please check your email and press enter when you are subscribed...");
Console.ReadLine();

// Publish message
client.Publish(new PublishRequest
{
Subject = "Test",
Message = "Testing testing 1 2 3",
TopicArn = topicArn
});

// Verify email receieved
Console.WriteLine("Please check your email and press enter when you receive the message...");
Console.ReadLine();

// Delete topic
client.DeleteTopic(new DeleteTopicRequest
{
TopicArn = topicArn
});

关于c# - 亚马逊简单通知服务 AWSSDK C# - S.O.S,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13006828/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com