Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pre-signed URLs for media storage when using S3 #9

Merged
merged 5 commits into from
Feb 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/paperclip/storage_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ def copy_to_local_file(style, local_dest_path)
end
end
end

Paperclip::Storage::S3.prepend(Paperclip::Storage::S3Extensions)
end

module Paperclip
module Storage
module S3SignedExtensions
def url(style_name = default_style)
time = 3600
if path(style_name)
base_options = { expires_in: time }
s3_url = ENV['S3_BUCKET'] + '.s3.' + ENV['S3_REGION'] + '.amazonaws.com'
s3_object(style_name).presigned_url(
:get,
base_options.merge(s3_url_options)
).to_s&.gsub(s3_url, ENV['S3_ALIAS_HOST'])
else
super
end
end
end
end
end

Paperclip::Storage::S3.prepend(Paperclip::Storage::S3SignedExtensions)