Ruby-on-rails Rails 3. 在生产中上传文件时获得 Errno::EACCES 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8854725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:37:34  来源:igfitidea点击:

Rails 3. getting Errno::EACCES Permission Denied when uploading files on production

ruby-on-railsrubycarrierwave

提问by leonel

The app works fine in development but in production I get Errno::EACCES Permission Denied error when I try to upload a file using Carrierwave. I'm sure it has something to do with permissions. How can I set the permissions to allow file uploads?

该应用程序在开发中运行良好,但在生产中,当我尝试使用 Carrierwave 上传文件时,我收到 Errno::EACCES Permission Denied 错误。我确定它与权限有关。如何设置允许文件上传的权限?

pdf_uploader.rb

pdf_uploader.rb

def store_dir
  "#{Rails.root}/uploads/#{model.id}"
end

def cache_dir
  "#{Rails.root}/tmp/uploads/cache/#{model.id}"
end

回答by alexkv

chmod -R 777 PATH_TO_APP/uploads 
chmod -R 777 PATH_TO_APP/tmp 

回答by Starkers

As far as I know there are two things that can be going on here:

据我所知,这里可能会发生两件事:

1) The directory you're saving your images to doesn't have read/write privileges for other users.

1) 您保存图像的目录对其他用户没有读/写权限。

To fix:

修理:

terminal

终端

$ cd [my_app]
$ chmod -R 666 tmp
$ chmod -R 666 public/uploads

or if you're saving your images in an private directory:

或者如果您将图像保存在私人目录中:

$ chmod -R 666 private/uploads

We're using 666 over 777. 666 allows read and write privileges to a directory, and carrierwave needs to write its images. 777 allows read, write privileges and for executable files to be executed!In other words, a nasty program could be uploaded to your server disguised as an image if you're using 777. Even though carrierwave's extension white-list solves this problem, you should always use 666 over 777.

我们在 777 上使用 666。666 允许对目录的读写权限,并且carrierwave 需要写入其图像。777 允许读、写权限和可执行文件的执行!换句话说,如果您使用的是 777,一个讨厌的程序可能会伪装成图像上传到您的服务器。即使carrierwave 的扩展白名单解决了这个问题,您还是应该始终使用 666 而不是 777。

2) You're not using double quoted strings in the store_dirmethod.

2)您没有在store_dir方法中使用双引号字符串。

To fix:

修理:

app/example_uploader.rb

应用程序/example_uploader.rb

class BaseUploader < CarrierWave::Uploader::Base
  # other methods removed for brevity

  def store_dir
    "#{Rails.root}/private/" # works perfectly. Many thanks to @RGB
  end

end

Just want to point out how subtle this is. You need double quoted strings and Rails.root!I was doing this:

只是想指出这是多么微妙。您需要双引号字符串和Rails.root! 我是这样做的:

def store_dir
    Rails.root + '/private' # raises Errno::EACCES error
end

and it was not working at all. So subtle. The community should address this.

它根本不起作用。如此微妙。社区应该解决这个问题。

回答by Hiromichan

Uhm I have been having the same issue with a ubuntu server. Uploading a file with carrierwave and then trying to read it with roo (a gem for excel files).

嗯,我在 ubuntu 服务器上遇到了同样的问题。使用carrierwave上传文件,然后尝试使用roo(excel文件的gem)读取它。

Errno::EACCES in IngestionController#upload
Permission denied

Permissions have been chmod-ed to 777 on that directory and the file gets created ok. I believe the issues is when reading the store path.

该目录的权限已被修改为 777,文件创建正常。我相信问题在于阅读商店路径时。

excelx_file = params[:excel_file]
filex = MetadataUploader.new
filex.store!(excelx_file)
workbook = Excelx.new("#{filex.store_path}") <- This is the actual line throwing the error.

Although everything works ok when executing the same app on my mac.

虽然在我的 mac 上执行相同的应用程序时一切正常。

回答by Jai Kumar Rajput

We need to grant permissions to access the required directory for the system root user

我们需要为系统root用户授予访问所需目录的权限

sudo chmod 777 -R your_project_directory_to_be_access

For security reasons, just keep in your mind:

出于安全原因,请记住:

chmod 777gives everybody read, write and execute rights which for most problems is definitively too much.

chmod 777给每个人读、写和执行的权限,这对于大多数问题来说绝对是太多了。