Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.