阅读背景:

AWS S3集成产生未定义的方法“匹配”

来源:互联网 

I'm working on a simple project using Paperclip to upload images. Everything has been working just fine until I attempted to integrate S3 with Paperclip. Upon 'uploading' a user's image I get a NoMethodError (undefined method 'match' for nil:NilClass): error. This only happens when I have my S3 configuration running - if I comment it out the file uploads perfectly.

我正在做一个使用Paperclip上传图片的简单项目。在我尝试将S3与Paperclip集成之前,一切正常。在“上传”用户的图像时,我得到一个NoMethodError(为nil:NilClass): error。只有在我的S3配置运行时才会发生这种情况——如果我把它注释掉,文件就会被完美地上传。

My configuration:

我的配置:

development.rb:
....
....
  config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['AWS_BUCKET_ID'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

My Model:

我的模型:

 class User < ActiveRecord::Base
        has_attached_file :image_file, default_url: "/myapp/images/:style/missing.png"
        validates_attachment_file_name :image_file, matches: [/png\Z/, /jpeg\Z/, /tiff\Z/, /bmp\Z/, /jpg\Z/]

entire error output from console:

控制台输出的全部错误:

NoMethodError (undefined method `match' for nil:NilClass):
  app/controllers/images_controller.rb:33:in `block in create'
  app/controllers/images_controller.rb:32:in `create'

Things I tried:

我试过的东西:

  • I added the AWS keys and bucket name directly into the code instead of as an environmental variable.

    我将AWS键和桶名直接添加到代码中,而不是作为环境变量。

  • As mentioned above, I commented out the AWS configuration in my environment file and it seemed to work perfectly.

    如上所述,我在我的环境文件中注释了AWS配置,它似乎工作得很好。

It's probably worth mentioning that I installed the fog gem earlier to start configuring for Google Cloud Storage, but decided to stick with S3 instead. I used gem uninstall fog to remove the gem but it appears some dependencies stayed behind.

值得一提的是,我早些时候安装了fog gem以开始为谷歌云存储进行配置,但是我决定继续使用S3。我使用了gem卸载fog来移除gem,但是它似乎有一些依赖项留在了后面。

2 个解决方案

#1


30  

Add :s3_region to your config map:

添加:s3_region到配置映射:

E.g.

如。

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['AWS_BUCKET_ID'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  },
  :s3_region => ENV['AWS_REGION']
}

Unfortunately this is a v5.0.0 feature (in Beta). And therefore there's nothing about it in the docs. However there is a comment about it in the actual 5.0 code.

不幸的是,这是v5.0.0特性(在Beta中)。因此,在文档中没有关于它的内容。但是在实际的5.0代码中有一个关于它的注释。

Also good to note that the s3_permissions key in papertrail expects a string value now (it used to accept symbols).

同样值得注意的是,papertrail中的s3_permissions键现在期望得到一个字符串值(它过去接受符号)。

:s3_permissions => 'public-read'

#2


3  

You need to specify the region in you s3_credentials, in a way such as region: ENV["AWS_REGION"]. As for cleaning up unused gems you can run bundle clean.

您需要以区域:ENV[“AWS_REGION”]等方式在您的s3_credentials中指定区域。至于清理未使用的宝石,你可以把它清理干净。


分享到: