Portfolio and Amazon S3 issue

I have a rails application running on Heroku. I use paperclip for some simple image downloads for user avatars and some other things, I have S3 installed as my backend and everything seems to work fine except when trying to click on S3. I get the following error:

The AWS Access Key Id you provided does not exist in our records. 

I think that I incorrectly stuck my passkey and secret key, I tried again, still no luck. I think that it was just a buggy key, I disabled it and created a new one. Not lucky yet.

Now for both keys I used the S3 browser application in OS X and was able to connect to each of them and look at my current buckets and add / remove buckets. Is there something I should look for? I have my S3 application and clip setting, for example,

 development: bucket: (unique name) access_key_id: ENV['S3_KEY'] secret_access_key: ENV['S3_SECRET'] test: bucket: (unique name) access_key_id: ENV['S3_KEY'] secret_access_key: ENV['S3_SECRET'] production: bucket: (unique_name) access_key_id: ENV['S3_KEY'] secret_access_key: ENV['S3_SECRET'] has_attached_file :cover, :styles => { :thumb => "50x50" }, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => ":class/:id/:style/:filename" 

EDIT NOTE: ENV ['S3_KEY'] and ENV ['S3_SECRET'] are environment variables in heroku that I have tried even using my keys directly, and it still does not work.

Note. I just added bits (unique name), in fact they are not. I also checked the names of the bucket, but I don’t even think it is that far. I also set up environment settings for heroku correctly and set them to dev

+4
source share
3 answers

You do not install a bucket. This is in your s3.yml file, but you do not read this value from your call until has_attached_file .

Paperclip S3 docs: http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method

Also pay attention to those people who tell you not to use the s3.yml file with Heroku. This is a waste and just an added abstraction that doesn't buy anything. You already have ENV configured with the values ​​you need, so use them.

I did this before when I don't want to push the s3.yml file on Heroku, but I want to use it for testing and development. In the initializer, you can do something like this:

 # If an s3.yml file exists, use the key, secret key, and bucket values from there. # Otherwise, pull them from the environment. if File.exists?("#{Rails.root}/config/s3.yml") s3_config = YAML.load_file("#{Rails.root}/config/s3.yml") S3[:key] = s3_config[Rails.env]['key'] S3[:secret] = s3_config[Rails.env]['secret'] S3[:bucket] = s3_config[Rails.env]['bucket'] else S3[:key] = ENV['S3_KEY'] S3[:secret] = ENV['S3_SECRET'] S3[:bucket] = ENV['S3_BUCKET'] end 

Then, when you customize Paperclip in your model, you refer to a value similar to this:

 ... :s3_credentials => { :access_key_id => S3[:key], :secret_access_key => S3[:secret] }, :bucket => S3[:bucket] 

Obviously, this means that you do not want your s3.yml file in your git repository (which in fact, you should not anyway).

+3
source

I kept getting the same AWS::S3::InvalidAccessKeyId and had a very similar s3.yml file. As x1a4 recommended, I used ERB in my yaml file and it worked. Here's what it looks like now:

 # myapp/config/s3.yml development: &DEFAULTS bucket: myapp_dev access_key_id: <%= ENV['S3_KEY'] %> secret_access_key: <%= ENV['S3_SECRET'] %> test: <<: *DEFAULTS bucket: myapp_test production: <<: *DEFAULTS bucket: myapp staging: <<: *DEFAULTS bucket: myapp_staging 

I suppose this may be too indirect for some people, but it seemed to me that this is the purest implementation.

+3
source

Your s3 yaml file actually uses the lines ENV['S3_KEY'] and ENV['S3_SECRET'] as auth information for s3. They are not rated as ruby ​​code.

There are a few things, at least that you can do outside of putting this factual information in a yaml file. You can explore the possibility of including ERB in your yaml configurations, or simply not use the yaml file for your credentials at all, because you always pull the environment in each of your rails_envs, so the yaml file is just an additional layer of indirection in your case, which is useless.

+1
source

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


All Articles