I am currently trying to get access to Amazon S3 inside a virtual machine and download files like so:
s3 = boto3.resource('s3',
aws_access_key_id="xxxxxxxxxxx",
aws_secret_access_key="xxxxxxxxxxxxxxxxx")
s3client = boto3.client('s3')
bucket = s3.Bucket('bucketone')
for obj in bucket.objects.all():
s3client.download_file(bucket_name, obj.key, filename)
But I’m getting the error:
botocore.exceptions.ClientError: An error occurred (InvalidAccessKeyId) when calling the ListObjects operation: The AWS Access Key Id you provided does not exist in our records.
What could I be doing wrong? I checked my aws_access_key_id
and aws_secret_access_key
multiple times, but still getting the same error. The same code locally, but not on a virtual machine, actually works on a different computer as well. There is a reason why I’m hardcoding in the keys, as I have to.
You need to set the access for the boto3 session. You don't really want to put your keys in your code. What I would recommend doing first is running 'aws configure' and setting your aws_access_key_id and aws_secret_access_key in your .credentials file. Then in your code do the following:
session = boto3.Session(profile_name='name_of_your_profile')
If you have just the default profile, you might not need to do that or for good measure, just put:
session = boto3.Session(profile_name='default')
Once you have that in your code you can establish a connection to s3 with:
s3 = session.resource('s3')
bucket = s3.Bucket('bucketone')
for obj in bucket.objects.all():
print(obj.key)
There is some problem with your code as well. You are creating an s3 client. S3 client does not have a Bucket method or property. To do the same thing with the s3 client you would do:
s3client = session.client('s3')
response = s3client.get_object(Bucket='bucketone', key='your key')
You can then iterate through the response that is returned to see the list of objects in the bucket.
That should take care of your error.