Displaying a file (image) from S3 via Flask & BytesIO Displaying a file (image) from S3 via Flask & BytesIO flask flask

Displaying a file (image) from S3 via Flask & BytesIO


Rewinding your BytesIO object should do the trick, with file.seek(0) just before send_file(...).

For the record I'm not sure your boto3/botocore calls are "best practices", to try your usecase I ended up with:

from boto3.session import Sessionsession = Session(    aws_access_key_id=KEY_ID, aws_secret_access_key=ACCESS_KEY, region_name=REGION_NAME)s3 = session.resource("s3")@base_bp.route("/test-stuff")def test_stuff():    a_file = io.BytesIO()    s3_object = s3.Object(BUCKET, PATH)    s3_object.download_fileobj(a_file)    a_file.seek(0)    return send_file(a_file, mimetype=s3_object.content_type)

It works on when reading the file from disk because you instanciate your BytesIO with the full content of the file, so it's properly fulfilled and still at "position 0".