Class: Oss::Multipart

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/oss/multipart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#camelize, #hash_filter, #oss_headers_to_s, #set_query_string

Constructor Details

#initialize(client) ⇒ Multipart

Returns a new instance of Multipart.



12
13
14
# File 'lib/oss/multipart.rb', line 12

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/oss/multipart.rb', line 212

def method_missing(method)
  if @xml_obj.nil?
    super
  else
    camel = Util.camelize(method)
    value = @xml_obj.xpath(camel)
    raise "missing xml attribute #{camel}" if value.length == 0
    value.inner_text
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/oss/multipart.rb', line 10

def client
  @client
end

#xml_objObject

Returns the value of attribute xml_obj.



10
11
12
# File 'lib/oss/multipart.rb', line 10

def xml_obj
  @xml_obj
end

Instance Method Details

#abort_multipart_upload(bucket_name, object_name, upload_id) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/oss/multipart.rb', line 146

def abort_multipart_upload(bucket_name, object_name, upload_id)

  client.delete(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      sign_configs: {resource: "/#{bucket_name}", sign_query_string: true},
      query_string: {'uploadId' => upload_id},
  )

  true
end

#complete_multipart_upload(bucket_name, object_name, upload_id, parts) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/oss/multipart.rb', line 116

def complete_multipart_upload(bucket_name, object_name, upload_id, parts)
  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    CompleteMultipartUpload do
      parts.each do |part|
        Part do
          PartNumber part[:part_number]
          ETag part[:etag]
        end
      end
    end
  end

  @xml_obj = client.post(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      sign_configs: {resource: "/#{bucket_name}", sign_query_string: true, content_type: 'application/x-www-form-urlencoded',},
      query_string: {'uploadId' => upload_id},
      payload:      payload.to_xml
  ).xpath('CompleteMultipartUploadResult')

  # return object info
  {
      location: @xml_obj.xpath('Location').text,
      bucket:   @xml_obj.xpath('Bucket').text,
      key:      @xml_obj.xpath('Key').text,
      etag:     @xml_obj.xpath('ETag').text,
  }
end

#initiate_multipart_upload(bucket_name, object_name, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oss/multipart.rb', line 16

def initiate_multipart_upload(bucket_name, object_name, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        expires:             'Expires',
                                        content_control:     'Cache-Control',
                                        content_encoding:    'Content-Encoding',
                                        content_disposition: 'Content-Disposition',
                                        encryption:          'x-oss-server-side-encryption',
                                    })

  # sign configs
  sign_configs = {
      resource:     "/#{bucket_name}",
      content_type: 'application/x-www-form-urlencoded',
      oss_headers:  Util.oss_headers_to_s(options, {
                                                     encryption: 'x-oss-server-side-encryption',
                                                 })
  }

  @xml_obj = client.post(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}?uploads",
      headers:      headers,
      sign_configs: sign_configs,
  ).xpath('InitiateMultipartUploadResult')

  # return init info
  {
    bucket:    @xml_obj.xpath('Bucket').text,
    key:       @xml_obj.xpath('Key').text,
    upload_id: @xml_obj.xpath('UploadId').text,
  }
end

#list_multipart_upload(bucket_name, options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/oss/multipart.rb', line 158

def list_multipart_upload(bucket_name, options = {})
  @xml_obj = client.get(
      host:         "#{bucket_name}.#{client.endpoint}",
      sign_configs: {resource: "/#{bucket_name}"},
      path:         '/?uploads',
      query_string: options
  ).xpath('ListMultipartUploadsResult')

  self
end

#list_parts(bucket_name, object_name, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/oss/multipart.rb', line 184

def list_parts(bucket_name, object_name, options = {})
  @xml_obj = client.get(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      query_string: options,
      sign_configs: {resource: "/#{bucket_name}", sign_query_string: true},
  ).xpath('ListPartsResult')

  self
end

#partsObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/oss/multipart.rb', line 195

def parts
  parts = @xml_obj.xpath('Part')
  results = Array.new

  # parse as array
  parts.each do |part|
    results << {
        part_number:   part.xpath('PartNumber').text,
        last_modified: part.xpath('LastModified').text,
        etag:          part.xpath('ETag').text,
        size:          part.xpath('Size').text,
    }
  end
  results
end

#upload_part(bucket_name, object_name, upload_id, file, part_number = 1, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/oss/multipart.rb', line 50

def upload_part(bucket_name, object_name, upload_id, file, part_number = 1, options = {})
  # sign configs
  sign_configs = {
      resource:              "/#{bucket_name}",
      content_type:          'application/x-www-form-urlencoded',
      content_length_check:  true,
      content_md5_check:     options[:content_md5_check],
      sign_query_string:     true
  }

  resp = client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      sign_configs: sign_configs,
      query_string: {'partNumber' => part_number, 'uploadId' => upload_id},
      payload:      file,
      as:           :raw
  )

  {etag: resp.headers[:etag],  part_number: part_number}
end

#upload_part_copy(bucket_name, object_name, upload_id, old_bucket, old_object, part_number = 1, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/oss/multipart.rb', line 72

def upload_part_copy(bucket_name, object_name, upload_id, old_bucket, old_object, part_number = 1, options = {})
  # copy source format
  options[:copy_source] = "/#{old_bucket}/#{old_object}"
  options[:copy_source_range] = "bytes=#{options[:copy_source_begin]}-#{options[:copy_source_end]}" unless options[:copy_source_begin].nil?

  # set header
  headers = Util.hash_filter(options, {
                                        if_modified_since:   'x-oss-copy-source-if-modified-since',
                                        if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                        if_match:            'x-oss-copy-source-if-match',
                                        if_none_match:       'x-oss-copy-source-if-none-match',
                                        copy_source:         'x-oss-copy-source',
                                        copy_source_range:   'x-oss-copy-source-range'
                                    })

  # sign configs
  sign_configs = {
      resource:          "/#{bucket_name}",
      sign_query_string: true,
      content_type:      'application/x-www-form-urlencoded',
      oss_headers:       Util.oss_headers_to_s(options, {
                                                    if_modified_since:   'x-oss-copy-source-if-modified-since',
                                                    if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                                    if_match:            'x-oss-copy-source-if-match',
                                                    if_none_match:       'x-oss-copy-source-if-none-match',
                                                    copy_source:         'x-oss-copy-source',
                                                    copy_source_range:   'x-oss-copy-source-range'
                                                })
  }

  @xml_obj = client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      headers:      headers,
      sign_configs: sign_configs,
      query_string: {'partNumber' => part_number, 'uploadId' => upload_id},
  )

  {
      last_modify: @xml_obj.xpath('CopyPartResult/LastModified').text,
      etag:        @xml_obj.xpath('CopyPartResult/ETag').text,
  }
end

#uploadsObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/oss/multipart.rb', line 169

def uploads
  uploads = @xml_obj.xpath('Upload')
  results = Array.new

  # parse as array
  uploads.each do |upload|
    results << {
        key:       upload.xpath('Key').text,
        upload_id: upload.xpath('UploadId').text,
        initiated: upload.xpath('Initiated').text,
    }
  end
  results
end