如何通过 python 脚本使用 SNS API 设置 SetPlatformApplicationAttributes

How to set SetPlatformApplicationAttributes using SNS API via python script

我正在尝试使用 boto3 库通过 python 为 SNS 主题设置 Platfrom 应用程序属性。

下面是脚本片段

client1 = boto3.client('sns',region_name="us-east-1",aws_access_key_id=access_key,aws_secret_access_key=secret_access_key)

def getSNSattr():
    response = client1.set_platform_application_attributes(
    PlatformApplicationArn='arn:aws:sns:us-east-1:63124104179:testTop1',
    Attributes={
        'key':'SuccessFeedbackSampleRate',
        'value':'100'
    }
    )

以下是我遇到的错误。 aws 文档没有 python 的示例,也没有太大帮助。我不确定我在这里错过了什么。请帮助

*Traceback (most recent call last):
  File "sns2.py", line 20, in <module>
    getSNSattr()
  File "sns2.py", line 15, in getSNSattr
    'key': 'SuccessFeedbackSampleRate'
  File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 317, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 615, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidParameterException: An error occurred (InvalidParameter) when calling the SetPlatformApplicationAttributes operation: Invalid parameter: PlatformApplicationArn Reason: Wrong number of slashes in relative portion of the ARN.*

AWS 文档供参考: https://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html

https://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.set_platform_application_attributes

您放入 PlatformApplicationArn 的 ARN 不是平台 ARN,而是主题 ARN。如果要为主题设置属性,请考虑使用 SetTopicAttributes API。我也会使用双引号,因为属性是 JSON 格式的。

所有这些都可以使用 AWS CLI 轻松测试:

$ aws sns get-platform-application-attributes --platform-application-arn arn:aws:sns:us-east-1:123456789012:app/GCM/My_App

{
    "Attributes": {
        "FailureFeedbackRoleArn": "arn:aws:iam::123456789012:role/SNSFailureFeedback",
        "EventDeliveryFailure": "arn:aws:sns:us-east-1:123456789012:Test",
        "EventEndpointDeleted": "arn:aws:sns:us-east-1:123456789012:Test",
        "EventEndpointUpdated": "arn:aws:sns:us-east-1:123456789012:Test",
        "Enabled": "true",
        "EventEndpointCreated": "arn:aws:sns:us-east-1:123456789012:Test",
        "SuccessFeedbackRoleArn": "arn:aws:iam::123456789012:role/SNSSuccessFeedback"
    }
}

$ aws sns set-platform-application-attributes --platform-application-arn arn:aws:sns:us-east-1:123456789012:app/GCM/My_App --attributes '{"SuccessFeedbackSampleRate": "100"}'

$ aws sns get-platform-application-attributes --platform-application-arn arn:aws:sns:us-east-1:123456789012:app/GCM/My_App

{
    "Attributes": {
        "FailureFeedbackRoleArn": "arn:aws:iam::123456789012:role/SNSFailureFeedback",
        "EventDeliveryFailure": "arn:aws:sns:us-east-1:123456789012:Test",
        "EventEndpointDeleted": "arn:aws:sns:us-east-1:123456789012:Test",
        "SuccessFeedbackSampleRate": "100",
        "EventEndpointUpdated": "arn:aws:sns:us-east-1:123456789012:Test",
        "Enabled": "true",
        "EventEndpointCreated": "arn:aws:sns:us-east-1:123456789012:Test",
        "SuccessFeedbackRoleArn": "arn:aws:iam::123456789012:role/SNSSuccessFeedback"
    }
}

如果要为应用程序设置属性,请将 SetPlatformApplicationAttributes API 与 Application ARN 结合使用,或者将 SetTopicAttributes API 与 Topic ARN 如果您想为主题设置属性。还使用双引号作为属性 JSON 格式。

代码应如下所示:

def getSNSattr():
    response = client1.set_platform_application_attributes(
    PlatformApplicationArn="arn:aws:sns:us-east-1:63124104179:app/GCM/testApp1",
    Attributes={
        "SuccessFeedbackSampleRate":"100"
    }
    )