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

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

    try specifying keys manually

        s3 = boto3.resource('s3',
             aws_access_key_id=ACCESS_ID,
             aws_secret_access_key= ACCESS_KEY)
    

    Make sure you don't include your ACCESS_ID and ACCESS_KEY in the code directly for security concerns. Consider using environment configs and injecting them in the code as suggested by @Tiger_Mike.

    For Prod environments consider using rotating access keys: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_RotateAccessKey

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

    from the terminal type:-

    aws configure
    

    then fill in your keys and region.

    after this do next step use any environment. You can have multiple keys depending your account. Can manage multiple enviroment or keys

    import boto3
    aws_session = boto3.Session(profile_name="prod")
    # Create an S3 client
    s3 = aws_session.client('s3')
    
    0 讨论(0)
  • 2020-11-30 23:48

    These instructions are for windows machine with a single user profile for AWS. Make sure your ~/.aws/credentials file looks like this

    [profile_name]
    aws_access_key_id = yourAccessId
    aws_secret_access_key = yourSecretKey
    

    I had to set the AWS_DEFAULT_PROFILEenvironment variable to profile_name found in your credentials.
    Then my python was able to connect. eg from here

    import boto3
    
    # Let's use Amazon S3
    s3 = boto3.resource('s3')
    
    # Print out bucket names
    for bucket in s3.buckets.all():
        print(bucket.name)
    
    0 讨论(0)
  • 2020-11-30 23:50

    Create an S3 client object with your credentials

    AWS_S3_CREDS = {
        "aws_access_key_id":"your access key", # os.getenv("AWS_ACCESS_KEY")
        "aws_secret_access_key":"your aws secret key" # os.getenv("AWS_SECRET_KEY")
    }
    s3_client = boto3.client('s3',**AWS_S3_CREDS)
    

    It is always good to get credentials from os environment

    To set Environment variables run the following commands in terminal

    if linux or mac

    $ export AWS_ACCESS_KEY="aws_access_key"
    $ export AWS_SECRET_KEY="aws_secret_key"
    

    if windows

    c:System\> set AWS_ACCESS_KEY="aws_access_key"
    c:System\> set AWS_SECRET_KEY="aws_secret_key"
    
    0 讨论(0)
  • 2020-11-30 23:51

    I work for a large corporation and encountered this same error, but needed a different work around. My issue was related to proxy settings. I had my proxy set up so I needed to set my no_proxy to whitelist AWS before I was able to get everything to work. You can set it in your bash script as well if you don't want to muddy up your Python code with os settings.

    Python:

    import os
    os.environ["NO_PROXY"] = "s3.amazonaws.com"
    

    Bash:

    export no_proxy = "s3.amazonaws.com"
    

    Edit: The above assume a US East S3 region. For other regions: use s3.[region].amazonaws.com where region is something like us-east-1 or us-west-2

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

    The boto3 is looking for the credentials in the folder like

    C:\ProgramData\Anaconda3\envs\tensorflow\Lib\site-packages\botocore\.aws
    

    You should save two files in this folder credentials and config.

    You may want to check out the general order in which boto3 searches for credentials in this link. Look under the Configuring Credentials sub heading.

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