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.

PSCampbell
  • 858
  • 9
  • 27
  • I'm having same problem. Did you find a fix yet? – rguerrettaz May 10 '16 at 22:53
  • 1
    Did you try adding :s3_region => 'us-east-1' (or whatever the region your s3 is in) Here's a link which may help: https://github.com/thoughtbot/paperclip/blob/95acf3b898bd782f4429d42fd2d8812505390328/lib/paperclip/storage/s3.rb#L96 – rguerrettaz May 10 '16 at 23:30
  • 1
    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. – PSCampbell May 11 '16 at 04:27

2 Answers2

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'
rguerrettaz
  • 836
  • 8
  • 12
  • Now that v5 has been released officially, this has been documented in their [UPGRADING](https://github.com/thoughtbot/paperclip/blob/4ef27eb84fe283f5dbe62fd99d7359822e79db28/UPGRADING#L10) doc. – Nick Jul 06 '16 at 21:52
  • 1
    For info, the `:region` value is in your file pass. ex : `EU (Frankfurt)` is `eu-central-1` – Alain ANDRE Feb 23 '17 at 19:16
  • AWS Regions and Endpoints http://docs.aws.amazon.com/general/latest/gr/rande.html – askrynnikov Dec 27 '17 at 06:02
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.

Yimanei
  • 242
  • 2
  • 8