AWS S3 integration yields undefined method `match'
Asked Answered
A

2

20

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.

Archduchess answered 10/5, 2016 at 3:56 Comment(3)
I'm having same problem. Did you find a fix yet?Modernity
Did you try adding :s3_region => 'us-east-1' (or whatever the region your s3 is in) Here's a link which may help: github.com/thoughtbot/paperclip/blob/…Modernity
Yes, I found that once I added my AWS region, it seemed to work just fine. As an answer below mentioned, this isn't really thoroughly explained in the docs - and was even more confusing since I'd basically copy pasted code from another project that had worked flawlessly.Archduchess
M
38

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'
Modernity answered 11/5, 2016 at 0:14 Comment(3)
Now that v5 has been released officially, this has been documented in their UPGRADING doc.Slingshot
For info, the :region value is in your file pass. ex : EU (Frankfurt) is eu-central-1Gytle
AWS Regions and Endpoints docs.aws.amazon.com/general/latest/gr/rande.htmlTilda
T
2

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.

Trichloroethylene answered 10/5, 2016 at 5:29 Comment(1)
Did you restart your application after adding :s3_region in development.rb?Obryan

© 2022 - 2024 — McMap. All rights reserved.