Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active September 27, 2020 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/59ff72b7beeb2724a0979247d0fe7541 to your computer and use it in GitHub Desktop.
Save ericboehs/59ff72b7beeb2724a0979247d0fe7541 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# This file lives in lib/active_storage/service/s3_directory_service.rb.
require 'aws-sdk-s3'
require 'active_storage/service/s3_service'
require 'active_support/core_ext/numeric/bytes'
module ActiveStorage
class Service
class S3DirectoryService < S3Service
def proxy_url(url)
return url unless ENV['ATTACHMENT_HOST']
URI.join(ENV['ATTACHMENT_HOST'], URI(url).path).to_s
end
def url(key, **)
instrument :url, key: key do |payload|
payload[:url] = proxy_url object_for(key).public_url
end
end
end
end
end
# This file lives in config/storage.yml.
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
amazon:
service: S3Directory
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
region: <%= ENV['AWS_S3_REGION'] %>
bucket: <%= ENV['AWS_BUCKET'] %>
upload:
cache_control: 'public, max-age=31536000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment