Class: Aws::S3::TransferManager
- Inherits:
-
Object
- Object
- Aws::S3::TransferManager
- Defined in:
- gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb
Overview
A high-level S3 transfer utility that provides enhanced upload and download capabilities with automatic multipart handling, progress tracking, and handling of large files. The following features are supported:
- upload a file with multipart upload
- upload a stream with multipart upload
- download a S3 object with multipart download
- track transfer progress by using progress listener
Instance Attribute Summary collapse
-
#client ⇒ S3::Client
readonly
Instance Method Summary collapse
-
#download_file(destination, bucket:, key:, **options) ⇒ Boolean
Downloads a file in S3 to a path on disk.
-
#initialize(options = {}) ⇒ TransferManager
constructor
A new instance of TransferManager.
-
#upload_file(source, bucket:, key:, **options) {|response| ... } ⇒ Boolean
Uploads a file from disk to S3.
-
#upload_stream(bucket:, key:, **options, &block) ⇒ Boolean
Uploads a stream in a streaming fashion to S3.
Constructor Details
#initialize(options = {}) ⇒ TransferManager
Returns a new instance of TransferManager.
19 20 21 |
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb', line 19 def initialize( = {}) @client = .delete(:client) || Client.new end |
Instance Attribute Details
#client ⇒ S3::Client (readonly)
24 25 26 |
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb', line 24 def client @client end |
Instance Method Details
#download_file(destination, bucket:, key:, **options) ⇒ Boolean
Downloads a file in S3 to a path on disk.
# small files (< 5MB) are downloaded in a single API call
tm = TransferManager.new
tm.download_file('/path/to/file', bucket: 'bucket', key: 'key')
Files larger than 5MB are downloaded using multipart method:
# large files are split into parts and the parts are downloaded in parallel
tm.download_file('/path/to/large_file', bucket: 'bucket', key: 'key')
You can provide a callback to monitor progress of the download:
# bytes and part_sizes are each an array with 1 entry per part
# part_sizes may not be known until the first bytes are retrieved
progress = proc do |bytes, part_sizes, file_size|
bytes.map.with_index do |b, i|
puts "Part #{i + 1}: #{b} / #{part_sizes[i]}".join(' ') + "Total: #{100.0 * bytes.sum / file_size}%"
end
end
tm.download_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
96 97 98 99 100 |
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb', line 96 def download_file(destination, bucket:, key:, **) downloader = FileDownloader.new(client: @client) downloader.download(destination, .merge(bucket: bucket, key: key)) true end |
#upload_file(source, bucket:, key:, **options) {|response| ... } ⇒ Boolean
Uploads a file from disk to S3.
# a small file are uploaded with PutObject API
tm = TransferManager.new
tm.upload_file('/path/to/small_file', bucket: 'bucket', key: 'key')
Files larger than or equal to :multipart_threshold
are uploaded using multipart upload APIs.
# large files are automatically split into parts and the parts are uploaded in parallel
tm.upload_file('/path/to/large_file', bucket: 'bucket', key: 'key')
The response of the S3 upload API is yielded if a block given.
# API response will have etag value of the file
tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key') do |response|
etag = response.etag
end
You can provide a callback to monitor progress of the upload:
# bytes and totals are each an array with 1 entry per part
progress = proc do |bytes, totals|
bytes.map.with_index do |b, i|
puts "Part #{i + 1}: #{b} / #{totals[i]} " + "Total: #{100.0 * bytes.sum / totals.sum}%"
end
end
tm.upload_file('/path/to/file', bucket: 'bucket', key: 'key', progress_callback: progress)
169 170 171 172 173 174 175 176 177 178 |
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb', line 169 def upload_file(source, bucket:, key:, **) = .dup uploader = FileUploader.new( multipart_threshold: .delete(:multipart_threshold), client: @client ) response = uploader.upload(source, .merge(bucket: bucket, key: key)) yield response if block_given? true end |
#upload_stream(bucket:, key:, **options, &block) ⇒ Boolean
Uploads a stream in a streaming fashion to S3.
Passed chunks automatically split into multipart upload parts and the parts are uploaded in parallel. This allows for streaming uploads that never touch the disk.
Note: There are known issues in JRuby until jruby-9.1.15.0, so avoid using this with older JRuby versions.
231 232 233 234 235 236 237 238 239 240 241 |
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/transfer_manager.rb', line 231 def upload_stream(bucket:, key:, **, &block) = .dup uploader = MultipartStreamUploader.new( client: @client, thread_count: .delete(:thread_count), tempfile: .delete(:tempfile), part_size: .delete(:part_size) ) uploader.upload(.merge(bucket: bucket, key: key), &block) true end |