aws-sdk-s3 1.188.0 → 1.205.0

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 (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +119 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-sdk-s3/bucket.rb +43 -4
  5. data/lib/aws-sdk-s3/bucket_versioning.rb +33 -0
  6. data/lib/aws-sdk-s3/client.rb +1943 -252
  7. data/lib/aws-sdk-s3/client_api.rb +289 -0
  8. data/lib/aws-sdk-s3/customizations/object.rb +76 -86
  9. data/lib/aws-sdk-s3/customizations.rb +3 -1
  10. data/lib/aws-sdk-s3/default_executor.rb +103 -0
  11. data/lib/aws-sdk-s3/endpoint_parameters.rb +17 -17
  12. data/lib/aws-sdk-s3/endpoint_provider.rb +220 -50
  13. data/lib/aws-sdk-s3/endpoints.rb +110 -0
  14. data/lib/aws-sdk-s3/errors.rb +11 -0
  15. data/lib/aws-sdk-s3/file_downloader.rb +197 -134
  16. data/lib/aws-sdk-s3/file_uploader.rb +9 -13
  17. data/lib/aws-sdk-s3/legacy_signer.rb +2 -1
  18. data/lib/aws-sdk-s3/multipart_download_error.rb +8 -0
  19. data/lib/aws-sdk-s3/multipart_file_uploader.rb +92 -107
  20. data/lib/aws-sdk-s3/multipart_stream_uploader.rb +96 -107
  21. data/lib/aws-sdk-s3/multipart_upload_error.rb +3 -4
  22. data/lib/aws-sdk-s3/object.rb +110 -35
  23. data/lib/aws-sdk-s3/object_multipart_copier.rb +2 -1
  24. data/lib/aws-sdk-s3/object_summary.rb +72 -20
  25. data/lib/aws-sdk-s3/object_version.rb +7 -9
  26. data/lib/aws-sdk-s3/plugins/endpoints.rb +1 -1
  27. data/lib/aws-sdk-s3/plugins/url_encoded_keys.rb +2 -1
  28. data/lib/aws-sdk-s3/resource.rb +6 -0
  29. data/lib/aws-sdk-s3/transfer_manager.rb +303 -0
  30. data/lib/aws-sdk-s3/types.rb +1490 -189
  31. data/lib/aws-sdk-s3.rb +1 -1
  32. data/sig/bucket.rbs +12 -3
  33. data/sig/client.rbs +170 -31
  34. data/sig/errors.rbs +2 -0
  35. data/sig/multipart_upload.rbs +1 -1
  36. data/sig/object.rbs +15 -10
  37. data/sig/object_summary.rbs +11 -9
  38. data/sig/resource.rbs +8 -1
  39. data/sig/types.rbs +215 -29
  40. metadata +7 -4
@@ -0,0 +1,303 @@
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 capabilities with automatic
6
+ # multipart handling, progress tracking, and handling of large files. The following features are supported:
7
+ #
8
+ # * upload a file with multipart upload
9
+ # * upload a stream with multipart upload
10
+ # * download a S3 object with multipart download
11
+ # * track transfer progress by using progress listener
12
+ #
13
+ # ## Executor Management
14
+ # TransferManager uses executors to handle concurrent operations during multipart transfers. You can control
15
+ # concurrency behavior by providing a custom executor or relying on the default executor management.
16
+ #
17
+ # ### Default Behavior
18
+ # When no `:executor` is provided, TransferManager creates a new DefaultExecutor for each individual
19
+ # operation (`download_file`, `upload_file`, etc.) and automatically shuts it down when that operation completes.
20
+ # Each operation gets its own isolated thread pool with the specified `:thread_count` (default 10 threads).
21
+ #
22
+ # ### Custom Executor
23
+ # You can provide your own executor (e.g., `Concurrent::ThreadPoolExecutor`) for fine-grained control over thread
24
+ # pools and resource management. When using a custom executor, you are responsible for shutting it down
25
+ # when finished. The executor may be reused across multiple TransferManager operations.
26
+ #
27
+ # Custom executors must implement the same interface as DefaultExecutor.
28
+ #
29
+ # **Required methods:**
30
+ #
31
+ # * `post(*args, &block)` - Execute a task with given arguments and block
32
+ # * `kill` - Immediately terminate all running tasks
33
+ #
34
+ # **Optional methods:**
35
+ #
36
+ # * `shutdown(timeout = nil)` - Gracefully shutdown the executor with optional timeout
37
+ #
38
+ # @example Using default executor (automatic creation and shutdown)
39
+ # tm = TransferManager.new # No executor provided
40
+ # # DefaultExecutor created, used, and shutdown automatically
41
+ # tm.download_file('/path/to/file', bucket: 'bucket', key: 'key')
42
+ #
43
+ # @example Using custom executor (manual shutdown required)
44
+ # require 'concurrent-ruby'
45
+ #
46
+ # executor = Concurrent::ThreadPoolExecutor.new(max_threads: 5)
47
+ # tm = TransferManager.new(executor: executor)
48
+ # tm.download_file('/path/to/file1', bucket: 'bucket', key: 'key1')
49
+ # executor.shutdown # You must shutdown custom executors
50
+ #
51
+ class TransferManager
52
+
53
+ # @param [Hash] options
54
+ # @option options [S3::Client] :client (S3::Client.new)
55
+ # The S3 client to use for {TransferManager} operations. If not provided, a new default client
56
+ # will be created automatically.
57
+ # @option options [Object] :executor
58
+ # The executor to use for multipart operations. Must implement the same interface as {DefaultExecutor}.
59
+ # If not provided, a new {DefaultExecutor} will be created automatically for each operation and
60
+ # shutdown after completion. When provided a custom executor, it will be reused across operations, and
61
+ # you are responsible for shutting it down when finished.
62
+ def initialize(options = {})
63
+ @client = options[:client] || Client.new
64
+ @executor = options[:executor]
65
+ end
66
+
67
+ # @return [S3::Client]
68
+ attr_reader :client
69
+
70
+ # @return [Object]
71
+ attr_reader :executor
72
+
73
+ # Downloads a file in S3 to a path on disk.
74
+ #
75
+ # # small files (< 5MB) are downloaded in a single API call
76
+ # tm = TransferManager.new
77
+ # tm.download_file('/path/to/file', bucket: 'bucket', key: 'key')
78
+ #
79
+ # Files larger than 5MB are downloaded using multipart method:
80
+ #
81
+ # # large files are split into parts and the parts are downloaded in parallel
82
+ # tm.download_file('/path/to/large_file', bucket: 'bucket', key: 'key')
83
+ #
84
+ # You can provide a callback to monitor progress of the download:
85
+ #
86
+ # # bytes and part_sizes are each an array with 1 entry per part
87
+ # # part_sizes may not be known until the first bytes are retrieved
88
+ # progress = proc do |bytes, part_sizes, file_size|
89
+ # bytes.map.with_index do |b, i|
90
+ # puts "Part #{i + 1}: #{b} / #{part_sizes[i]}".join(' ') + "Total: #{100.0 * bytes.sum / file_size}%"
91
+ # end
92
+ # end
93
+ # tm.download_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
94
+ #
95
+ # @param [String, Pathname, File, Tempfile] destination
96
+ # Where to download the file to. This can either be a String or Pathname to the file, an open File object,
97
+ # or an open Tempfile object. If you pass an open File or Tempfile object, then you are responsible for
98
+ # closing it after the download completes. Download behavior varies by destination type:
99
+ #
100
+ # * **String/Pathname paths**: Downloads to a temporary file first, then atomically moves to the final
101
+ # destination. This prevents corruption of any existing file if the download fails.
102
+ # * **File/Tempfile objects**: Downloads directly to the file object without using temporary files.
103
+ # You are responsible for managing the file object's state and closing it after the download completes.
104
+ # If the download fails, the file object may contain partial data.
105
+ #
106
+ # @param [String] bucket
107
+ # The name of the S3 bucket to upload to.
108
+ #
109
+ # @param [String] key
110
+ # The object key name in S3 bucket.
111
+ #
112
+ # @param [Hash] options
113
+ # Additional options for {Client#get_object} and #{Client#head_object} may be provided.
114
+ #
115
+ # @option options [String] :mode ("auto") `"auto"`, `"single_request"` or `"get_range"`
116
+ #
117
+ # * `"auto"` mode is enabled by default, which performs `multipart_download`
118
+ # * `"single_request`" mode forces only 1 GET request is made in download
119
+ # * `"get_range"` mode requires `:chunk_size` parameter to configured in customizing each range size
120
+ #
121
+ # @option options [Integer] :chunk_size required in `"get_range"` mode.
122
+ #
123
+ # @option options [Integer] :thread_count (10) Customize threads used in the multipart download.
124
+ # Only used when no custom executor is provided (creates {DefaultExecutor} with given thread count).
125
+ #
126
+ # @option options [String] :checksum_mode ("ENABLED")
127
+ # This option is deprecated. Use `:response_checksum_validation` on your S3 client instead.
128
+ # To disable checksum validation, set `response_checksum_validation: 'when_required'`
129
+ # when creating your S3 client.
130
+ #
131
+ # @option options [Callable] :on_checksum_validated
132
+ # Called each time a request's checksum is validated with the checksum algorithm and the
133
+ # response. For multipart downloads, this will be called for each part that is downloaded and validated.
134
+ #
135
+ # @option options [Proc] :progress_callback
136
+ # A Proc that will be called when each chunk of the download is received. It will be invoked with
137
+ # `bytes_read`, `part_sizes`, `file_size`. When the object is downloaded as parts (rather than by ranges),
138
+ # the `part_sizes` will not be known ahead of time and will be `nil` in the callback until the first bytes
139
+ # in the part are received.
140
+ #
141
+ # @raise [MultipartDownloadError] Raised when an object validation fails outside of service errors.
142
+ #
143
+ # @return [Boolean] Returns `true` when the file is downloaded without any errors.
144
+ #
145
+ # @see Client#get_object
146
+ # @see Client#head_object
147
+ def download_file(destination, bucket:, key:, **options)
148
+ download_opts = options.merge(bucket: bucket, key: key)
149
+ executor = @executor || DefaultExecutor.new(max_threads: download_opts.delete(:thread_count))
150
+ downloader = FileDownloader.new(client: @client, executor: executor)
151
+ downloader.download(destination, download_opts)
152
+ executor.shutdown unless @executor
153
+ true
154
+ end
155
+
156
+ # Uploads a file from disk to S3.
157
+ #
158
+ # # a small file are uploaded with PutObject API
159
+ # tm = TransferManager.new
160
+ # tm.upload_file('/path/to/small_file', bucket: 'bucket', key: 'key')
161
+ #
162
+ # Files larger than or equal to `:multipart_threshold` are uploaded using multipart upload APIs.
163
+ #
164
+ # # large files are automatically split into parts and the parts are uploaded in parallel
165
+ # tm.upload_file('/path/to/large_file', bucket: 'bucket', key: 'key')
166
+ #
167
+ # The response of the S3 upload API is yielded if a block given.
168
+ #
169
+ # # API response will have etag value of the file
170
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key') do |response|
171
+ # etag = response.etag
172
+ # end
173
+ #
174
+ # You can provide a callback to monitor progress of the upload:
175
+ #
176
+ # # bytes and totals are each an array with 1 entry per part
177
+ # progress = proc do |bytes, totals|
178
+ # bytes.map.with_index do |b, i|
179
+ # puts "Part #{i + 1}: #{b} / #{totals[i]} " + "Total: #{100.0 * bytes.sum / totals.sum}%"
180
+ # end
181
+ # end
182
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
183
+ #
184
+ # @param [String, Pathname, File, Tempfile] source
185
+ # A file on the local file system that will be uploaded. This can either be a `String` or `Pathname` to the
186
+ # file, an open `File` object, or an open `Tempfile` object. If you pass an open `File` or `Tempfile` object,
187
+ # then you are responsible for closing it after the upload completes. When using an open Tempfile, rewind it
188
+ # before uploading or else the object will be empty.
189
+ #
190
+ # @param [String] bucket
191
+ # The name of the S3 bucket to upload to.
192
+ #
193
+ # @param [String] key
194
+ # The object key name for the uploaded file.
195
+ #
196
+ # @param [Hash] options
197
+ # Additional options for {Client#put_object} when file sizes below the multipart threshold.
198
+ # For files larger than the multipart threshold, options for {Client#create_multipart_upload},
199
+ # {Client#complete_multipart_upload}, and {Client#upload_part} can be provided.
200
+ #
201
+ # @option options [Integer] :multipart_threshold (104857600)
202
+ # Files larger han or equal to `:multipart_threshold` are uploaded using the S3 multipart upload APIs.
203
+ # Default threshold is `100MB`.
204
+ #
205
+ # @option options [Integer] :thread_count (10) Customize threads used in the multipart upload.
206
+ # Only used when no custom executor is provided (creates {DefaultExecutor} with the given thread count).
207
+ #
208
+ # @option options [Proc] :progress_callback (nil)
209
+ # A Proc that will be called when each chunk of the upload is sent.
210
+ # It will be invoked with `[bytes_read]` and `[total_sizes]`.
211
+ #
212
+ # @raise [MultipartUploadError] If a file is being uploaded in parts, and the upload can not be completed,
213
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that
214
+ # returns the failures that caused the upload to be aborted.
215
+ #
216
+ # @return [Boolean] Returns `true` when the file is uploaded without any errors.
217
+ #
218
+ # @see Client#put_object
219
+ # @see Client#create_multipart_upload
220
+ # @see Client#complete_multipart_upload
221
+ # @see Client#upload_part
222
+ def upload_file(source, bucket:, key:, **options)
223
+ upload_opts = options.merge(bucket: bucket, key: key)
224
+ executor = @executor || DefaultExecutor.new(max_threads: upload_opts.delete(:thread_count))
225
+ uploader = FileUploader.new(
226
+ multipart_threshold: upload_opts.delete(:multipart_threshold),
227
+ client: @client,
228
+ executor: executor
229
+ )
230
+ response = uploader.upload(source, upload_opts)
231
+ yield response if block_given?
232
+ executor.shutdown unless @executor
233
+ true
234
+ end
235
+
236
+ # Uploads a stream in a streaming fashion to S3.
237
+ #
238
+ # Passed chunks automatically split into multipart upload parts and the parts are uploaded in parallel.
239
+ # This allows for streaming uploads that never touch the disk.
240
+ #
241
+ # **Note**: There are known issues in JRuby until jruby-9.1.15.0, so avoid using this with older JRuby versions.
242
+ #
243
+ # @example Streaming chunks of data
244
+ # tm = TransferManager.new
245
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
246
+ # 10.times { write_stream << 'foo' }
247
+ # end
248
+ # @example Streaming chunks of data
249
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
250
+ # IO.copy_stream(IO.popen('ls'), write_stream)
251
+ # end
252
+ # @example Streaming chunks of data
253
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
254
+ # IO.copy_stream(STDIN, write_stream)
255
+ # end
256
+ #
257
+ # @param [String] bucket
258
+ # The name of the S3 bucket to upload to.
259
+ #
260
+ # @param [String] key
261
+ # The object key name for the uploaded file.
262
+ #
263
+ # @param [Hash] options
264
+ # Additional options for {Client#create_multipart_upload}, {Client#complete_multipart_upload}, and
265
+ # {Client#upload_part} can be provided.
266
+ #
267
+ # @option options [Integer] :thread_count (10)
268
+ # The number of parallel multipart uploads. Only used when no custom executor is provided (creates
269
+ # {DefaultExecutor} with the given thread count). An additional thread is used internally for task coordination.
270
+ #
271
+ # @option options [Boolean] :tempfile (false)
272
+ # Normally read data is stored in memory when building the parts in order to complete the underlying
273
+ # multipart upload. By passing `:tempfile => true`, the data read will be temporarily stored on disk reducing
274
+ # the memory footprint vastly.
275
+ #
276
+ # @option options [Integer] :part_size (5242880)
277
+ # Define how big each part size but the last should be. Default `:part_size` is `5 * 1024 * 1024`.
278
+ #
279
+ # @raise [MultipartUploadError] If an object is being uploaded in parts, and the upload can not be completed,
280
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that returns
281
+ # the failures that caused the upload to be aborted.
282
+ #
283
+ # @return [Boolean] Returns `true` when the object is uploaded without any errors.
284
+ #
285
+ # @see Client#create_multipart_upload
286
+ # @see Client#complete_multipart_upload
287
+ # @see Client#upload_part
288
+ def upload_stream(bucket:, key:, **options, &block)
289
+ upload_opts = options.merge(bucket: bucket, key: key)
290
+ executor = @executor || DefaultExecutor.new(max_threads: upload_opts.delete(:thread_count))
291
+ uploader = MultipartStreamUploader.new(
292
+ client: @client,
293
+ executor: executor,
294
+ tempfile: upload_opts.delete(:tempfile),
295
+ part_size: upload_opts.delete(:part_size)
296
+ )
297
+ uploader.upload(upload_opts, &block)
298
+ executor.shutdown unless @executor
299
+ true
300
+ end
301
+ end
302
+ end
303
+ end