Class: Aliyun::OSS::Multipart::Upload

Inherits:
Transaction show all
Defined in:
lib/aliyun/oss/upload.rb

Overview

A multipart upload transaction

Constant Summary collapse

PART_SIZE =
4 * 1024 * 1024
READ_SIZE =
16 * 1024

Constants included from Logging

Logging::DEFAULT_LOG_FILE

Instance Method Summary collapse

Methods included from Logging

#logger, set_log_file, set_log_level

Methods inherited from Struct::Base

#to_s

Methods included from Struct::Base::AttrHelper

#attrs

Constructor Details

#initialize(protocol, opts) ⇒ Upload

Returns a new instance of Upload.



13
14
15
16
17
18
19
20
21
22
# File 'lib/aliyun/oss/upload.rb', line 13

def initialize(protocol, opts)
  args = opts.dup
  @protocol = protocol
  @progress = args.delete(:progress)
  @file = args.delete(:file)
  @checkpoint_file = args.delete(:cpt_file)
  @file_meta = {}
  @parts = []
  super(args)
end

Instance Method Details

#checkpointObject

Checkpoint structures:

Examples:

states = {
  :id => 'upload_id',
  :file => 'file',
  :file_meta => {
    :mtime => Time.now,
    :md5 => 1024
  },
  :parts => [
    {:number => 1, :range => [0, 100], :done => false},
    {:number => 2, :range => [100, 200], :done => true}
  ],
  :md5 => 'states_md5'
}


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aliyun/oss/upload.rb', line 64

def checkpoint
  logger.debug("Begin make checkpoint, disable_cpt: #{options[:disable_cpt]}")

  ensure_file_not_changed

  states = {
    :id => id,
    :file => @file,
    :file_meta => @file_meta,
    :parts => @parts
  }

  # report progress
  if @progress
    done = @parts.count { |p| p[:done] }
    @progress.call(done.to_f / @parts.size) if done > 0
  end

  write_checkpoint(states, @checkpoint_file) unless options[:disable_cpt]

  logger.debug("Done make checkpoint, states: #{states}")
end

#runObject

Run the upload transaction, which includes 3 stages:

  • 1a. initiate(new upload) and divide parts

  • 1b. rebuild states(resumed upload)

    1. upload each unfinished part

    1. commit the multipart upload transaction



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aliyun/oss/upload.rb', line 29

def run
  logger.info("Begin upload, file: #{@file}, checkpoint file: " \
              "#{@checkpoint_file}")

  # Rebuild transaction states from checkpoint file
  # Or initiate new transaction states
  rebuild

  # Divide the file to upload into parts to upload separately
  divide_parts if @parts.empty?

  # Upload each part
  @parts.reject { |p| p[:done] }.each { |p| upload_part(p) }

  # Commit the multipart upload transaction
  commit

  logger.info("Done upload, file: #{@file}")
end