简体   繁体   中英

Aws missing region error when uploading image with paperclip gem Amazon S3

I'm relatively new to rails. I had the paperclip gem working fine in development locally with users uploading avatars on sign up with no problems. I deployed to production via Heroku and also had no errors when creating users with uploaded images on signup but all of the images that were uploaded appeared as missing thumbnails in production. I read up and apparently in production with paperclip I should use something called Amazon S3(correct me if there is a better way) so I'm trying to do that.

Here is the error I'm getting when I try to create a user with an uploaded image:

Aws::Errors::MissingRegionError in Devise::RegistrationsController#create
missing region; use :region option or export region name to ENV['AWS_REGION']

here is what I have in my production and development environments

config.paperclip_defaults = {
  storage: :s3,
  s3_host_name: 's3-ap-southeast-1.amazonaws.com',
  s3_credentials: {
    bucket: ENV['AWS_S3_BUCKET'],
    s3_region: ENV['S3_REGION'],
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
  }
}

Here is what I have in my paperclip.rb initializer

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'


    Paperclip::Attachment.default_options[:s3_host_name] = 's3-ap-southeast-1.amazonaws.com'

note(I am in the middle east)

and here is what I have in my user model

#paperclip gem storing avatar images


 has_attached_file :avatar, 
          :bucket => 'bucket_name',
          :styles => {
          :thumb    => ['100x100#',  :jpg, :quality => 70],
         :preview  => ['480x480#',  :jpg, :quality => 70],
         :large    => ['600>',      :jpg, :quality => 70],
         :retina   => ['1200>',     :jpg, :quality => 30]
},
        :convert_options => {
         :thumb    => '-set colorspace sRGB -strip',
         :preview  => '-set colorspace sRGB -strip',
         :large    => '-set colorspace sRGB -strip',
         :retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
}

I have looked for questions regarding S3 Paperclip Gem and missing Region error but have never seen one like this saying the error is in a Devise controller. I think it's because the association between the :avatar is with user which is controlled by devise and I don't have a separate users controller.

I feel like there should be an easier way to solve the problem of having missing images in production with paperclip gem than using this amazon S3 thing. Especially if everything was working fine and dandy locally without Amazon S3. Any alternative suggestions? Ideas?

You do not necessarily have to use Amazon services to store your images, but if you host on Heroku you can't store locally. This is because heroku uses an ephemeral file system, so you must expect that anything you store locally might disappear at any point. This makes things much easier when you need to scale your application, as you don't rely on any local storage if you spin up new dynos (it also makes Heroku's life easier, as they don't need to bother about shared storage that all of an app's dynos should mount).

Now, back to S3. First, you do have alternatives, if you use the 'fog' gem with paperclip, you can access a number of storage providers in a relatively easy manner.

Still, S3 is the most common and your problem should be easy to sort out by defining the AWS_REGION variable. On Heroku, you can do this by running the following:

heroku config:set AWS_REGION=us-west-2

Or whatever region you are using.

Also, last I checked paperclip was not compatible with AWS SDK 2, so if you have strange issues you can try switching back to the SDK 1 by using this in your Gemfile:

gem 'aws-sdk', '< 2.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM