tao
Accessing the priv folder from Mix release
I’m deploying a Phoenix app with Mix releases. I’d like to access a static file, stored in the priv/ directory, during runtime.
When I enter the running app, I can see the following directory structure:
iex(backend@5714fba14b09)10> File.ls()
{:ok, ["lib", "tmp", "bin", "erts-10.4.4", "releases", "Procfile"]}
If I want to access anything in priv, I have to use the full path lib/backend-2.2.0/priv/.../myfile.json (for example). I could write code to generate this path based on the version and app name but that seems fragile – is there a better way? I tried looking into how Ecto does this for migrations, but with no luck!
Most Liked
NobbZ
The most idiomatic and most reliable and only correct way is to use :code.priv_dir/1.
tao
Thank you both!
I’ve noticed that neither Application.app_dir/2 not :code.priv_dir/1 seem to work in config files, since the app does not exist yet. Is there any way around this?
LostKobrakai
If you do Application.app_dir(:myapp, "priv/static/file.ext") it’ll work. It surely isn’t the same as :code.priv_dir as it’s always starting at the parent of the priv dir, but it works. E.g. Plug.Static is using it.
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









