Joseph Horsch
Joseph Horsch

Reputation: 564

Elixir Phoenix Serve S3 Image

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

Answers (1)

vahid abdi
vahid abdi

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

Related Questions