Class: ActiveStorage::Service::S3Service
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::S3Service
- Defined in:
- activestorage/lib/active_storage/service/s3_service.rb
Overview
Active Storage S3 Service
Wraps the Amazon Simple Storage Service (S3) as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#multipart_upload_threshold ⇒ Object
readonly
Returns the value of attribute multipart_upload_threshold.
-
#upload_options ⇒ Object
readonly
Returns the value of attribute upload_options.
Attributes inherited from ActiveStorage::Service
Instance Method Summary collapse
- #compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key, &block) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}) ⇒ Object
-
#initialize(bucket:, upload: {}, public: false, **options) ⇒ S3Service
constructor
A new instance of S3Service.
- #upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
Methods inherited from ActiveStorage::Service
build, configure, #open, #public?, #update_metadata, #url
Methods included from ActiveSupport::Autoload
#autoload, #autoload_at, #autoload_under, #eager_autoload, #eager_load!, extended
Constructor Details
#initialize(bucket:, upload: {}, public: false, **options) ⇒ S3Service
Returns a new instance of S3Service.
17 18 19 20 21 22 23 24 25 26 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 17 def initialize(bucket:, upload: {}, public: false, **) @client = Aws::S3::Resource.new(**) @bucket = @client.bucket(bucket) @multipart_upload_threshold = upload.delete(:multipart_threshold) || 100.megabytes @public = public @upload_options = upload @upload_options[:acl] = "public-read" if public? end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket
14 15 16 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 14 def bucket @bucket end |
#client ⇒ Object (readonly)
Returns the value of attribute client
14 15 16 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 14 def client @client end |
#multipart_upload_threshold ⇒ Object (readonly)
Returns the value of attribute multipart_upload_threshold
15 16 17 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 15 def multipart_upload_threshold @multipart_upload_threshold end |
#upload_options ⇒ Object (readonly)
Returns the value of attribute upload_options
15 16 17 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 15 def @upload_options end |
Instance Method Details
#compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 100 def compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) content_disposition = content_disposition_with(type: disposition, filename: filename) if disposition && filename object_for(destination_key).upload_stream( content_type: content_type, content_disposition: content_disposition, part_size: MINIMUM_UPLOAD_PART_SIZE, metadata: , ** ) do |out| source_keys.each do |source_key| stream(source_key) do |chunk| IO.copy_stream(StringIO.new(chunk), out) end end end end |
#delete(key) ⇒ Object
62 63 64 65 66 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 62 def delete(key) instrument :delete, key: key do object_for(key).delete end end |
#delete_prefixed(prefix) ⇒ Object
68 69 70 71 72 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 68 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do bucket.objects(prefix: prefix).batch_delete! end end |
#download(key, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 40 def download(key, &block) if block_given? instrument :streaming_download, key: key do stream(key, &block) end else instrument :download, key: key do object_for(key).get.body.string.force_encoding(Encoding::BINARY) rescue Aws::S3::Errors::NoSuchKey raise ActiveStorage::FileNotFoundError end end end |
#download_chunk(key, range) ⇒ Object
54 55 56 57 58 59 60 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 54 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do object_for(key).get(range: "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}").body.string.force_encoding(Encoding::BINARY) rescue Aws::S3::Errors::NoSuchKey raise ActiveStorage::FileNotFoundError end end |
#exist?(key) ⇒ Boolean
74 75 76 77 78 79 80 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 74 def exist?(key) instrument :exist, key: key do |payload| answer = object_for(key).exists? payload[:exist] = answer answer end end |
#headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}) ⇒ Object
94 95 96 97 98 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 94 def headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}, **) content_disposition = content_disposition_with(type: disposition, filename: filename) if filename { "Content-Type" => content_type, "Content-MD5" => checksum, "Content-Disposition" => content_disposition, **() } end |
#upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 28 def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}, **) instrument :upload, key: key, checksum: checksum do content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename if io.size < multipart_upload_threshold upload_with_single_part key, io, checksum: checksum, content_type: content_type, content_disposition: content_disposition, custom_metadata: else upload_with_multipart key, io, content_type: content_type, content_disposition: content_disposition, custom_metadata: end end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 82 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) instrument :url, key: key do |payload| generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i, content_type: content_type, content_length: content_length, content_md5: checksum, metadata: , whitelist_headers: ["content-length"], ** payload[:url] = generated_url generated_url end end |