aws boto sns - get endpoint_arn by device token

早过忘川 提交于 2019-12-04 08:05:44
fino

Amazon gives you the arn on the Error Message. You can parse it from there. Not optimal, but saves some db lookup.

error: SNS ERROR - Could not subcribe user to SNSInvalidParameter: Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-east-[rest of the ARN Key] already exists with the same Token, but different attributes. Here is some coffe with regex ready to go

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} "
John Pang

Same answer as @fino but code for Django. Hope this help.

import re

try:
    sns_connection = sns.connect_to_region(...)
    response = sns_connection.create_platform_endpoint(...)
    arn = response['CreatePlatformEndpointResponse']['CreatePlatformEndpointResult']['EndpointArn']
    return arn

except Exception, err:
    print err
    #try to get arn from error
    result_re = re.compile(r'Endpoint(.*)already', re.IGNORECASE)
    result = result_re.search(err.message)
    if result:
        arn = result.group(0).replace('Endpoint ','').replace(' already','')
        print arn
        return arn
    return ''
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!