Rails 3.获取Errno :: EACCES权限在生产时上传文件时被拒绝

时间:2021-12-23 22:58:03

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

4 个解决方案

#1


15  

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

#2


2  

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.

权限已在该目录上chmod至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上执行相同的应用程序时一切正常。

#3


2  

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.

我们使用666而不是777. 666允许对目录的读写权限,而carrierwave需要写入其图像。 777允许执行读,写权限和可执行文件!换句话说,如果您正在使用777,可以将令人讨厌的程序上传到您的服务器伪装成图像。尽管carrierwave的扩展名white-list解决了这个问题,但您应该始终使用666而不是777。

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

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.

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

#4


-1  

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 777 gives everybody read, write and execute rights which for most problems is definitively too much.

chmod 777为每个人提供了读,写和执行权限,这对于大多数问题来说确实太过分了。

#1


15  

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

#2


2  

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.

权限已在该目录上chmod至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上执行相同的应用程序时一切正常。

#3


2  

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.

我们使用666而不是777. 666允许对目录的读写权限,而carrierwave需要写入其图像。 777允许执行读,写权限和可执行文件!换句话说,如果您正在使用777,可以将令人讨厌的程序上传到您的服务器伪装成图像。尽管carrierwave的扩展名white-list解决了这个问题,但您应该始终使用666而不是777。

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

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.

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

#4


-1  

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 777 gives everybody read, write and execute rights which for most problems is definitively too much.

chmod 777为每个人提供了读,写和执行权限,这对于大多数问题来说确实太过分了。