Class: ActiveStorage::Service::S3Service

Inherits:
ActiveStorage::Service show all
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

Attributes inherited from ActiveStorage::Service

#name

Instance Method Summary collapse

Methods inherited from ActiveStorage::Service

build, configure, #inspect, #open, #public?, #update_metadata, #url

Methods included from ActiveSupport::Autoload

#autoload, #autoload_at, #autoload_under, #eager_autoload, #eager_load!

Constructor Details

#initialize(bucket:, upload: {}, public: false, default_digest_type: :md5, **options) ⇒ S3Service

Returns a new instance of S3Service.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 17

def initialize(bucket:, upload: {}, public: false, default_digest_type: :md5, **options)
  @client = Aws::S3::Resource.new(**options)
  @transfer_manager = Aws::S3::TransferManager.new(client: @client.client) if defined?(Aws::S3::TransferManager)
  @bucket = @client.bucket(bucket)

  @default_digest_type = default_digest_type.downcase.to_sym
  @default_digest_class = digest_to_class(@default_digest_type)

  @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

#bucketObject (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

#clientObject (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

#default_digest_classObject (readonly)

Returns the value of attribute default_digest_class.



14
15
16
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 14

def default_digest_class
  @default_digest_class
end

#default_digest_typeObject (readonly)

Returns the value of attribute default_digest_type.



14
15
16
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 14

def default_digest_type
  @default_digest_type
end

#multipart_upload_thresholdObject (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_optionsObject (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
  @upload_options
end

Instance Method Details

#checksum_implementation(check_digest_type: nil) ⇒ Object



127
128
129
130
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 127

def checksum_implementation(check_digest_type: nil, **)
  return default_digest_class unless check_digest_type && check_digest_type != default_digest_type
  digest_to_class(check_digest_type)
end

#compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 104

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

  upload_stream(
    key: destination_key,
    content_type: content_type,
    content_disposition: content_disposition,
    part_size: MINIMUM_UPLOAD_PART_SIZE,
    metadata: ,
    **upload_options
  ) do |out|
    source_keys.each do |source_key|
      stream(source_key) do |chunk|
        IO.copy_stream(StringIO.new(chunk), out)
      end
    end
  end
end

#compute_checksum(file, **options) ⇒ Object



123
124
125
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 123

def compute_checksum(file, **options)
  default_digest_type == :md5 ? super : "#{default_digest_type}:#{super}"
end

#delete(key) ⇒ Object



66
67
68
69
70
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 66

def delete(key)
  instrument :delete, key: key do
    object_for(key).delete
  end
end

#delete_prefixed(prefix) ⇒ Object



72
73
74
75
76
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 72

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    bucket.objects(prefix: prefix).batch_delete!
  end
end

#download(key, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 44

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



58
59
60
61
62
63
64
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 58

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

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 78

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



98
99
100
101
102
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 98

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, **s3_http_headers_for_direct_upload(checksum), "Content-Disposition" => content_disposition, **() }
end

#upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 32

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



86
87
88
89
90
91
92
93
94
95
96
# File 'activestorage/lib/active_storage/service/s3_service.rb', line 86

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, **s3_sdk_upload_params(checksum),
      metadata: , whitelist_headers: ["content-length"], **upload_options

    payload[:url] = generated_url

    generated_url
  end
end