AWS SNS HTTP/S订阅、确认和发布示例

639 阅读4分钟

当使用亚马逊简单通知服务(SNS)的 "订阅 "和 "发布 "功能时,中间有订阅确认步骤。在有人订阅了一个 "端点 "之后,亚马逊SNS会发送一个订阅确认信息。如果email "协议 "中使用了电子邮件地址作为 "端点 "属性,就会发送一封电子邮件。如果http/https "协议 "中使用了一个URL作为 "端点 "属性,则会发送一个POST请求。在这两种情况下,消息内容都会有SubscribeURL 数据。这个URL必须被访问以确认订阅,否则当有东西被发布时,原始订阅将被忽略。一个被访问的URL的成功响应(200 OK )将是XML,包含SubscriptionArnRequestId 属性。

例子

我在测试中使用了https 协议和https://asd34hjfd.m.pipedream.net/ 端点。你可以分别使用电子邮件和电子邮件地址作为协议和端点。

订阅确认信息内容

这是你将从亚马逊收到的内容:

{
  "headers": {
    "host": "asd34hjfd.m.pipedream.net",
    "x-amzn-trace-id": "Root=1-60146237-1bb8db87053a3fa237869185",
    "content-length": "697",
    "user-agent": "Amazon Simple Notification Service Agent",
    "accept-encoding": "gzip, deflate",
    "accept": "*/*",
    "content-type": "text/plain",
    "x-amz-sns-message-type": "SubscriptionConfirmation",
    "x-amz-sns-topic-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "x-amz-sns-subscription-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter:44980782-18b0-4596-aee3-144f3f47dedf"
  },
  "body": {
    "Type": "SubscriptionConfirmation",
    "MessageId": "718e57b1-076f-4d95-929e-99996c34209d",
    "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "Message": "You have chosen to subscribe to the topic arn:aws:sns:eu-west-1:000000000000:newsletter.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
    "Timestamp": "2021-01-29T19:29:57.582Z",
    "SignatureVersion": "1",
    "Signature": "EXAMPLEpH+..",
    "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem",
    "SubscribeURL": "http://localhost:4566/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:eu-west-1:000000000000:newsletter&Token=90a9c15d",
    "Token": "90a9c15d"
  },
  "inferred_body_type": "JSON",
  "method": "POST",
  "url": "https://asd34hjfd.m.pipedream.net/",
  "client_ip": "1.10.40.10",
  "query": {
    
  }
}

发布消息内容

一旦你确认了上面的订阅,这就是你的https://asd34hjfd.m.pipedream.net/ 端点将收到的POST请求的内容。我已经发布了hello 消息:

{
  "headers": {
    "host": "asd34hjfd.m.pipedream.net",
    "x-amzn-trace-id": "Root=1-6014626e-0703d14767d4d65d2a957a97",
    "content-length": "363",
    "user-agent": "Amazon Simple Notification Service Agent",
    "accept-encoding": "gzip, deflate",
    "accept": "*/*",
    "content-type": "text/plain",
    "x-amz-sns-message-type": "Notification",
    "x-amz-sns-topic-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "x-amz-sns-subscription-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter:44980782-18b0-4596-aee3-144f3f47dedf"
  },
  "body": {
    "Type": "Notification",
    "MessageId": "7983a6d0-d1af-4846-977f-b14aa5e040a5",
    "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "Message": "hello",
    "Timestamp": "2021-01-29T19:30:53.973Z",
    "SignatureVersion": "1",
    "Signature": "EXAMPLEpH+..",
    "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem"
  },
  "inferred_body_type": "JSON",
  "method": "POST",
  "url": "https://asd34hjfd.m.pipedream.net/",
  "client_ip": "1.10.40.10",
  "query": {
    
  }
}

测试

创建一个主题(你)

$ aws --profile localstack --endpoint-url http://localhost:4566 sns create-topic --name newsletter
{
    "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter"
}

订阅一个主题(客户):

