Class: ActiveStorage::Service::S3Service
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::S3Service
- Defined in:
- lib/active_storage/service/s3_service.rb
Overview
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
- #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) ⇒ 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) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Methods inherited from ActiveStorage::Service
build, configure, #open, #public?, #update_metadata, #url
Constructor Details
#initialize(bucket:, upload: {}, public: false, **options) ⇒ S3Service
Returns a new instance of S3Service.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_storage/service/s3_service.rb', line 15 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.
12 13 14 |
# File 'lib/active_storage/service/s3_service.rb', line 12 def bucket @bucket end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/active_storage/service/s3_service.rb', line 12 def client @client end |
#multipart_upload_threshold ⇒ Object (readonly)
Returns the value of attribute multipart_upload_threshold.
13 14 15 |
# File 'lib/active_storage/service/s3_service.rb', line 13 def multipart_upload_threshold @multipart_upload_threshold end |
#upload_options ⇒ Object (readonly)
Returns the value of attribute upload_options.
13 14 15 |
# File 'lib/active_storage/service/s3_service.rb', line 13 def @upload_options end |
Instance Method Details
#delete(key) ⇒ Object
60 61 62 63 64 |
# File 'lib/active_storage/service/s3_service.rb', line 60 def delete(key) instrument :delete, key: key do object_for(key).delete end end |
#delete_prefixed(prefix) ⇒ Object
66 67 68 69 70 |
# File 'lib/active_storage/service/s3_service.rb', line 66 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do bucket.objects(prefix: prefix).batch_delete! end end |
#download(key, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_storage/service/s3_service.rb', line 38 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
52 53 54 55 56 57 58 |
# File 'lib/active_storage/service/s3_service.rb', line 52 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
72 73 74 75 76 77 78 |
# File 'lib/active_storage/service/s3_service.rb', line 72 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) ⇒ Object
92 93 94 95 96 |
# File 'lib/active_storage/service/s3_service.rb', line 92 def headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, **) 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) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_storage/service/s3_service.rb', line 26 def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **) 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 else upload_with_multipart key, io, content_type: content_type, content_disposition: content_disposition end end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_storage/service/s3_service.rb', line 80 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) 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, whitelist_headers: ["content-length"], ** payload[:url] = generated_url generated_url end end |