Skip to content

Commit 8739cf7

Browse files
committed
Update classes to use S3
This updates the base carrierwave config, plus the two image upload classes, to start to use AWS S3. We're doing this to replace our old SFTP server
1 parent 2a1899f commit 8739cf7

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

app/uploaders/avatar_uploader.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# encoding: utf-8
22

33
class AvatarUploader < CarrierWave::Uploader::Base
4-
storage :sftp if Rails.env.production?
4+
storage :s3 if Rails.env.production?
55

66
include CarrierWave::MiniMagick
77

8+
def content_type_allowlist
9+
[/image\//]
10+
end
11+
812
# Override the directory where uploaded files will be stored.
913
# This is a sensible default for uploaders that are meant to be mounted:
1014
def store_dir
11-
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
15+
"uploads/#{model.class.to_s.underscore}/#{model.id}"
1216
end
1317

1418
# Provide a default URL as a default if there hasn't been a file uploaded:

app/uploaders/image_uploader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class ImageUploader < CarrierWave::Uploader::Base
2-
storage :sftp if Rails.env.production?
2+
storage :s3 if Rails.env.production?
33

44
# Include RMagick or MiniMagick support:
55
include CarrierWave::MiniMagick

config/initializers/carrier_wave.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
if Rails.env.development?
55
config.storage = :file
66
elsif Rails.env.production?
7-
config.cache_dir = "#{Rails.root}/tmp/uploads"
8-
config.root = Rails.root.join('tmp')
9-
config.sftp_host = ENV['UPLOADER_ASSET_HOST']
10-
config.sftp_user = ENV['UPLOADER_USER']
11-
config.sftp_folder = ENV['UPLOADER_FOLDER']
12-
config.sftp_url = ENV['UPLOADER_URL']
13-
config.sftp_options = {
14-
password: ENV['UPLOADER_PASSW'],
15-
port: 22
7+
config.storage = :aws
8+
config.aws_bucket = ENV.fetch('S3_BUCKET_NAME', 'prod-sponsor-logos')
9+
config.aws_acl = 'public-read'
10+
config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
11+
config.aws_credentials = {
12+
access_key_id: ENV.fetch('AWS_ACCESS_KEY'),
13+
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
14+
region: ENV.fetch('AWS_REGION', 'eu-north-1') # Required
1615
}
1716
end
1817
end

0 commit comments

Comments
 (0)