JoeZMar

JoeZMar

Uploading images to server

I currently have an app deployed to fly.io and I am having issues trying to upload images straight to the phoenix server.

I want my images to be uploaded to a /static/uploads folder and served from there.

main_photo_location =
      consume_uploaded_entries(socket, :main_photo_location, fn %{path: path}, entry ->
        dest = Path.join([:code.priv_dir(:artistic_translations), "static", "uploads", "#{entry.uuid}-#{entry.client_name}"])
        IO.inspect(dest, label: "DESTINATION")
        # The `static/uploads` directory must exist for `File.cp!/2`
        # and MyAppWeb.static_paths/0 should contain uploads to work,.
        File.mkdir_p!(Path.dirname(dest))
        File.cp!(path, dest)
        {:ok, "/uploads/" <> Path.basename(dest)}
      end)

    [main_photo_location | _] = main_photo_location

    artwork_params =
      Map.put(artwork_params, "photo_locations", photo_locations)
      |> Map.put("main_photo_location", main_photo_location)

Here is an excerpt from my save event to consume the uploaded images.

I have added my uploads folder to the static_path route so that they can be served.

Right now, I can upload images and they appear where I expected them to be, but if the app restarts or is deployed again they break.

So I ssh into my console and check /app/lib/my_app/priv/static and there is no uploads folder, but the images work when I upload them. I don’t want to use an S3 bucket because there are only a handful of images that need to be uploaded for now. How can I upload to a folder called uploads in my static directory and have it persist when I redeploy? I did rely on the File.mkdir_p!(Path.dirname(dest)) command to create the folder if it doesn’t exist. Any ideas as to what I am doing wrong?

Marked As Solved

thomas.fortes

thomas.fortes

Can’t test now, but you can try to set up a Plug.Static like this in your endpoint.ex, and adjust at to "/uploads", from to the uploads dir in the system ("/app/uploads"), and since the uploaded files are probably at the root of /app/uploads you don’t need the only option.

Also Liked

LostKobrakai

LostKobrakai

You need volumes for that. As with any docker based system files won’t persist across instances unless you mount some persistent storage into the running docker instance.

Last Post!

JoeZMar

JoeZMar

Thank you, that was exactly what I needed.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement