简体   繁体   中英

Rails4.1: AWS errors missing region

I'm trying to create an image uploader using paperclip, the aws-sdk gem and Amazon S3. I get this error:

Aws::Errors::MissingRegionError in ProjectsController#create. 
Missing region; use :region option or export region name to ENV['AWS_REGION']. using Oregon region. 

projects_controller.rb

def create
    @project = current_user.projects.new(project_params)
    @project.save
    respond_with(@project)
end

application.rb file

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']
        }
    }

According to the S3 documentation I understand I need to change to ENV['AWS_REGION']

config.paperclip_defaults = {
        storage: :s3,
            region: "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']
        }
    }

and add region to my .env file

export AWS_BUCKET=realv1
export AWS_ACCESS_KEY_ID=<MY_ACCESS_KEY_ID_HERE>
export AWS_SECRET_ACCESS_KEY=<MY_ACCESS_KEY_HERE>
export AWS_REGION="'us-west-2'

I rebooted the server and retested but still get the same errors.

update

update:

in rails console

2.0.0p247 :001 > ENV["AWS_REGION"]

=> "us-west-2" 2.0.0p247 :002 >

The error is originating from the Paperclip gem. In your config.paperclip_defaults you used region when you should have used s3_region . This is how the config should look:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_region => ENV['AWS_REGION'],
  :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

You have a syntax error in your .env file which is most probably causing this error.

Change:

export AWS_REGION="'us-west-2'

to:

export AWS_REGION='us-west-2'

Reload the shell and try again.

Upate

Make sure your ENV hash has the correct region . You can check by typing: ENV | grep AWS ENV | grep AWS in your console.

Then, add region: ENV['AWS_REGION'] to your s3_credentials :

config.paperclip_defaults = {
        storage: :s3,
            region: 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']
        }
    }

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