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

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

提交回复
热议问题