Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 948
Benjamints Avatar asked Dec 13 '16 10:12

Benjamints


2 Answers

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'],
  }
}
like image 135
Andrey Deineko Avatar answered Nov 02 '22 22:11

Andrey Deineko


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
like image 33
Kaleem Ullah Avatar answered Nov 02 '22 23:11

Kaleem Ullah