$ aws --profile localstack --endpoint-url http://localhost:4566 sns subscribe --topic-arn arn:aws:sns:eu-west-1:000000000000:newsletter --protocol https --notification-endpoint https://efs324vfd43.m.pipedream.net
{
    "SubscriptionArn": "arn:aws:sns:eu-west-1:000000000000:newsletter:43014665-f108-4176-96ef-121382da3776"
}

这是订阅的细节:

$ aws --profile localstack --endpoint-url http://localhost:4566 sns list-subscriptions
{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:eu-west-1:000000000000:newsletter:43014665-f108-4176-96ef-121382da3776",
            "Owner": "",
            "Protocol": "https",
            "Endpoint": "https://efs324vfd43.m.pipedream.net",
            "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter"
        }
    ]
}

确认订阅(客户端)

这是你的端点将收到的内容,你必须访问SubscribeURL 。一旦访问,将返回一个200 OK XML响应,如上所述:

{
  "headers": {
    "host": "efs324vfd43.m.pipedream.net",
    "x-amzn-trace-id": "Root=1-60147dc0-45ac810e098a1f4a57c5a652",
    "content-length": "697",
    "user-agent": "Amazon Simple Notification Service Agent",
    "accept-encoding": "gzip, deflate",
    "accept": "*/*",
    "content-type": "text/plain",
    "x-amz-sns-message-type": "SubscriptionConfirmation",
    "x-amz-sns-topic-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "x-amz-sns-subscription-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter:43014665-f108-4176-96ef-121382da3776"
  },
  "body": {
    "Type": "SubscriptionConfirmation",
    "MessageId": "97c3152b-7a51-4cc1-85fb-a05c5e00bac6",
    "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "Message": "You have chosen to subscribe to the topic arn:aws:sns:eu-west-1:000000000000:newsletter.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
    "Timestamp": "2021-01-29T21:27:27.739Z",
    "SignatureVersion": "1",
    "Signature": "EXAMPLEpH+..",
    "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem",
    "SubscribeURL": "http://localhost:4566/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:eu-west-1:000000000000:newsletter&Token=555f40e4",
    "Token": "555f40e4"
  },
  "inferred_body_type": "JSON",
  "method": "POST",
  "url": "https://efs324vfd43.m.pipedream.net/",
  "client_ip": "1.15.10.10",
  "query": {
    
  }
}

发布消息到一个主题(你)

$ aws --profile localstack --endpoint-url http://localhost:4566 sns publish --topic-arn arn:aws:sns:eu-west-1:000000000000:newsletter --message "hello"
{
    "MessageId": "b774ac11-acc9-4c28-b0dd-a4aeeaceb58d"
}

这是你的端点将收到的请求:

{
  "headers": {
    "host": "efs324vfd43.m.pipedream.net",
    "x-amzn-trace-id": "Root=1-60147ee8-4d11aef729aa6bb6192fbcfc",
    "content-length": "363",
    "user-agent": "Amazon Simple Notification Service Agent",
    "accept-encoding": "gzip, deflate",
    "accept": "*/*",
    "content-type": "text/plain",
    "x-amz-sns-message-type": "Notification",
    "x-amz-sns-topic-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "x-amz-sns-subscription-arn": "arn:aws:sns:eu-west-1:000000000000:newsletter:43014665-f108-4176-96ef-121382da3776"
  },
  "body": {
    "Type": "Notification",
    "MessageId": "b774ac11-acc9-4c28-b0dd-a4aeeaceb58d",
    "TopicArn": "arn:aws:sns:eu-west-1:000000000000:newsletter",
    "Message": "hello",
    "Timestamp": "2021-01-29T21:32:23.540Z",
    "SignatureVersion": "1",
    "Signature": "EXAMPLEpH+..",
    "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem"
  },
  "inferred_body_type": "JSON",
  "method": "POST",
  "url": "https://efs324vfd43.m.pipedream.net/",
  "client_ip": "1.15.10.10",
  "query": {
    
  }
}