Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 integration yields undefined method `match'

Tags:

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.

like image 588
PSCampbell Avatar asked May 10 '16 03:05

PSCampbell


People also ask

Does S3 Putobject overwrite?

If an object already exists in a bucket, the new object will overwrite it because Amazon S3 stores the last write request.

Can an S3 bucket run out of space?

S3 provides unlimited scalability, and there is no official limit on the amount of data and number of objects you can store in an S3 bucket. The size limit for objects stored in a bucket is 5 TB.


2 Answers

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'
like image 139
rguerrettaz Avatar answered Oct 05 '22 06:10

rguerrettaz


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.

like image 39
Yimanei Avatar answered Oct 05 '22 06:10

Yimanei