ruby-on-railsamazon-s3paperclipfog

where to define name of s3 bucket for different env while using fog gem


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

Solution

  • 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)