Class: Dag::Client::API::Storage::MultipartUpload

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MultipartUpload.



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dag/client/api/storage.rb', line 262

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



305
306
307
308
# File 'lib/dag/client/api/storage.rb', line 305

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



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/dag/client/api/storage.rb', line 289

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



275
276
277
278
279
280
281
# File 'lib/dag/client/api/storage.rb', line 275

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



283
284
285
286
287
# File 'lib/dag/client/api/storage.rb', line 283

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