I'm trying to use the download_obj feature but it seems that is not injected correctly into the client. I tried with the example from the documentation and from tests but I had no luck. download_file works as expected
That is a currently unreleased feature. It will be available in the next minor version of boto3. Please check out the stable dos to only see features which have been pushed out in a release.
def download(self, filename, fileobj=None, bucket=settings.AWS_BUCKET):
'''
If fileobj is passed, use it to store data coming from AWS.
Do not return (mutable object was already passed).
- Exempli gratia,
with BytesIO() as fileobj:
<S3CONNECTION>.download(filename, fileobj)
Otherwise, return new, in-memory filelike object and rely on gc
- Exempli gratia,
fileobj = <S3CONNECTION>.download(filename)
from io import BytesIO
f = fileobj or BytesIO()
'''
try:
try:
# inject the data into exiting or new, just created object
self.download_fileobj(bucket, filename, f)
except AttributeError:
# download the data into temp file
# copy downloaded content into fileobj
# remove temp file
_file = os.path.join(settings.FILE_LOCATION, filename)
self.download_file(bucket, filename, _file)
with open(_file, 'rb') as file_:
f.write(file_.read())
os.remove(_file)
except exceptions.ClientError:
raise CustomException("File not found")
f.seek(0)
if not fileobj: return f
I'm seeing the same thing on 1.7.62 even though the docs say it should exist: https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.download_fileobj
>>> import boto3
>>> boto3.__version__
'1.7.62'
>>> s3 = boto3.resource("s3")
>>> s3.download_fileobj
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 's3.ServiceResource' object has no attribute 'download_fileobj'
@ns-cweber change how you "create" s3 as follows :
s3 = boto3.client('s3')
You have created s3 as a 'resource' - hence the error.
Ah, that's it. Thanks very much. 馃憤
Most helpful comment
@ns-cweber change how you "create"
s3as follows :s3 = boto3.client('s3')You have created s3 as a 'resource' - hence the error.