首页 > 解决方案 > NoCredentialsError botocore.exceptions.NoCredentialsError:即使在传递凭据后也无法找到凭据

问题描述

我正在向 Flask 应用程序内的 SQS 队列发送消息。我正在使用https://github.com/vsouza/docker-SQS-local在本地模仿这个 SQS 功能。

我正在为boto3.client()内部功能设置凭据。当我在 Pycharm 中直接运行发送消息的函数时,它正在向 SQS 队列发送消息。

但是当我 dockerize 这个 Flask 应用程序并调用触发这个函数的端点时,它会抛出错误。

引发 NoCredentialsError botocore.exceptions.NoCredentialsError:无法找到凭据

这是发送消息的代码。

def send_mes():
    config = Config()
    sqs = boto3.client('sqs', aws_access_key_id=None, aws_secret_access_key=None,
                       endpoint_url=config.QUEUE_ENDPOINT_URL, region_name='default')
    feeder_queue = config.FEEDER_QUEUE

    def inside_fun():
        while True:
            resp = sqs.send_message(
                QueueUrl=feeder_queue,
                MessageBody=(
                    f'Sample message for Queue at {datetime.now()}.'
                )
            )
            print(resp)
            time.sleep(3)

    t1 = threading.Thread(target=inside_fun)
    t2 = threading.Thread(target=inside_fun)

    t1.start()
    t2.start()

    t1.join()
    t2.join()


if __name__ == '__main__':
    send_mes()

请指出我在哪里犯错?

标签: python-3.xamazon-web-servicesdockerboto3amazon-sqs

解决方案


我没有在代码中直接将 AWS 凭证传递给客户端,而是设置了环境变量并且它起作用了。


推荐阅读