Skip to content

Instantly share code, notes, and snippets.

@phuochau
Created May 25, 2019 02:23
Show Gist options
  • Save phuochau/1cc27f6ffdfaf7d0f9f788c99e48da56 to your computer and use it in GitHub Desktop.
Save phuochau/1cc27f6ffdfaf7d0f9f788c99e48da56 to your computer and use it in GitHub Desktop.
Generate S3 signed URL with Elixir
defmodule Famiby.Helpers.AWS do
alias Famiby.Helpers
@bucket_name Application.get_env(:ex_aws, :bucket)
@region Application.get_env(:ex_aws, :region)
@env Application.get_env(:famiby, :env)
def s3_signed_url(filename, acl \\ "public-read") do
key = "#{@env}/#{filename}"
content_type = Helpers.File.file_type(filename)
query_params = [
{"ContentType", content_type},
{"x-amz-acl", acl}
]
presign_options = [virtual_host: false, query_params: query_params]
case ExAws.Config.new(:s3) |> ExAws.S3.presigned_url(:put, @bucket_name, key, presign_options) do
{:ok, url} -> {:ok, %{file_url: s3_url(key), signed_url: url, content_type: content_type}}
err -> err
end
end
def s3_url(key) do
"http://#{@bucket_name}.s3-#{@region}.amazonaws.com/#{key}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment