Home > database >  How to download a file from aws bucket only by knowing bucket name and object name in python?
How to download a file from aws bucket only by knowing bucket name and object name in python?

Time:09-09

I need to download a file from aws bucket ( name known ) and object name know. But access key id and secret key are not known.

import boto3
import pandas as pd

s3 = boto3.client('s3')

s3 = boto3.resource(service_name='s3')
s3.Bucket('yz').download_file(Key='keh.docx', Filename='keh.docx')

Getting the Following error NoCredentialsError: Unable to locate credentials

Is there any way that we can download a file without secret key and access id?

CodePudding user response:

Is there any way that we can download a file without secret key and access id?

Yes, if the object is public. Then you don't need any credentials. Also you can download objects with credentials if you have S3 pre-signed url for the object.

In all other cases, credentials are required in one form or the other.

CodePudding user response:

An example from the boto3 documentation

Import boto3

s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME')

If the file is in a public bucket, you can only use the request package.

import queries
url = 'http://s3.amazonaws.com/[bucket_name]/[path_to_file]/file'
file = requests.get(url, allow_redirects=True)
open('./file', 'wb').write(file.content)
  • Related