JoeZMar

JoeZMar

Uploading images to server

I currently have an app deployed to fly.io and I am having issues trying to upload images straight to the phoenix server.

I want my images to be uploaded to a /static/uploads folder and served from there.

main_photo_location =
      consume_uploaded_entries(socket, :main_photo_location, fn %{path: path}, entry ->
        dest = Path.join([:code.priv_dir(:artistic_translations), "static", "uploads", "#{entry.uuid}-#{entry.client_name}"])
        IO.inspect(dest, label: "DESTINATION")
        # The `static/uploads` directory must exist for `File.cp!/2`
        # and MyAppWeb.static_paths/0 should contain uploads to work,.
        File.mkdir_p!(Path.dirname(dest))
        File.cp!(path, dest)
        {:ok, "/uploads/" <> Path.basename(dest)}
      end)

    [main_photo_location | _] = main_photo_location

    artwork_params =
      Map.put(artwork_params, "photo_locations", photo_locations)
      |> Map.put("main_photo_location", main_photo_location)

Here is an excerpt from my save event to consume the uploaded images.

I have added my uploads folder to the static_path route so that they can be served.

Right now, I can upload images and they appear where I expected them to be, but if the app restarts or is deployed again they break.

So I ssh into my console and check /app/lib/my_app/priv/static and there is no uploads folder, but the images work when I upload them. I don’t want to use an S3 bucket because there are only a handful of images that need to be uploaded for now. How can I upload to a folder called uploads in my static directory and have it persist when I redeploy? I did rely on the File.mkdir_p!(Path.dirname(dest)) command to create the folder if it doesn’t exist. Any ideas as to what I am doing wrong?

Marked As Solved

thomas.fortes

thomas.fortes

Can’t test now, but you can try to set up a Plug.Static like this in your endpoint.ex, and adjust at to "/uploads", from to the uploads dir in the system ("/app/uploads"), and since the uploaded files are probably at the root of /app/uploads you don’t need the only option.

Also Liked

LostKobrakai

LostKobrakai

You need volumes for that. As with any docker based system files won’t persist across instances unless you mount some persistent storage into the running docker instance.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement