1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/active_storage/service/s3_service.rb

88 lines
2.3 KiB
Ruby
Raw Normal View History

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
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)
@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
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
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
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
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
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|
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
def url(key, expires_in:, disposition:, filename:)
2017-07-09 11:04:28 -04:00
instrument :url, key do |payload|
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}\""
payload[:url] = generated_url
generated_url
2017-07-09 11:04:28 -04:00
end
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|
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
payload[:url] = generated_url
generated_url
2017-07-09 12:03:13 -04:00
end
end
2017-07-02 10:47:28 -04:00
private
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