AWS S3 integration yields undefined method `match'

PSCampbell picture PSCampbell · May 10, 2016 · Viewed 8.3k times · Source

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.

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.

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

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.

Answer

rguerrettaz picture rguerrettaz · May 11, 2016

Add :s3_region to your config map:

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.

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

:s3_permissions => 'public-read'