How to set up AWS credentials in rails for development?

I'm trying to figure out what is the best way to set up my credentials during development? I can set up production credentials due to heroku configuration variables ... but you need help if you are just testing the application locally.

I have it in my development.rb

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['BUCKET_NAME'],
    :access_key_id => ENV['ACCESS_KEY_ID'],
    :secret_access_key => ENV['SECRET_ACCESS_KEY']
  }
}  

But I'm not sure how to reference these ENV variables?

I created this aws.ymlfile in my / config folder

development:
  BUCKET_NAME: "somename"
  ACCESS_KEY_ID: "4205823951412980"
  SECRET_ACCESS_KEY: "123141ABNCEFEHUDSL2309489850"

I thought that if I matched the name ENV, would this work? This is probably not the case ...

I included aws.ymlin my .ignorefile

/config/aws.yml
+4
source share
4 answers

dotenv gem. gem 'dotenv-rails', :groups => [:development, :test], `.env ' .

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
+4

config.yml ./config.

./config/application.rb ,

config.before_initialize do
  dev = File.join(Rails.root, 'config', 'config.yml')
  YAML.load(File.open(dev)).each do |key,value|
    ENV[key.to_s] = value
  end if File.exists?(dev)
end

./config/config.yml

BUCKET_NAME: "somename"
ACCESS_KEY_ID: "4205823951412980"
SECRET_ACCESS_KEY: "123141ABNCEFEHUDSL2309489850"

# config/application.rb

#config/application.rb
require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

    module Appname
      class Application < Rails::Application
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.

        # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
        # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
        # config.time_zone = 'Central Time (US & Canada)'

        # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
        # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
        # config.i18n.default_locale = :de

        # Do not swallow errors in after_commit/after_rollback callbacks.
        config.active_record.raise_in_transactional_callbacks = true

        initializer 'setup_asset_pipeline', :group => :all  do |app|
          # We don't want the default of everything that isn't js or css, because it pulls too many things in
          app.config.assets.precompile.shift

          # Explicitly register the extensions we are interested in compiling
          app.config.assets.precompile.push(Proc.new do |path|
            File.extname(path).in? [
              '.html', '.erb', '.haml',                 # Templates
              '.png',  '.gif', '.jpg', '.jpeg',         # Images
              '.eot',  '.otf', '.svc', '.woff', '.ttf', # Fonts
            ]
          end)
        end

        I18n.enforce_available_locales = false

        config.before_initialize do
          dev = File.join(Rails.root, 'config', 'config.yml')
          YAML.load(File.open(dev)).each do |key,value|
            ENV[key.to_s] = value
          end if File.exists?(dev)
        end
      end
    end
+1

Foreman .ENV. Heroku . :

config/initiliazers/aws.rb

require 'aws-sdk'
require 'aws-sdk-resources'

# Rails.configuration.aws is used by AWS, Paperclip, and S3DirectUpload
Rails.configuration.aws = YAML.load(ERB.new(File.read("#{Rails.root}/config/aws.yml")).result)[Rails.env].symbolize_keys!
AWS.config(logger: Rails.logger)
AWS.config(Rails.configuration.aws)

config/initializers/paperclip.rb

# https://devcenter.heroku.com/articles/paperclip-s3
if Rails.env.test?
  Paperclip::Attachment.default_options.merge!(
    path: ":rails_root/public/paperclip/:rails_env/:class/:attachment/:id_partition/:filename",
    storage: :filesystem
  )
else
  Paperclip::Attachment.default_options.merge!(
    url: ':s3_domain_url',
    path: '/:class/:filename',
    storage: :s3,
    s3_credentials: Rails.configuration.aws,
    s3_protocol: 'https'
  )
end

config/aws.yml

defaults: &defaults
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
test:
  <<: *defaults
  bucket: <%= ENV['AWS_TEST_BUCKET'] %>
development:
  <<: *defaults
  bucket: <%= ENV['AWS_TEST_BUCKET'] %>
production:
  <<: *defaults
  bucket: <%= ENV['AWS_BUCKET'] %>
+1

If all else fails, you can simply start the rails application with command line options. It’s good to know the base case, where you know for sure that your parameters are really passed to your application:

ACCESS_KEY_ID = ABCDEFG SECRET_ACCESS_KEY = secreTaccessKey BUCKET_NAME = dev-images rails s

I prefer to set an alias in env:

alias railss = 'ACCESS_KEY_ID = ABCDEFG SECRET_ACCESS_KEY = secreTaccessKey BUCKET_NAME = dev-images rails s'

0
source

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


All Articles