简体   繁体   中英

Ruby on Rails app can't query AWS - error 403 forbidden

I was in a course last year to develop RoR applications. I developed one that searches AWS using my account's key and returns results. Now whenever I search Amazon I'm getting a 403 error.

I recreated my key and I've updated it in the Rails console but to no avail. How can I check my credentials or what else can I do to start hunting this problem down?

I would set the location of the keys in a new .yml file called aws.yml under config/locales/ as following:

development:
  access_key_id: XXXXXXXXXXXXXX

  secret_access_key: XXXXXXXXXXXXXXXX
  s3_region: eu-central-1
test:
  access_key_id: XXXXXXXXXXXXXXXX

  secret_access_key: XXXXXXXXXXXXXXXX
  s3_region: eu-central-1
production:
  access_key_id: XXXXXXXXXXXXXXXX

  secret_access_key: XXXXXXXXXXXXXXXX
  s3_region: eu-central-1

And then in the model which has the attached file as in:

has_attached_file :img

You can set the s3_credentials as

s3_credentials: "#{Rails.root}/config/aws.yml",

So the final result in your model should look like this:

has_attached_file :img,
              styles: { :model_index => "250x350", :img_show => "325x475"},
              storage: :s3,
              url: ':s3_domain_url',
              bucket: 'yourbucket',
              s3_credentials: "#{Rails.root}/config/aws.yml",
              path: "resources/:id/:style/:basename.:extension"

And then in your view:

<%= @model.model_img.url(:img_show) %>

And there you have it!

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