Read files in Phoenix in production mode

本秂侑毒 提交于 2019-12-11 06:03:46

问题


I've Phoenix app (I created it with --api flag) that has some additional data required to process requests that I store in priv/data/filters.csv.

So it works fine in dev mode, I can read this file like: File.stream!("priv/data/filters.csv"), but once app is delivered using mix edeliver update this file is not accessible anymore.

I can see this folder in build directory, but after app is delivered there is no such folder priv. I'm not sure why it's not copied to deliver folder.

What I'm doing wrong? Do I need to copy these files after deliver process? Where can I store my files that I have to use in prod mode?


回答1:


You should use :code.priv_dir/1 to get the absolute path to the priv directory of your application at runtime. This will work with Erlang releases created by e.g. Distillery:

File.stream!(Path.join(:code.priv_dir(:my_app), "data/filters.csv"))

For me, after doing MIX_ENV=prod mix release, the file priv/foo is copied to _build/prod/lib/my_app/priv/foo.




回答2:


I had a similar issue and managed to resolve it using Application.app_dir(my_app, "priv")




回答3:


You should check your configuration in the lib/YOUR_APP/endpoint.ex. By default Plug.Static white lists the files that are served. Try adding your files at the only: ~w(...) list.



来源:https://stackoverflow.com/questions/43414104/read-files-in-phoenix-in-production-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!