venomnert

venomnert

AWS S3 File uploading using presigned url

Background

I am trying to generate a presigned url which I can send to a react frontend. However I am struggling to generate a presigned url with ex_aws_s3.

Dependencies

{:ex_aws, "~> 2.1.2"},
{:ex_aws_s3, "~> 2.0"},
{:hackney, "~> 1.9"},
{:sweet_xml, "~> 0.6"}

Shell

source .env
iex -S mix phx.server

IEX code

bucket = "fls-bm-app"
path = "/place_image/"

query_params = [{"ContentType", "image/png"}, {"ACL", "public-read"}]
presign_options = [virtual_host: true, query_params: query_params]

ExAws.Config.new(:s3)
|> ExAws.S3.presigned_url(:put, bucket, path, presign_options)

This returns a the following url
https://fls-bm-app.s3.amazonaws.com/place_image/?ACL=public-read&ContentType=image%2Fpng&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA5BRXG4R66NZ6MWLO%2F20220205%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220205T200722Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=63de6df3730ea5a70c73035

When trying to push a file from the frontend to the above url it creates the folder /place_image/ only, but it doesn’t upload the file.

I am stumped as to where to go from here.

Marked As Solved

venomnert

venomnert

Thank you @riebeekn for the suggestion, this is exactly what I was looking for.

I created a video going more into detail about the implementation and usage of it.

Related article: Demystifying direct uploads from the browser to Amazon S3 - with a full example in 167 lines of code

Also Liked

riebeekn

riebeekn

Sounds like you are trying to push the file vs downloading it? I could be wrong, but I though presigned_url was only for getting a download link, maybe you need to use presigned_post?

For uploading files I’ve used put_object, i.e.

      local_file = File.read!(src_path)

      ExAws.S3.put_object(@bucket, dest_path, local_file)
      |> ExAws.request!()

I don’t know if that will be of us to you however, given you are using react on the frontend.

axelson

axelson

Scenic Core Team

Try putting the full file name in this path. So something like “/place_image/myfile.jpg”

Although I would recommend presigned post as well because then you can avoid a CORS preflight request.

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement