Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

后端 未结 12 2251
逝去的感伤
逝去的感伤 2020-11-30 23:00

When I simply run the following code, I always gets this error.

s3 = boto3.resource(\'s3\')
    bucket_name = \"python-sdk-sample-%s\" % uuid.uuid4()
    pri         


        
12条回答
  •  天命终不由人
    2020-11-30 23:39

    Make sure your ~/.aws/credentials file in Unix looks like this:

    [MyProfile1]
    aws_access_key_id = yourAccessId
    aws_secret_access_key = yourSecretKey
    
    [MyProfile2]
    aws_access_key_id = yourAccessId
    aws_secret_access_key = yourSecretKey
    

    Your Python script should look like this, and it'll work:

    from __future__ import print_function
    import boto3
    import os
    
    os.environ['AWS_PROFILE'] = "MyProfile1"
    os.environ['AWS_DEFAULT_REGION'] = "us-east-1"
    
    ec2 = boto3.client('ec2')
    
    # Retrieves all regions/endpoints that work with EC2
    response = ec2.describe_regions()
    print('Regions:', response['Regions'])
    

    Source: https://boto3.readthedocs.io/en/latest/guide/configuration.html#interactive-configuration.

提交回复
热议问题