Reputation: 564
I would like to request an S3 image and then serve it using Phoenix.
def getImage(conn, %{"id" => uuid}) do
file = ExAws.S3.get_object("bucket", "images/image.jpg")
|> ExAws.request
conn
|> put_resp_content_type("image/jpg")
|> put_resp_header(
"content-disposition",
"attachment; filename=\"file.jpg\""
)
|> send_resp(200, file)
end
I have found endless tutorials on how to upload to S3 but nothing on retrieving. Thank you in advance!
Upvotes: 1
Views: 474
Reputation: 10298
You have to pattern match against get_object
function and extract image content from that.
def getImage(conn, %{"id" => uuid}) do
{:ok, %{body: image_content}} = ExAws.S3.get_object("bucket", "images/image.jpg")
|> ExAws.request
conn
|> put_resp_content_type("image/jpg")
|> put_resp_header(
"content-disposition",
"attachment; filename=\"file.jpg\""
)
|> send_resp(200, image_content)
end
Upvotes: 4