Issue
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 rel="nofollow noreferrer">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.
- ex_aws dependency
- partial explanation (note: you need to add poison and hackney to make this work, they are not mentioned)
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.
Answered By - Navin Peiris Answer Checked By - Marie Seifert (WPSolving Admin)