【问题标题】:Moto SNS client can't get publishing working AttributeError: 'sns.ServiceResource' object has no attribute 'publish'Moto SNS 客户端无法发布工作 AttributeError:“sns.ServiceResource”对象没有属性“发布”
【发布时间】:2020-03-08 22:20:14
【问题描述】:

我正在尝试使用 Moto 模拟 SNS,使用 pytest 文档中的示例。

sns.create_topic() 有效,但 sns.publish() 无效。从 boto 文档中,我应该能够像这样调用 publish():

@pytest.fixture()
def aws_credentials():
    """Mocked AWS Credentials for moto."""
    os.environ["AWS_ACCESS_KEY_ID"] = "testing"
    os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
    os.environ["AWS_SECURITY_TOKEN"] = "testing"
    os.environ["AWS_SESSION_TOKEN"] = "testing"


@pytest.fixture()
def sts(aws_credentials):
    with mock_sts():
        yield boto3.client("sts", region_name="us-east-1")


@pytest.fixture
def sns(aws_credentials):
    with mock_sns():
        yield boto3.resource("sns", region_name="us-east-1")

@mock_sts
def test_publish(sns):
    resp = sns.create_topic(Name="sdfsdfsdfsd")
    mesg = {"TopicArn": "arnsdfsdf", "Message": "sdfsdfsdfsd"}
    response = sns.publish(mesg)

我收到以下错误:

AttributeError: 'sns.ServiceResource' 对象没有属性 'publish'

Moto 不支持发布吗?我希望它验证对 publish() 的调用对我有效 - 我不想发布猴子补丁。

【问题讨论】:

    标签: python amazon-web-services moto


    【解决方案1】:

    SNS 使用 boto3 客户端,而不是资源。 所以改变这个:

    with mock_sns():
        yield boto3.resource("sns", region_name="us-east-1")
    

    到这里:

    with mock_sns():
        yield boto3.client("sns", region_name="us-east-1")
    

    它应该可以工作。

    示例测试用例:https://github.com/spulec/moto/blob/master/tests/test_sns/test_publishing_boto3.py#L28

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 2023-03-26
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      相关资源
      最近更新 更多