0

I'm trying to view S3 bucket list through a python scripts using boto3. Credential file and config file is available in the C:\Users\user1.aws location. Secret access and access key available there for user "vscode". But unable to run the script which return exception message as

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

Code sample follows,

import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():     
    print(bucket.name)

Do I need to specify user mentioned above ("vscode") ?

Copied the credential and config file to folder of python script is running. But same exception occurs.

ewokx
  • 2,204
  • 3
  • 14
  • 27
DasGuin
  • 11
  • 1
  • 1
  • 8
  • You should checkout the python-dotenv package. You can store the secrets in a `.env` file at your root and retrieve these using the package. I think that should solve the problem. You can also check this [What is the use of python-dotenv?](https://stackoverflow.com/questions/41546883/what-is-the-use-of-python-dotenv) for more information. – anosha_rehan Apr 04 '22 at 05:16
  • You should use the [AWS Command-Line Interface (CLI)](http://aws.amazon.com/cli/) `aws configure` command to store the credentials in the standard credentials location. Then, boto3 will automatically retrieve those credentials. – John Rotenstein Apr 04 '22 at 12:52
  • Actually I change the [vscode] to [default] in the credentials file of C:\Users\user1\.aws location. Now no exceptions. I don't clear what's happening there @anosha_rehan – DasGuin Apr 04 '22 at 16:53

2 Answers2

2

When I got this error, I replaced resource with client and also added the secrets during initialization:

client = boto3.client('s3', region_name=settings.AWS_REGION, aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                          aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
anosha_rehan
  • 1,522
  • 10
  • 17
-3

You can try with boto3.client('s3') instead of boto3.resource('s3')

peter21
  • 27
  • 2