I'm not sure if this is a duplicate, I've seen plenty of questions on returning an image from a flask endpoint but what happens when the photo you're trying to return resides remotely? Eg, in an S3 bucket?
Originally my code returns an image from a python flask endpoint when there is a GET request.
class ReturnImage(Resource):
def get(self):
#Some code here
full_file_path = '/local/home/pic.gif'
return send_file(full_file_path,mimetype='image/gif')
Except that only works when the image pic.gif
is stored locally on my machine. If I now want to return an image that is not stored locally, how do I do it? I tried to use urllib
library to download the image and then return the output of that but doesn't seem to do the trick. Below is my attempt,
import urllib.request
class ReturnImage(Resource):
def get(self):
#Some code here
full_file_path = '/local/home/pic.gif'
image = urllib.request.urlretrieve("https://s3-us-west-2.amazonaws.com/pic.gif")
return send_file(image,mimetype='image/gif')