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

后端 未结 12 2186
逝去的感伤
逝去的感伤 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:38

    If you are looking for an alternative way, try adding your credentials using AmazonCLI

    from the terminal type:-

    aws configure
    

    then fill in your keys and region.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 23:39

    If you have multiple aws profiles in ~/.aws/credentials like...

    [Profile 1]
    aws_access_key_id = *******************
    aws_secret_access_key = ******************************************
    [Profile 2]
    aws_access_key_id = *******************
    aws_secret_access_key = ******************************************
    

    Follow two steps:

    1. Make one you want to use as a default using export AWS_DEFAULT_PROFILE=Profile 1 command in terminal.

    2. Make sure to run above command in the same terminal from where you use boto3 or you open an editor.[Understand the following scenario]

    Scenario:

    • If you have two terminal open called t1 and t2.
    • And you run the export command in t1 and you open JupyterLab or any other from t2, you will get NoCredentialsError: Unable to locate credentials error.

    Solution:

    • Run the export command in t1 and then open JupyterLab or any other from the same terminal t1.
    0 讨论(0)
  • 2020-11-30 23:41

    I had the same issue and found out that the format of my ~/.aws/credentials file was wrong.

    It worked with a file containing:

    [default]
    aws_access_key_id=XXXXXXXXXXXXXX
    aws_secret_access_key=YYYYYYYYYYYYYYYYYYYYYYYYYYY
    

    Note that the profile name must be "[default]". Some official documentation make reference to a profile named "[credentials]", which did not work for me.

    0 讨论(0)
  • 2020-11-30 23:44

    I also had the same issue,it can be solved by creating a config and credential file in the home directory. Below show the steps I did to solve this issue.

    Create a config file :

    touch ~/.aws/config
    

    And in that file I entered the region

    [default]
    region = us-west-2
    

    Then create the credential file:

    touch ~/.aws/credentials
    

    Then enter your credentials

    [Profile1]
    aws_access_key_id = XXXXXXXXXXXXXXXXXXXX 
    aws_secret_access_key = YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
    

    After set all these, then my python file to connect bucket. Run this file will list all the contents.

    import boto3
    import os
    
    os.environ['AWS_PROFILE'] = "Profile1"
    os.environ['AWS_DEFAULT_REGION'] = "us-west-2"
    
    s3 = boto3.client('s3', region_name='us-west-2')
    print("[INFO:] Connecting to cloud")
    
    # Retrieves all regions/endpoints that work with S3
    
    response = s3.list_buckets()
    print('Regions:', response)
    

    You can also refer below links:

    • Amazon S3 with Python Boto3 Library
    • Boto 3 documentation
    • Boto3: Amazon S3 as Python Object Store
    0 讨论(0)
  • 2020-11-30 23:44

    If you're sure you configure your aws correctly, just make sure the user of the project can read from ./aws or just run your project as a root

    0 讨论(0)
提交回复
热议问题