Skip to content

Commit

Permalink
Make uploads behavior parametrizable via env variables
Browse files Browse the repository at this point in the history
- UPLOADS_LOCATION: "local" means use a local uploads folder, "s3" means
  use an AWS S3 bucket.
- new secret to configure AWS region to use, in case S3 is used for
  uploads.
- if S3 is used for uploads, AWS_S3_BUCKET env variable sets the bucket
  name ("feedbunch-<environment>" by default, e.g.
"feedbunch-production" for the production environment.
  • Loading branch information
amatriain committed Jan 6, 2021
1 parent 0f901b7 commit 152d40b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
19 changes: 16 additions & 3 deletions config/environments/production.rb
Expand Up @@ -47,7 +47,7 @@
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# The FORCE_SECURE can be used to enable or disable this behavior (enabled by default)
# The FORCE_SECURE environment variable can be used to enable or disable this behavior (enabled by default)
force_secure_str = ENV.fetch("FORCE_SECURE") { "true" }
force_secure_str = force_secure_str.downcase.strip
force_secure = ActiveRecord::Type::Boolean.new.cast force_secure_str
Expand Down Expand Up @@ -131,8 +131,21 @@
enable_starttls_auto: true
}

# Upload files to Amazon S3
Feedbunch::Application.config.uploads_manager = S3Client
# UPLOADS_LOCATION enviroment variable controls the location for file uploads and downloads.
# The options here are "s3" (the default), which stores files in the cloud (AWS S3)
# and "local", which stores files locally in an uploads folder.
uploads_location = ENV.fetch("UPLOADS_LOCATION") { "s3" }
uploads_location = uploads_location.downcase.strip

case uploads_location
when "s3"
Feedbunch::Application.config.uploads_manager = S3Client
when "local"
Feedbunch::Application.config.uploads_manager = FileClient
else
# if an unrecognized value is passed in UPLOADS_LOCATION, use AWS S3 by default
Feedbunch::Application.config.uploads_manager = S3Client
end

# Use sidekiq as backend for ActiveJob jobs
config.active_job.queue_adapter = :sidekiq
Expand Down
9 changes: 6 additions & 3 deletions config/initializers/aws-sdk.rb
Expand Up @@ -9,7 +9,10 @@
access_key = Rails.application.secrets.aws_access_key_id
secret = Rails.application.secrets.aws_secret_access_key
Aws.config[:credentials] = Aws::Credentials.new access_key, secret
Aws.config[:region] = 'eu-west-1'
Aws.config[:region] = Rails.application.secrets.aws_region

# Name of the S3 bucket for storing objects. Different for each environment.
Feedbunch::Application.config.s3_bucket = "feedbunch-#{Rails.env}"
# Name of the S3 bucket for storing objects. Can be changed with the AWS_S3_BUCKET
# environment variable, by default takes the value "feedbunch-<environment>",
# e.g. "feedbunch-production" in the production environment
s3_bucket = ENV.fetch("AWS_S3_BUCKET") { "feedbunch-#{Rails.env}" }
Feedbunch::Application.config.s3_bucket = s3_bucket
5 changes: 5 additions & 0 deletions config/secrets_ci.yml
Expand Up @@ -3,6 +3,7 @@ development:
devise_secret_key: '--'
aws_access_key_id: '--'
aws_secret_access_key: '--'
aws_region: '--'
smtp_address: '--'
smtp_port: '--'
smtp_user_name: '--'
Expand All @@ -16,6 +17,7 @@ test:
devise_secret_key: '--'
aws_access_key_id: '--'
aws_secret_access_key: '--'
aws_region: '--'
smtp_address: '--'
smtp_port: '--'
smtp_user_name: '--'
Expand All @@ -29,6 +31,7 @@ production:
devise_secret_key: '--'
aws_access_key_id: '--'
aws_secret_access_key: '--'
aws_region: '--'
smtp_address: '--'
smtp_port: '--'
smtp_user_name: '--'
Expand All @@ -42,6 +45,7 @@ staging:
devise_secret_key: '--'
aws_access_key_id: '--'
aws_secret_access_key: '--'
aws_region: '--'
smtp_address: '--'
smtp_port: '--'
smtp_user_name: '--'
Expand All @@ -55,6 +59,7 @@ ci:
devise_secret_key: '25d1372ab5fb586daf85e173b3acba11de3c299650db2c474bfb6fffcf16fa466831962309843d9f5b8f943cabbaf897e6ab374efba2eef6983b9a71c4fa78b7'
aws_access_key_id: '--'
aws_secret_access_key: '--'
aws_region: '--'
smtp_address: '--'
smtp_port: '--'
smtp_user_name: '--'
Expand Down

0 comments on commit 152d40b

Please sign in to comment.