diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
commit | 571509ad12bf3bcb3190efd7494a38c4796302b8 (patch) | |
tree | 083bcf7f1a56e6856c486de7accef2d3778826e7 /lib/active_vault/site/s3_site.rb | |
parent | e635dac88f0dfcc36a2313c10f860cb6e3a52cfa (diff) | |
download | rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.gz rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.bz2 rails-571509ad12bf3bcb3190efd7494a38c4796302b8.zip |
Rename from ActiveFile to ActiveVault since activefile gem name was taken
Diffstat (limited to 'lib/active_vault/site/s3_site.rb')
-rw-r--r-- | lib/active_vault/site/s3_site.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/active_vault/site/s3_site.rb b/lib/active_vault/site/s3_site.rb new file mode 100644 index 0000000000..49a7522170 --- /dev/null +++ b/lib/active_vault/site/s3_site.rb @@ -0,0 +1,63 @@ +require "aws-sdk" + +class ActiveVault::Site::S3Site < ActiveVault::Site + attr_reader :client, :bucket + + def initialize(access_key_id:, secret_access_key:, region:, bucket:) + @client = Aws::S3::Resource.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region) + @bucket = @client.bucket(bucket) + end + + def upload(key, io) + object_for(key).put(body: io) + end + + def download(key) + if block_given? + stream(key, &block) + else + object_for(key).get.body.read + end + end + + def delete(key) + object_for(key).delete + end + + def exist?(key) + object_for(key).exists? + end + + + def url(key, expires_in:, disposition:, filename:) + object_for(key).presigned_url :get, expires_in: expires_in, + response_content_disposition: "#{disposition}; filename=\"#{filename}\"" + end + + def byte_size(key) + object_for(key).size + end + + def checksum(key) + object_for(key).etag.remove(/"/) + end + + + 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 = 5242880 # 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 + end +end |