aws-sdk-s3 1.182.0 → 1.199.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +116 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-sdk-s3/bucket.rb +46 -6
  5. data/lib/aws-sdk-s3/client.rb +1693 -591
  6. data/lib/aws-sdk-s3/client_api.rb +396 -162
  7. data/lib/aws-sdk-s3/customizations/object.rb +63 -76
  8. data/lib/aws-sdk-s3/customizations.rb +2 -1
  9. data/lib/aws-sdk-s3/endpoint_provider.rb +234 -129
  10. data/lib/aws-sdk-s3/endpoints.rb +84 -0
  11. data/lib/aws-sdk-s3/errors.rb +11 -0
  12. data/lib/aws-sdk-s3/file_downloader.rb +65 -82
  13. data/lib/aws-sdk-s3/file_uploader.rb +3 -5
  14. data/lib/aws-sdk-s3/legacy_signer.rb +2 -1
  15. data/lib/aws-sdk-s3/multipart_download_error.rb +8 -0
  16. data/lib/aws-sdk-s3/multipart_file_uploader.rb +34 -65
  17. data/lib/aws-sdk-s3/multipart_stream_uploader.rb +80 -88
  18. data/lib/aws-sdk-s3/multipart_upload.rb +4 -2
  19. data/lib/aws-sdk-s3/multipart_upload_error.rb +3 -4
  20. data/lib/aws-sdk-s3/object.rb +85 -26
  21. data/lib/aws-sdk-s3/object_acl.rb +7 -1
  22. data/lib/aws-sdk-s3/object_multipart_copier.rb +2 -1
  23. data/lib/aws-sdk-s3/object_summary.rb +54 -24
  24. data/lib/aws-sdk-s3/plugins/streaming_retry.rb +5 -7
  25. data/lib/aws-sdk-s3/plugins/url_encoded_keys.rb +2 -1
  26. data/lib/aws-sdk-s3/resource.rb +6 -0
  27. data/lib/aws-sdk-s3/transfer_manager.rb +252 -0
  28. data/lib/aws-sdk-s3/types.rb +1626 -294
  29. data/lib/aws-sdk-s3.rb +1 -1
  30. data/sig/bucket.rbs +12 -3
  31. data/sig/client.rbs +143 -31
  32. data/sig/errors.rbs +2 -0
  33. data/sig/multipart_upload.rbs +1 -1
  34. data/sig/object.rbs +13 -10
  35. data/sig/object_summary.rbs +9 -9
  36. data/sig/resource.rbs +8 -1
  37. data/sig/types.rbs +183 -29
  38. metadata +8 -9
