Pbotsaris

Pbotsaris

Local Liveview Uploads in production

Hello everyone,

I’m trying to deploy an app that uses Liveview uploads to save to both a local /uploads folder as well a S3 bucket. I guess I just wanted to test how both work! Anyway, the upload to S3 is working in production but I’m getting 404 on images after attempting to upload locally. I am deploying with Releases and when I build with images uploaded in dev, they are available in the local /upload folder. But I soon as I try to upload a new image while running the build I get a 404. Any ideas? What am I missing?

Thanks for your help in advance!

Marked As Solved

sfusato

sfusato

In the context of the local dev environment this works great, since the application is running from the project’s root folder. In prod, however, your application is running from within _build/prod/rel/..., thus why the file isn’t copied where you intended to when you pass a relative path.

Now, the uploads directory is pretty much a config concern, meaning I would personally prefer to have it in a config file rather than in a module’s function or a module’s attribute. For prod, pass the absolute path to it.

Example:

# dev.exs
config :labs, upload_path: "priv/static/uploads"
# prod.exs
config :labs, upload_path: "/var/www/labs/priv/static/uploads"

Retrieve it with Application.fetch_env!(:labs, :upload_path)

Also Liked

LostKobrakai

LostKobrakai

As already mentioned the priv directory is not always in the same place relative to the cwd. Use Application.app_dir(:my_app, "priv/static/uploads") instead, which will work in any environment.

mcrumm

mcrumm

Phoenix Core Team

In the :dev and :test environments, the priv dir is a symlink to the /priv dir at the root of your app.

/priv/static/ is included in the .gitignore for a Phoenix app by default, so if I had to guess your priv directory likely exists in prod, but the sub-directories do not.

Somewhere in your production build you can run mkdir -p /path/to/my/app/priv/static/uploads to ensure the directory exists.

PS– With the latest release of LiveView we updated the Uploads guides to use :code.priv_dir(:my_app) when building the path (though I probably would have used Application.app_dir if I had thought about it at the time).

kigila

kigila

Hi guys. You guys are the reason I am progressing in my learning jorney. After two hours debugging I see the solution here. I love you all.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement