سؤال

I am uploading my paperclip attachment to s3 because in my model I am using fog credentials like this... The problem is I need to have different bucket names for different env, by defining the bucket in model it gets specified for every env, so where else can I define it?

has_attached_file :news_logo,
  :storage => :fog,
  :fog_credentials => "#{Rails.root}/config/s3.yml",
  :fog_directory => "s3-bucket-name"

config/s3.yml

development:
  provider: AWS
  aws_access_key_id: xyz
  aws_secret_access_key: xyz
  path_style: true
هل كانت مفيدة؟

المحلول

You can use Rails.env in order to customize your bucket name, like:

has_attached_file :news_logo,
  :storage => :fog,
  :fog_credentials => "#{Rails.root}/config/s3.yml",
  :fog_directory => "s3-bucket-name-#{Rails.env}"

you can also do something like:

has_attached_file :news_logo,
  :storage => :fog,
  :fog_credentials => "#{Rails.root}/config/s3.yml",
  :fog_directory => (case Rails.env
                       when 'production' then 'my-production-bucket'
                       when 'testing' then 'testing-bucket'
                       else 'this-is-development-bucket';
                     end)

نصائح أخرى

Modify the code as follows

has_attached_file :news_logo,
  :storage => :fog,
  :fog_credentials => "#{Rails.root}/config/s3.yml",
  :fog_directory => S3_BUCKET[Rails.env]

and define the bucket names in the constant file.

config/initializers/constants.rb

S3_BUCKET = {
  'development' => 's3-bucket-name-development',
  'staging' => 's3-bucket-name-staging',
  'production' => 's3-bucket-name-production'
}.freeze
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top