aws-sdk-s3 1.196.0 → 1.197.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.
@@ -0,0 +1,244 @@
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] destination
49
+ # Where to download the file to.
50
+ #
51
+ # @param [String] bucket
52
+ # The name of the S3 bucket to upload to.
53
+ #
54
+ # @param [String] key
55
+ # The object key name in S3 bucket.
56
+ #
57
+ # @param [Hash] options
58
+ # Additional options for {Client#get_object} and #{Client#head_object} may be provided.
59
+ #
60
+ # @option options [String] :mode ("auto") `"auto"`, `"single_request"` or `"get_range"`
61
+ #
62
+ # * `"auto"` mode is enabled by default, which performs `multipart_download`
63
+ # * `"single_request`" mode forces only 1 GET request is made in download
64
+ # * `"get_range"` mode requires `:chunk_size` parameter to configured in customizing each range size
65
+ #
66
+ # @option options [Integer] :chunk_size required in `"get_range"` mode.
67
+ #
68
+ # @option options [Integer] :thread_count (10) Customize threads used in the multipart download.
69
+ #
70
+ # @option options [String] :version_id The object version id used to retrieve the object.
71
+ #
72
+ # @see https://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectVersioning.html ObjectVersioning
73
+ #
74
+ # @option options [String] :checksum_mode ("ENABLED")
75
+ # When `"ENABLED"` and the object has a stored checksum, it will be used to validate the download and will
76
+ # raise an `Aws::Errors::ChecksumError` if checksum validation fails. You may provide a `on_checksum_validated`
77
+ # callback if you need to verify that validation occurred and which algorithm was used.
78
+ # To disable checksum validation, set `checksum_mode` to `"DISABLED"`.
79
+ #
80
+ # @option options [Callable] :on_checksum_validated
81
+ # Called each time a request's checksum is validated with the checksum algorithm and the
82
+ # response. For multipart downloads, this will be called for each part that is downloaded and validated.
83
+ #
84
+ # @option options [Proc] :progress_callback
85
+ # A Proc that will be called when each chunk of the download is received. It will be invoked with
86
+ # `bytes_read`, `part_sizes`, `file_size`. When the object is downloaded as parts (rather than by ranges),
87
+ # the `part_sizes` will not be known ahead of time and will be `nil` in the callback until the first bytes
88
+ # in the part are received.
89
+ #
90
+ # @raise [MultipartDownloadError] Raised when an object validation fails outside of service errors.
91
+ #
92
+ # @return [Boolean] Returns `true` when the file is downloaded without any errors.
93
+ #
94
+ # @see Client#get_object
95
+ # @see Client#head_object
96
+ def download_file(destination, bucket:, key:, **options)
97
+ downloader = FileDownloader.new(client: @client)
98
+ downloader.download(destination, options.merge(bucket: bucket, key: key))
99
+ true
100
+ end
101
+
102
+ # Uploads a file from disk to S3.
103
+ #
104
+ # # a small file are uploaded with PutObject API
105
+ # tm = TransferManager.new
106
+ # tm.upload_file('/path/to/small_file', bucket: 'bucket', key: 'key')
107
+ #
108
+ # Files larger than or equal to `:multipart_threshold` are uploaded using multipart upload APIs.
109
+ #
110
+ # # large files are automatically split into parts and the parts are uploaded in parallel
111
+ # tm.upload_file('/path/to/large_file', bucket: 'bucket', key: 'key')
112
+ #
113
+ # The response of the S3 upload API is yielded if a block given.
114
+ #
115
+ # # API response will have etag value of the file
116
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key') do |response|
117
+ # etag = response.etag
118
+ # end
119
+ #
120
+ # You can provide a callback to monitor progress of the upload:
121
+ #
122
+ # # bytes and totals are each an array with 1 entry per part
123
+ # progress = proc do |bytes, totals|
124
+ # bytes.map.with_index do |b, i|
125
+ # puts "Part #{i + 1}: #{b} / #{totals[i]} " + "Total: #{100.0 * bytes.sum / totals.sum}%"
126
+ # end
127
+ # end
128
+ # tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
129
+ #
130
+ # @param [String, Pathname, File, Tempfile] source
131
+ # A file on the local file system that will be uploaded. This can either be a `String` or `Pathname` to the
132
+ # file, an open `File` object, or an open `Tempfile` object. If you pass an open `File` or `Tempfile` object,
133
+ # then you are responsible for closing it after the upload completes. When using an open Tempfile, rewind it
134
+ # before uploading or else the object will be empty.
135
+ #
136
+ # @param [String] bucket
137
+ # The name of the S3 bucket to upload to.
138
+ #
139
+ # @param [String] key
140
+ # The object key name for the uploaded file.
141
+ #
142
+ # @param [Hash] options
143
+ # Additional options for {Client#put_object} when file sizes below the multipart threshold.
144
+ # For files larger than the multipart threshold, options for {Client#create_multipart_upload},
145
+ # {Client#complete_multipart_upload}, and {Client#upload_part} can be provided.
146
+ #
147
+ # @option options [Integer] :multipart_threshold (104857600)
148
+ # Files larger han or equal to `:multipart_threshold` are uploaded using the S3 multipart upload APIs.
149
+ # Default threshold is `100MB`.
150
+ #
151
+ # @option options [Integer] :thread_count (10)
152
+ # The number of parallel multipart uploads. This option is not used if the file is smaller than
153
+ # `:multipart_threshold`.
154
+ #
155
+ # @option options [Proc] :progress_callback (nil)
156
+ # A Proc that will be called when each chunk of the upload is sent.
157
+ # It will be invoked with `[bytes_read]` and `[total_sizes]`.
158
+ #
159
+ # @raise [MultipartUploadError] If an file is being uploaded in parts, and the upload can not be completed,
160
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that
161
+ # returns the failures that caused the upload to be aborted.
162
+ #
163
+ # @return [Boolean] Returns `true` when the file is uploaded without any errors.
164
+ #
165
+ # @see Client#put_object
166
+ # @see Client#create_multipart_upload
167
+ # @see Client#complete_multipart_upload
168
+ # @see Client#upload_part
169
+ def upload_file(source, bucket:, key:, **options)
170
+ uploading_options = options.dup
171
+ uploader = FileUploader.new(
172
+ multipart_threshold: uploading_options.delete(:multipart_threshold),
173
+ client: @client
174
+ )
175
+ response = uploader.upload(source, uploading_options.merge(bucket: bucket, key: key))
176
+ yield response if block_given?
177
+ true
178
+ end
179
+
180
+ # Uploads a stream in a streaming fashion to S3.
181
+ #
182
+ # Passed chunks automatically split into multipart upload parts and the parts are uploaded in parallel.
183
+ # This allows for streaming uploads that never touch the disk.
184
+ #
185
+ # **Note**: There are known issues in JRuby until jruby-9.1.15.0, so avoid using this with older JRuby versions.
186
+ #
187
+ # @example Streaming chunks of data
188
+ # tm = TransferManager.new
189
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
190
+ # 10.times { write_stream << 'foo' }
191
+ # end
192
+ # @example Streaming chunks of data
193
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
194
+ # IO.copy_stream(IO.popen('ls'), write_stream)
195
+ # end
196
+ # @example Streaming chunks of data
197
+ # tm.upload_stream(bucket: 'bucket', key: 'key') do |write_stream|
198
+ # IO.copy_stream(STDIN, write_stream)
199
+ # end
200
+ #
201
+ # @param [String] bucket
202
+ # The name of the S3 bucket to upload to.
203
+ #
204
+ # @param [String] key
205
+ # The object key name for the uploaded file.
206
+ #
207
+ # @param [Hash] options
208
+ # Additional options for {Client#create_multipart_upload}, {Client#complete_multipart_upload}, and
209
+ # {Client#upload_part} can be provided.
210
+ #
211
+ # @option options [Integer] :thread_count (10)
212
+ # The number of parallel multipart uploads.
213
+ #
214
+ # @option options [Boolean] :tempfile (false)
215
+ # Normally read data is stored in memory when building the parts in order to complete the underlying
216
+ # multipart upload. By passing `:tempfile => true`, the data read will be temporarily stored on disk reducing
217
+ # the memory footprint vastly.
218
+ #
219
+ # @option options [Integer] :part_size (5242880)
220
+ # Define how big each part size but the last should be. Default `:part_size` is `5 * 1024 * 1024`.
221
+ #
222
+ # @raise [MultipartUploadError] If an object is being uploaded in parts, and the upload can not be completed,
223
+ # then the upload is aborted and this error is raised. The raised error has a `#errors` method that returns
224
+ # the failures that caused the upload to be aborted.
225
+ #
226
+ # @return [Boolean] Returns `true` when the object is uploaded without any errors.
227
+ #
228
+ # @see Client#create_multipart_upload
229
+ # @see Client#complete_multipart_upload
230
+ # @see Client#upload_part
231
+ def upload_stream(bucket:, key:, **options, &block)
232
+ uploading_options = options.dup
233
+ uploader = MultipartStreamUploader.new(
234
+ client: @client,
235
+ thread_count: uploading_options.delete(:thread_count),
236
+ tempfile: uploading_options.delete(:tempfile),
237
+ part_size: uploading_options.delete(:part_size)
238
+ )
239
+ uploader.upload(uploading_options.merge(bucket: bucket, key: key), &block)
240
+ true
241
+ end
242
+ end
243
+ end
244
+ end
data/lib/aws-sdk-s3.rb CHANGED
@@ -75,7 +75,7 @@ module Aws::S3
75
75
  autoload :ObjectVersion, 'aws-sdk-s3/object_version'
