Missing region use: region option or name of export region for ENV ['AWS_REGION']

I understand that there are other questions that coincide, but they did not solve my problem.

I keep getting an 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, he continues to appear with an error. I followed other people's advice on other issues. Hope someone can help.

Ben

+4
source share
3 answers

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

s3_region: ENV['AWS_REGION']

Or use the line:

s3_region: 'us-west-2'

, s3_region credentials 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'],
  }
}
+7

5 :

. , us-region. paperclip s3 :

  • yml let say aws.yml ( git )
  • i.e development/production.rb config/environments/
  • , . User

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

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",
}

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

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

, :

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
+2

:

config/initializers/paperclip.rb

:

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'

, eu-west-1, , .

-1

Source: https://habr.com/ru/post/1663609/


All Articles