凤凰文件复制Heroku [英] Phoenix file copying on Heroku

查看:157
本文介绍了凤凰文件复制Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图片上传到Heroku的凤凰应用程序中。我有一个简单的应用程序,遵循从 Phoenix的文档进行文件上传的说明。



我有一个简单的表单,控制器使用 File.cp_r()从temp目录复制文件。 / p>

  def create(conn,%{user=> user_params})do 
if upload = user_params [照片] do
File.cp_r(upload.path,priv / static / images / foo.jpg)#非动态名称,doens't问题
结束
...
end

只在我的本地存档。但是,当我上传到Heroku时,测试表单并在目录中测试 heroku run find ,我什么也没看到。



我注意到在heroku上的这个目录有一个看似不可允许的特权:

drwx ------ 2 u25619 dyno 4096 4月23日05:14 images



我尝试在 File.chmod(priv / static /图片,0o777),但无济于事;该目录似乎与我无关,所以我认为这是一个heroku问题。



任何想法如何处理?



编辑:通过使用phoenix dep ex_aws 来上传到Amazon S3存储桶来解决。




在heroku上的文件系统是短暂的,您将无法访问在部署或开始新实例时保存在其上的任何文件。

解决方案



另外,当您运行heroku运行时,您不会连接到当前正在运行应用程序的同一实例,而是会启动一个新实例所以这些上传的文件将不存在那里。



更好的方法是将上传的文件保存到S3或类似的地方,您仍然可以通过部署访问它。


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.

解决方案

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.

这篇关于凤凰文件复制Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