@@ -0,0 +1,252 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module S3
5
+ # A high-level S3 transfer utility that provides enhanced upload and download
6
+ # capabilities with automatic multipart handling, progress tracking, and
7
+ # handling of large files. The following features are supported:
8
+ #
9
+ # * upload a file with multipart upload
10
+ # * upload a stream with multipart upload
11
+ # * download a S3 object with multipart download
12
+ # * track transfer progress by using progress listener
13
+ #
14
+ class TransferManager
15
+ # @param [Hash] options
16
+ # @option options [S3::Client] :client (S3::Client.new)
17
+ # The S3 client to use for {TransferManager} operations. If not provided, a new default client
18
+ # will be created automatically.
19
+ def initialize(options = {})
20
+ @client = options.delete(:client) || Client.new
21
+ end
22
+
23
+ # @return [S3::Client]
24
+ attr_reader :client
25
+
26
+ # Downloads a file in S3 to a path on disk.
27
+ #
28
+ # # small files (< 5MB) are downloaded in a single API call
29
+ # tm = TransferManager.new
30
+ # tm.download_file('/path/to/file', bucket: 'bucket', key: 'key')
31
+ #
32
+ # Files larger than 5MB are downloaded using multipart method:
33
+ #
34
+ # # large files are split into parts and the parts are downloaded in parallel
35
+ # tm.download_file('/path/to/large_file', bucket: 'bucket', key: 'key')
36
+ #
37
+ # You can provide a callback to monitor progress of the download:
38
+ #
39
+ # # bytes and part_sizes are each an array with 1 entry per part
40
+ # # part_sizes may not be known until the first bytes are retrieved
41
+ # progress = proc do |bytes, part_sizes, file_size|
42
+ # bytes.map.with_index do |b, i|
43
+ # puts "Part #{i + 1}: #{b} / #{part_sizes[i]}".join(' ') + "Total: #{100.0 * bytes.sum / file_size}%"
44
+ # end
45
+ # end
46
+ # tm.download_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
47
+ #
48
+ # @param [String, Pathname, File, Tempfile] destination
49
+ # Where to download the file to. This can either be a String or Pathname to the file, an open File object,
50
+ # or an open Tempfile object. If you pass an open File or Tempfile object, then you are responsible for
51
+ # closing it after the download completes. Download behavior varies by destination type:
52
+ #
53
+ # * **String/Pathname paths**: Downloads to a temporary file first, then atomically moves to the final
54
+ # destination. This prevents corruption of any existing file if the download fails.
55
+ # * **File/Tempfile objects**: Downloads directly to the file object without using temporary files.
56
+ # You are responsible for managing the file object's state and closing it after the download completes.
57
+ # If the download fails, the file object may contain partial data.
58
+ #
59
+ # @param [String] bucket
60
+ # The name of the S3 bucket to upload to.
61
+ #
62
+ # @param [String] key
63
+ # The object key name in S3 bucket.
64
+ #
65
+ # @param [Hash] options
66
+ # Additional options for {Client#get_object} and #{Client#head_object} may be provided.
67
+ #
68
+ # @option options [String] :mode ("auto") `"auto"`, `"single_request"` or `"get_range"`
69
+ #
70
+ # * `"auto"` mode is enabled by default, which performs `multipart_download`
71
+ # * `"single_request`" mode forces only 1 GET request is made in download
72
+ # * `"get_range"` mode requires `:chunk_size` parameter to configured in customizing each range size
73
+ #
74
+ # @option options [Integer] :chunk_size required in `"get_range"` mode.
75
+ #
76
+ # @option options [Integer] :thread_count (10) Customize threads used in the multipart download.
77
+ #
78
+ # @option options [String] :version_id The object version id used to retrieve the object.
79
+ #
80
+ # @see https://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectVersioning.html ObjectVersioning
81
+ #
82
+ # @option options [String] :checksum_mode ("ENABLED")
83
+ # When `"ENABLED"` and the object has a stored checksum, it will be used to validate the download and will
84
+ # raise an `Aws::Errors::ChecksumError` if checksum validation fails. You may provide a `on_checksum_validated`
85
+ # callback if you need to verify that validation occurred and which algorithm was used.
86
+ # To disable checksum validation, set `checksum_mode` to `"DISABLED"`.
87
+ #
88
+ # @option options [Callable] :on_checksum_validated
89
+ # Called each time a request's checksum is validated with the checksum algorithm and the
90
+ # response. For multipart downloads, this will be called for each part that is downloaded and validated.
91
+ #
92
+ # @option options [Proc] :progress_callback
93
+ # A Proc that will be called when each chunk of the download is received. It will be invoked with
94
+ # `bytes_read`, `part_sizes`, `file_size`. When the object is downloaded as parts (rather than by ranges),
95
+ # the `part_sizes` will not be known ahead of time and will be `nil` in the callback until the first bytes
96
+ # in the part are received.
97
+ #
98
+ # @raise [MultipartDownloadError] Raised when an object validation fails outside of service errors.
99
+ #
100
+ # @return [Boolean] Returns `true` when the file is downloaded without any errors.
101
+ #
102
+ # @see Client#get_object
103
+ # @see Client#head_object
104
+ def download_file(destination, bucket:, key:, **options)
105
+ downloader = FileDownloader.new(client: @client)
106
+ downloader.download(destination, options.merge(bucket: bucket, key: key))
107
+ true
108
+ end
109
+
110
+ # Uploads a file from disk to S3.
111
+ #
112
+ # # a small file are uploaded with PutObject API
113
+ # tm = TransferManager.new
114
+ # tm.upload_file('/path/to/small_file', bucket: 'bucket', key: 'key')
115
+ #
116
+ # Files larger than or equal to `:multipart_threshold` are uploaded using multipart upload APIs.
117
+ #
118
+ # # large files are automatically split into parts and the parts are uploaded in parallel
119
+ # tm.upload_file('/path/to/large_file', bucket: 'bucket', key: 'key')
120
+ #
121
+ # The response of the S3 upload API is yielded if a block given.
122
+ #
123
+ # # API response will have etag value of the file
124
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key') do |response|
125
+ # etag = response.etag
126
+ # end
127
+ #
128
+ # You can provide a callback to monitor progress of the upload:
129
+ #
130
+ # # bytes and totals are each an array with 1 entry per part
131
+ # progress = proc do |bytes, totals|
132
+ # bytes.map.with_index do |b, i|
133
+ # puts "Part #{i + 1}: #{b} / #{totals[i]} " + "Total: #{100.0 * bytes.sum / totals.sum}%"
134
+ # end
135
+ # end
136
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
137
+ #
138
+ # @param [String, Pathname, File, Tempfile] source
139
+ # A file on the local file system that will be uploaded. This can either be a `String` or `Pathname` to the
140
+ # file, an open `File` object, or an open `Tempfile` object. If you pass an open `File` or `Tempfile` object,
141
+ # then you are responsible for closing it after the upload completes. When using an open Tempfile, rewind it
142
+ # before uploading or else the object will be empty.
143
+ #
144
+ # @param [String] bucket
145
+ # The name of the S3 bucket to upload to.
146
+ #
147
+ # @param [String] key
148
+ # The object key name for the uploaded file.
149
+ #
150
+ # @param [Hash] options
151
+ # Additional options for {Client#put_object} when file sizes below the multipart threshold.
152
+ # For files larger than the multipart threshold, options for {Client#create_multipart_upload},
153
+ # {Client#complete_multipart_upload}, and {Client#upload_part} can be provided.
154
+ #
155
+ # @option options [Integer] :multipart_threshold (104857600)
156
+ # Files larger han or equal to `:multipart_threshold` are uploaded using the S3 multipart upload APIs.
157
+ # Default threshold is `100MB`.
158
+ #
159
+ # @option options [Integer] :thread_count (10)
160
+ # The number of parallel multipart uploads. This option is not used if the file is smaller than
161
+ # `:multipart_threshold`.
162
+ #
163
+ # @option options [Proc] :progress_callback (nil)
164
+ # A Proc that will be called when each chunk of the upload is sent.
165
+ # It will be invoked with `[bytes_read]` and `[total_sizes]`.
166
+ #
167
+ # @raise [MultipartUploadError] If an file is being uploaded in parts, and the upload can not be completed,
168
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that
169
+ # returns the failures that caused the upload to be aborted.
170
+ #
171
+ # @return [Boolean] Returns `true` when the file is uploaded without any errors.
172
+ #
173
+ # @see Client#put_object
174
+ # @see Client#create_multipart_upload
175
+ # @see Client#complete_multipart_upload
176
+ # @see Client#upload_part
177
+ def upload_file(source, bucket:, key:, **options)
178
+ uploading_options = options.dup
179
+ uploader = FileUploader.new(
180
+ multipart_threshold: uploading_options.delete(:multipart_threshold),
181
+ client: @client
182
+ )
183
+ response = uploader.upload(source, uploading_options.merge(bucket: bucket, key: key))
184
+ yield response if block_given?
185
+ true
186
+ end
187
+
188
+ # Uploads a stream in a streaming fashion to S3.
189
+ #
190
+ # Passed chunks automatically split into multipart upload parts and the parts are uploaded in parallel.
191
+ # This allows for streaming uploads that never touch the disk.
192
+ #
193
+ # **Note**: There are known issues in JRuby until jruby-9.1.15.0, so avoid using this with older JRuby versions.
194
+ #
195
+ # @example Streaming chunks of data
196
+ # tm = TransferManager.new
197
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
198
+ # 10.times { write_stream << 'foo' }
199
+ # end
200
+ # @example Streaming chunks of data
201
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
202
+ # IO.copy_stream(IO.popen('ls'), write_stream)
203
+ # end
204
+ # @example Streaming chunks of data
205
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
206
+ # IO.copy_stream(STDIN, write_stream)
207
+ # end
208
+ #
209
+ # @param [String] bucket
210
+ # The name of the S3 bucket to upload to.
211
+ #
212
+ # @param [String] key
213
+ # The object key name for the uploaded file.
214
+ #
215
+ # @param [Hash] options
216
+ # Additional options for {Client#create_multipart_upload}, {Client#complete_multipart_upload}, and
217
+ # {Client#upload_part} can be provided.
218
+ #
219
+ # @option options [Integer] :thread_count (10)
220
+ # The number of parallel multipart uploads.
221
+ #
222
+ # @option options [Boolean] :tempfile (false)
223
+ # Normally read data is stored in memory when building the parts in order to complete the underlying
224
+ # multipart upload. By passing `:tempfile => true`, the data read will be temporarily stored on disk reducing
225
+ # the memory footprint vastly.
226
+ #
227
+ # @option options [Integer] :part_size (5242880)
228
+ # Define how big each part size but the last should be. Default `:part_size` is `5 * 1024 * 1024`.
229
+ #
230
+ # @raise [MultipartUploadError] If an object is being uploaded in parts, and the upload can not be completed,
231
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that returns
232
+ # the failures that caused the upload to be aborted.
233
+ #
234
+ # @return [Boolean] Returns `true` when the object is uploaded without any errors.
235
+ #
236
+ # @see Client#create_multipart_upload
237
+ # @see Client#complete_multipart_upload
238
+ # @see Client#upload_part
239
+ def upload_stream(bucket:, key:, **options, &block)
240
+ uploading_options = options.dup
241
+ uploader = MultipartStreamUploader.new(
242
+ client: @client,
243
+ thread_count: uploading_options.delete(:thread_count),
244
+ tempfile: uploading_options.delete(:tempfile),
245
+ part_size: uploading_options.delete(:part_size)
246
+ )
247
+ uploader.upload(uploading_options.merge(bucket: bucket, key: key), &block)
248
+ true
249
+ end
250
+ end
251
+ end
252
+ end