Class: S3::Client::API::Storage::MultipartUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/s3/client/api/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket, object, options = {}, &block) ⇒ MultipartUpload

Returns a new instance of MultipartUpload.



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/s3/client/api/storage.rb', line 271

def initialize(bucket, object, options = {}, &block)
  type = MIME::Types.type_for(object).first
  content_type = type ? type.to_s : 'application/octet-stream'
  options = options.merge(bucket: bucket, content_type: content_type)

  @bucket = bucket
  @object = object
  @splitsz = options.delete(:splitsz) || 100 * 1024 ** 2 #100MB
  @jobs = options.delete(:jobs) || 1
  @options = options
  @api = block[]
end

Instance Method Details

#abort_multipart_upload(upload_id) ⇒ Object



314
315
316
317
# File 'lib/s3/client/api/storage.rb', line 314

def abort_multipart_upload(upload_id)
  resource = "/#{@object}?uploadId=#{upload_id}"
  @api.execute_storage(RestParameter.new(:delete, resource, @options))
end

#complete_multipart_upload(upload_id, upload_objects) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/s3/client/api/storage.rb', line 298

def complete_multipart_upload(upload_id, upload_objects)
  resource = "/#{@object}?uploadId=#{upload_id}"

  payload = '<CompleteMultipartUpload>'
  upload_objects.each do |part, etag|
    payload += "<Part><PartNumber>#{part}</PartNumber><ETag>#{etag}</ETag></Part>"
  end
  payload += '</CompleteMultipartUpload>'

  @api.execute_storage(RestParameter.new(:post, resource, @options)) do
    payload
  end

  puts "complete multipart upload."
end

#initiate_multipart_uploadObject



284
285
286
287
288
289
290
# File 'lib/s3/client/api/storage.rb', line 284

def initiate_multipart_upload
  STDERR.puts "Initiate multipart upload...\njobs:#{@jobs}, splitsz:#{@splitsz}"
  resource = "/#{@object}?uploads"
  response = @api.execute_storage(RestParameter.new(:post, resource, @options))
  upload_id = response.elements['InitiateMultipartUploadResult/UploadId'].text
  return upload_id
end

#upload_part(upload_id, &block) ⇒ Object



292
293
294
295
296
# File 'lib/s3/client/api/storage.rb', line 292

def upload_part(upload_id, &block)
  upload_objects = {}
  split_stream(upload_id, upload_objects, &block)
  return Hash[upload_objects.sort]
end