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:

What is the problem?

1 Like

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}]

1 Like

"/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.

2 Likes

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

4 Likes

This always return

[{:error, :enoent}]

i’ve tried this but this return
{[:error, :enoent]}

Are you sure parent folders of that path exist?

1 Like

I have /priv/uploads inside my app but it is empty after that I do the File.write/3
I don’t understand why, I receive :ok but when I open the folders it is empty, :pensive:

1 Like

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.

1 Like