Hasimbola

Hasimbola

Store generated file inside created path

Hello , I have this code

def get_image do

{:ok, query} = Repo.query("SELECT photolink FROM produits")

for c <- query.rows do

  {:ok, photo} = Enum.fetch(c, 0)

  File.write("/tmp/#{photo}", photo, [:binary])

end

end

I want to store the file generated from File.write inside my project such as inside “/priv/uploads/file_name”
I am able to store the file inside /tmp but I have problem when I try to store the file inside another path

Please, can someone help me, thanks a lot

Thanks! :smiley:

Marked As Solved

egze

egze

"/priv/uploads/#{photo}" is an absolute path and doesn’t exist on your hard drive.

Look at this thread: Accessing the priv folder from Mix release - #2 by NobbZ

What you need is something like this:

"#{List.to_string(:code.priv_dir(:your_app_name))}/uploads/#{photo}"

or use a relative path.

Also Liked

LostKobrakai

LostKobrakai

This can be done simpler:
Application.app_dir(:your_app_name, Path.join("priv/uploads", photo))

NobbZ

NobbZ

In general you do not want to write to priv! You should consider it as a read only artifact store.

If you need to write to a persistent location, you should manage it aside to your application, and backup it seperately.

In general the priv folder is part of your release, if you really change its content, you invalidate the integrity of your release.

Files are stored in a separete section of your filesystem or in an S3 bucket or whatever file storage backend you use, and as such has to be considered state of your application similar to your PostgreSQL database.

LostKobrakai

LostKobrakai

What is the problem?

Hasimbola

Hasimbola

The program doesn’t found the path
I want to change

File.write(“/tmp/#{photo}”, photo, [:binary])

to something like this :

File.write(“/priv/uploads/#{photo}”, photo, [:binary])

but the path is not found , this return :

[{:error, :enoent}]

LostKobrakai

LostKobrakai

Are you sure parent folders of that path exist?

Where Next?

Popular in Questions Top

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement