JoeZMar

JoeZMar

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?

Showing Posts 1 to 6

JoeZMar

JoeZMar OP

Nevermind, still blows up

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.

JoeZMar

JoeZMar OP

So this is my first time using Docker, so that would make sense. I previously used to run Nginx and a ubuntu instance.

Right now I created a volume on fly.io and then put this:

[mounts]
  source="artistic_translations_uploads"
  destination="priv/static/uploads"

in my fly.toml. My uploads still blow up after X amount of time goes by. I can console into the app now and they don’t disappear, but after a little bit of time they are gone.

JoeZMar

JoeZMar OP

Okay, so I think I have a grip on it now, but am having another issue.

How do I mount the drive, and access them within the static_path?

fly.toml

[mounts]
  source="artistic_translations_uploads"
  destination="app/uploads"

runtime.exs

config :artistic_translations,
    uploads_dir: "app/uploads"

liveview

def handle_event("save", %{"artwork" => artwork_params}, socket) do
    photo_locations =
      consume_uploaded_entries(socket, :photo_locations, fn %{path: path}, entry ->
        dest = Path.join(["static", "uploads", "#{entry.uuid}-#{entry.client_name}"])
        File.cp!(path, Path.join(uploads_root(), "#{entry.uuid}-#{entry.client_name}"))
        {:ok, "/uploads/" <> Path.basename(dest)}
      end)
...

I can ssh into my console and see that the uploads are being copied into the uploads folder, but my images are not rendering if I use src={~p"/uploads/#{art_path}}"}

What am I doing wrong?

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.

JoeZMar

JoeZMar OP

Thank you, that was exactly what I needed.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews