Downloading the file from S3 through AWS cli works, but through python SDk I receive 403 forbidden error! I am using same credentials

Viewed 61

Downloading the file from S3 through AWS cli works, but through python SDk I receive 403 forbidden error! I am using the same credentials.

from boto3.session import Session

ACCESS_KEY = '******'
SECRET_KEY = '******'

session = Session(aws_access_key_id=ACCESS_KEY,
                  aws_secret_access_key=SECRET_KEY,
                  region_name='eu-west-1'
                  )
s3 = session.resource('s3')
bucket_name = "****"
s3.meta.client.download_file(bucket_name, 's3://folder/xyz.tar.xz', 'abc.tar.xz')


  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/s3transfer/futures.py", line 106, in result
    return self._coordinator.result()
  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/s3transfer/futures.py", line 265, in result
    raise self._exception
  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/s3transfer/tasks.py", line 255, in _main
    self._submit(transfer_future=transfer_future, **kwargs)
  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/s3transfer/download.py", line 340, in _submit
    response = client.head_object(
  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/abh16/controldbagent/lib/python3.8/site-packages/botocore/client.py", line 705, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

Using same credentials downloading through AWS CLI works fine.

1 Answers

I'm going to assume 2 things here.

  1. You don't have a S3 policy blocking non aws-cli connections.
  2. Your bucket and your objects(files) are completely private.

Then, according to boto3 docs the function works like download_file(Bucket, Key, Filename)

what is wrong is your Key parameter; drop the s3://

s3.meta.client.download_file(bucket_name, 'folder/xyz.tar.xz', 'abc.tar.xz')

Related