Search code examples
herokuelixirfile-permissionsphoenix-frameworkchmod

Phoenix file copying on Heroku


I'm trying to upload images to my Phoenix app on Heroku. I have a simple app that follows the instructions for file uploading from Phoenix's docs.

I have a simple form and the controller uses File.cp_r() to copy the file from the temp directory.

  def create(conn, %{"user" => user_params}) do
    if upload = user_params["photo"] do
      File.cp_r(upload.path, "priv/static/images/foo.jpg") #non-dynamic name, doens't matter
    end
    ...
  end

Works just file on my local. However when I upload this to Heroku, test the form and heroku run find on the directory, I don't see anything.

I have noticed that this directory on heroku has a seemingly forbidding privilege:

drwx------ 2 u25619 dyno 4096 Apr 23 05:14 images

I tried slipping in a nice little File.chmod("priv/static/images", 0o777), but to no avail; that directory seems locked away from me, so I think this is a heroku issue.

Any idea how to handle this?

EDIT: Resolved by using the phoenix dep ex_aws to upload to an Amazon S3 bucket.


Solution

  • The file system on heroku is ephemeral, and you won't have access to any files that you save on it across deploys or when you start new instances.

    Also, when you run heroku run, you're not connecting up to that same instance that's currently running your app, instead what it'll do is to launch a new instance so those uploaded files would not exist there.

    A better approach is to save the uploaded files to S3 or similar where you can still access it across deploys.