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
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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










