2017-07-02 10:47:28 -04:00
|
|
|
require "aws-sdk"
|
2017-07-06 09:41:22 -04:00
|
|
|
require "active_support/core_ext/numeric/bytes"
|
2017-07-02 10:47:28 -04:00
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
class ActiveStorage::Service::S3Service < ActiveStorage::Service
|
2017-07-02 10:47:28 -04:00
|
|
|
attr_reader :client, :bucket
|
|
|
|
|
2017-07-16 19:17:47 -04:00
|
|
|
def initialize(access_key_id:, secret_access_key:, region:, bucket:, **options)
|
|
|
|
@client = Aws::S3::Resource.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region, **options)
|
2017-07-12 02:44:08 -04:00
|
|
|
@bucket = @client.bucket(bucket)
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 10:01:11 -04:00
|
|
|
def upload(key, io, checksum: nil)
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :upload, key, checksum: checksum do
|
|
|
|
begin
|
2017-07-12 02:44:08 -04:00
|
|
|
object_for(key).put(body: io, content_md5: checksum)
|
2017-07-09 11:04:28 -04:00
|
|
|
rescue Aws::S3::Errors::BadDigest
|
|
|
|
raise ActiveStorage::IntegrityError
|
|
|
|
end
|
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
|
2017-07-12 02:44:08 -04:00
|
|
|
def download(key)
|
2017-07-02 10:47:28 -04:00
|
|
|
if block_given?
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :streaming_download, key do
|
2017-07-12 02:44:08 -04:00
|
|
|
stream(key, &block)
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
else
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :download, key do
|
2017-07-12 02:44:08 -04:00
|
|
|
object_for(key).get.body.read.force_encoding(Encoding::BINARY)
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :delete, key do
|
2017-07-12 02:44:08 -04:00
|
|
|
object_for(key).delete
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
|
2017-07-03 15:08:36 -04:00
|
|
|
def exist?(key)
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :exist, key do |payload|
|
2017-07-12 02:44:08 -04:00
|
|
|
answer = object_for(key).exists?
|
|
|
|
payload[:exist] = answer
|
|
|
|
answer
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
|
2017-07-04 11:34:37 -04:00
|
|
|
def url(key, expires_in:, disposition:, filename:)
|
2017-07-09 11:04:28 -04:00
|
|
|
instrument :url, key do |payload|
|
2017-07-12 02:44:08 -04:00
|
|
|
generated_url = object_for(key).presigned_url :get, expires_in: expires_in,
|
2017-07-09 11:04:28 -04:00
|
|
|
response_content_disposition: "#{disposition}; filename=\"#{filename}\""
|
2017-07-13 18:09:56 -04:00
|
|
|
|
2017-07-12 02:44:08 -04:00
|
|
|
payload[:url] = generated_url
|
2017-07-13 18:09:56 -04:00
|
|
|
|
2017-07-12 02:44:08 -04:00
|
|
|
generated_url
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-07-03 17:19:51 -04:00
|
|
|
end
|
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
def url_for_direct_upload(key, expires_in:, content_type:, content_length:)
|
|
|
|
instrument :url, key do |payload|
|
2017-07-12 02:44:08 -04:00
|
|
|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in,
|
2017-07-09 12:03:13 -04:00
|
|
|
content_type: content_type, content_length: content_length
|
2017-07-13 18:09:56 -04:00
|
|
|
|
2017-07-12 02:44:08 -04:00
|
|
|
payload[:url] = generated_url
|
2017-07-13 18:09:56 -04:00
|
|
|
|
2017-07-12 02:44:08 -04:00
|
|
|
generated_url
|
2017-07-09 12:03:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-02 10:47:28 -04:00
|
|
|
private
|
2017-07-12 02:44:08 -04:00
|
|
|
def object_for(key)
|
|
|
|
bucket.object(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reads the object for the given key in chunks, yielding each to the block.
|
|
|
|
def stream(key, options = {}, &block)
|
|
|
|
object = object_for(key)
|
|
|
|
|
|
|
|
chunk_size = 5.megabytes
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while offset < object.content_length
|
|
|
|
yield object.read(options.merge(range: "bytes=#{offset}-#{offset + chunk_size - 1}"))
|
|
|
|
offset += chunk_size
|
|
|
|
end
|
2017-07-02 10:47:28 -04:00
|
|
|
end
|
|
|
|
end
|