简体   繁体   中英

NoCredentialsError botocore.exceptions.NoCredentialsError: Unable to locate credentials even after passing credentials

I'm working on sending messages to a SQS queue inside a Flask application. I'm mimicing this SQS functionality in local using https://github.com/vsouza/docker-SQS-local .

I'm setting credentials for boto3.client() inside function. When I run the function for sending messages directly in Pycharm, it is sending messages to SQS queue.

But when I dockerize this Flask application and call the endpoint which triggers this function, it is throwing error.

raise NoCredentialsError botocore.exceptions.NoCredentialsError: Unable to locate credentials

Here's the code for sending messages.

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()

Please point out where am I making mistake?

Rather than passing the AWS credentials to the client directly in the code, I set the environment variables and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM