missing region; use :region option or export region name to ENV['AWS_REGION']
Asked Answered
S

3

9

I understand there are other questions that are the same, but they have no solved my problem.

I keep on receiving the error: Aws::Errors::MissingRegionError in BooksController#create,

missing region; use :region option or export region name to ENV['AWS_REGION']. However, this is my configuration in my

Development.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

Production.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

And Application.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

However, it keeps coming up with the error. I have followed other people's advice from other questions. Hope someone can help.

Ben

Seedcase answered 13/12, 2016 at 10:18 Comment(2)
Nope, same error.Seedcase
Try putting s3_region key outside of s3_credentials hash. In the same level as that of s3_host_name.Talipes
M
12

You should either set the ENV['AWS_REGION'] env variable to "us-west-2" and use it as

s3_region: ENV['AWS_REGION']

Or use a string:

s3_region: 'us-west-2'

Also, s3_region option should me moved out of credentials hash in config/environments/{development|production}:

config.paperclip_defaults = {
  storage: :s3,
  s3_host_name: "s3-us-west-2.amazonaws.com",
  s3_region: 'us-west-2', # or ENV['AWS_REGION']
  s3_credentials: {
    bucket: ENV['AWS_BUCKET'],
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  }
}
Minium answered 13/12, 2016 at 10:20 Comment(2)
Thanks, I tried moving the S3 Region outside my hash, but still to no avail it didn't work.Seedcase
Changing it to s3_region: 'us-west-2' worked! Without the 'ENV'. Thanks!Seedcase
A
4

Rails 5 Way:

according to updated documentation region is necessary. where as it was necessary for non us-region. and the recommended way to setup paperclip with s3 is:

  1. put your secret info in yml file let say aws.yml (must be git ignored)
  2. put your global configurations in environmental files i.e development/production.rb in config/environments/
  3. put your files related setting in model. in my case I am defining image properties in User model

example step-1: (config/aws.yml)

development:
  access_key_id: AWS_ACCESS_KEY_ID # without quotation marks
  secret_access_key: AWS_SECRET_KEY_ID # without quotation marks

production:
  access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %> # get it from terminal environment
  secret_access_key: <%= ENV["AWS_SECRET_KEY_ID"] %> # get it from terminal environment

example step-2: (config/environments/development.rb)

# US West (N. California)  us-west-2   apigateway.us-west-2.amazonaws.com  HTTPS

config.paperclip_defaults = {
  :storage => :s3,
  :s3_region => 'us-west-2',
  :bucket => 'production-bucket',
  :path => '/:class/:attachment/:id_partition/:style/:filename',
  :s3_credentials => "#{Rails.root}/config/aws.yml",
}

example step-3: (app/models/user.rb)

has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

finally you can upload file:

def upload_to_s3
    response = {JSON_KEY_STATUS_MESSAGE: "OK", server_time: DateTime.current.to_i}
    response[:user] = []
    response[:status] = '0'

    unless params[:avatar].present?
        response[:message] = 'either user avatar image file [avatar]'
        render json: response and return
    end

    begin
        user = User.new # create new user
        user.avatar = params[:avatar] # params[:avatar] is a file posted by form in mutli-part true over json api
        s = user.save # save it # will through error if you have more than one required attributes
        if(s != false)
            response[:message] = 'file Successfully upload to s3'
        else
            response[:message] = 'fail to upload file to s3'
        end
    rescue => e
        response[:message] = e.message # this guy will help debug a lot!
    end
    render json: response and return
end
Andrade answered 16/3, 2017 at 13:30 Comment(0)
K
-1

create a file called:

config/initializers/paperclip.rb

And add the following:

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-eu-west-1.amazonaws.com'

And in the 3rd line where I have eu-west-1 replace that depending on the region you are in.

Koral answered 13/12, 2016 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.