76
76
  autoload :EventStreams, 'aws-sdk-s3/event_streams'
77
77
 
78
- GEM_VERSION = '1.196.0'
78
+ GEM_VERSION = '1.197.0'
79
79
 
80
80
  end
81
81
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sdk-s3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.196.0
4
+ version: 1.197.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
@@ -134,6 +134,7 @@ files:
134
134
  - lib/aws-sdk-s3/file_part.rb
135
135
  - lib/aws-sdk-s3/file_uploader.rb
136
136
  - lib/aws-sdk-s3/legacy_signer.rb
137
+ - lib/aws-sdk-s3/multipart_download_error.rb
137
138
  - lib/aws-sdk-s3/multipart_file_uploader.rb
138
139
  - lib/aws-sdk-s3/multipart_stream_uploader.rb
139
140
  - lib/aws-sdk-s3/multipart_upload.rb
@@ -169,6 +170,7 @@ files:
169
170
  - lib/aws-sdk-s3/presigned_post.rb
170
171
  - lib/aws-sdk-s3/presigner.rb
171
172
  - lib/aws-sdk-s3/resource.rb
173
+ - lib/aws-sdk-s3/transfer_manager.rb
172
174
  - lib/aws-sdk-s3/types.rb
173
175
  - lib/aws-sdk-s3/waiters.rb
174
176
  - sig/bucket.rbs