amazon-web-services - aws boto sns-通过设备 token 获取endpoint_arn

标签 amazon-web-services boto amazon-sns

当前,如果我们要使用以下方法将设备添加到SNS应用程序中:

ep = SNSConnection.create_platform_endpoint(app_arn,device_token,user_data)

有一个选项是该设备已在过去添加。
为了验证设备是否已经添加,我们正在使用:

def is_device_registered(device_token):
        list_of_endpoints = SNSConnection.list_endpoints_by_platform_application(AC.INPLAY_CHAT_APPLICATION_SNS_ARN)
        all_app_endpoints = list_of_endpoints['ListEndpointsByPlatformApplicationResponse']['ListEndpointsByPlatformApplicationResult']['Endpoints']
        for ep in all_app_endpoints:
            ep_device_token = ep['Attributes']['Token']
            if device_token == ep_device_token:
                endpoint_arn =  ep['EndpointArn']
                print 'Found an endpoint for device_token: %s, entry:%s' % (device_token,endpoint_arn)
                return endpoint_arn
        return None

这是非常低效的,无法扩展。

是否有一个boto sns函数来获取device_token并返回endpoint_arn(如果存在)?
(如果没有,则没有)。

最佳答案

Amazon为您提供有关错误消息的信息。您可以从那里解析它。不是最佳选择,但是可以节省一些数据库查找。

错误:SNS错误-无法将用户预订为SNSInvalidParameter:无效参数: token 原因:端点arn:aws:sns:us-east- [ARN key 的其余部分]已经存在,具有相同的 token ,但属性不同。这是准备使用正则表达式的一些咖啡

if err?
  log.error "SNS ERROR - Could not subcribe user to SNS" + err
  #Try to get arn from error

  result = err.message.match(/Endpoint(.*)already/)
  if result?.length
    #Assign and remove leading and trailing white spaces.
    result = result[1].replace /^\s+|\s+$/g, ""
    log.debug "found existing arn-> #{result} "

关于amazon-web-services - aws boto sns-通过设备 token 获取endpoint_arn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227262/

相关文章:

android - 使用 aws-ruby-sdk 通过 AWS SNS 向多个平台发送消息

amazon-web-services - 未收到 Amazon SES 退回通知

amazon-web-services - 未处理的异常 : SocketException: Failed host lookup: (OS Error: No address associated with hostname, errno = 7)

python - 克隆后增加 EC2 EBS 卷 - resize2fs 不起作用

amazon-s3 - 如何使用 boto 或其他工具为 S3 存储桶中的文件设置 Http Header

python - 无法使用python将项目添加到dynamodb表

ios - 为 Amazon SNS 服务创建终端节点的最有效方法是什么?

node.js - 我们可以直接从 url 上传图像到亚马逊 S3

amazon-web-services - AWS Data Pipeline 与 Step Functions

amazon-web-services - Amazon S3 Glacier 与 Glacier 存储类