Problems uploading file

Hello all,

I’m having issues while trying to upload a CSV file to my Phoenix with LiveView site hosted in fly.io. It works fine locally and even tried deploying to Heroku and worked fine too without any change.

The error I’m getting is the following (fly logs):

2022-08-17T13:02:18Z app[2d9c3130] dfw [info]13:02:18.654 [error] GenServer #PID<0.2223.0> terminating
2022-08-17T13:02:18Z app[2d9c3130] dfw [info]** (File.CopyError) could not copy from “/tmp/plug-1660/live_view_upload-1660741336-554237837513481-1” to “priv/static/uploads/live_view_upload-1660741336-554237837513481-1”: no such file or directory
2022-08-17T13:02:18Z app[2d9c3130] dfw [info] (elixir 1.13.0) lib/file.ex:818: File.cp!/3
2022-08-17T13:02:18Z app[2d9c3130] dfw [info] (sales_point 0.1.0) lib/sales_point_web/live/product_live/import_component.ex:32: anonymous fn/2 in SalesPointWeb.ProductLive.ImportComponent.consume_uploaded_document/1
2022-08-17T13:02:18Z app[2d9c3130] dfw [info] (phoenix_live_view 0.17.11) lib/phoenix_live_view/upload_channel.ex:21: Phoenix.LiveView.UploadChannel.consume/3

I hope someone can help me.

Thanks,
Rodolfo Baeza.

It looks like “priv/static/uploads/ doesn’t exist on the fly instance, but you won’t want to save files there anyway because that is not a going to save files in a place that survives restarts/deploys. You’ll want to mount a volume and make your prod upload location be at that mount point. For example after mounting a volume, you can have something like this in your fly.toml:

$ fly volumes create data --size 10
[mounts]
  source="data"
  destination="/app/uploads"

Now if you fly ssh console, you should see an /app/uploads directory on your instance. Finally, your prod upload directory can reference environment configuration for the root upload directory, which you can set to this location:

in config/runtime.exs:

config :my_app,
  uploads_dir: "/app/uploads"

And somewhere in your app code you can do something :

def whatever(...) do
  File.cp!(tmp_path, Path.join(uploads_root(), new_file_basename))
end

defp uploads_root, do: Application.get_env(:my_app, :uploads_dir)

Make sense?

1 Like

Thanks Chris it worked! Is there any documentation about this? I couldn’t find anything while trying to fix the issue before.