Hi!
Now I am trying to upload image using ex_aws for Amazon AWS S3
Here is my code
def upload_file_to_S3(bucket_name, s3_filename, file_binary) do
S3.put_object(bucket_name, s3_filename, file_binary, [{:acl, :public_read}])
|> ExAws.request()
end
and I hope(?) it returns with link(uploaded image url) but it didn’t return it
This is first time using ex_aws, and trying to find docs about it but can’t find it.
How can I get an upload image url like " https://s3-us-west-1.amazonaws.com/text-marketing/03da408158584cd38a51aea77754d86c.jpg"
so I can use it in html ‘a’ tag or save it to the database.
1 Like
You can find a example here:
defmodule Epr.Services.AwsS3 do
def put_object(bucket, filename, binary_file, opts \\ []) do
ExAws.S3.put_object(bucket, filename, binary_file, opts)
|> ExAws.request()
end
def delete_object(bucket, filename) do
ExAws.S3.delete_object(bucket, filename)
|> ExAws.request()
end
def presigned_url(
method,
bucket,
filename,
config \\ ExAws.Config.new(:s3),
opts \\ [expires_in: 7200]
) do
{:ok, url} = ExAws.S3.presigned_url(config, method, bucket, filename, opts)
url
end
end
4 Likes
one more question.
The url that get from presigned_url expires in some time.
What if I need to display that image in some page, do I need to make a request to S3 to get urls for ever page visit?
What solution do you recommend?