Class: Oss::Object
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#xml_obj ⇒ Object
Returns the value of attribute xml_obj.
Instance Method Summary collapse
- #append_object(bucket_name, object, file, position = 0, options = {}) ⇒ Object
- #copy_object(bucket_name, object, old_bucket, old_object, options = {}) ⇒ Object
- #delete_multiple_objects(bucket_name, objects = []) ⇒ Object
-
#delete_object(bucket_name, object_name) ⇒ Object
params: - bucket_name - object_name.
- #get_object(bucket_name, object, options = {}) ⇒ Object
-
#get_object_acl(bucket_name, object_name) ⇒ Object
params: - bucket_name - object_name.
- #head_object(bucket_name, object_name, options = {}) ⇒ Object
-
#initialize(client) ⇒ Object
constructor
A new instance of Object.
- #method_missing(method) ⇒ Object
-
#post_object(bucket_name, key, options = {}) ⇒ Object
params: - bucket_name - key - options.
- #put_object(bucket_name, object, file, options = {}) ⇒ Object
-
#put_object_acl(bucket_name, object_name, acl) ⇒ Object
params: - bucket_name - object_name - acl.
Methods included from Util
#camelize, #hash_filter, #oss_headers_to_s, #set_query_string
Constructor Details
#initialize(client) ⇒ Object
Returns a new instance of Object.
14 15 16 |
# File 'lib/oss/object.rb', line 14 def initialize(client) @client = client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
305 306 307 308 309 310 311 312 313 314 |
# File 'lib/oss/object.rb', line 305 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
#client ⇒ Object
Returns the value of attribute client.
12 13 14 |
# File 'lib/oss/object.rb', line 12 def client @client end |
#xml_obj ⇒ Object
Returns the value of attribute xml_obj.
12 13 14 |
# File 'lib/oss/object.rb', line 12 def xml_obj @xml_obj end |
Instance Method Details
#append_object(bucket_name, object, file, position = 0, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/oss/object.rb', line 129 def append_object(bucket_name, object, file, position = 0, = {}) # set header headers = Util.hash_filter(, { expires: 'Expires', content_control: 'Cache-Control', content_encoding: 'Content-Encoding', content_type: 'Content-Type', content_md5: 'Content-MD5', content_disposition: 'Content-Disposition', encryption: 'x-oss-server-side-encryption', acl: 'x-oss-object-acl', }) # sign configs sign_configs = { resource: "/#{bucket_name}", content_type: [:content_type], content_md5: [:content_md5], sign_query_string: true, oss_headers: Util.oss_headers_to_s(, { acl: 'x-oss-object-acl', encryption: 'x-oss-server-side-encryption', }) } resp = client.post( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object}?append&position=#{position}", headers: headers, sign_configs: sign_configs, payload: file, as: :raw ) { hash_crc64ecma: resp.headers[:x_oss_hash_crc64ecma], next_append_position: resp.headers[:x_oss_next_append_position].to_i } end |
#copy_object(bucket_name, object, old_bucket, old_object, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 |
# File 'lib/oss/object.rb', line 54 def copy_object(bucket_name, object, old_bucket, old_object, = {}) # copy source format [:copy_source] = "/#{old_bucket}/#{old_object}" # set header headers = Util.hash_filter(, { metadata_directive: 'x-oss-metadata-directive', 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', encryption: 'x-oss-server-side-encryption', acl: 'x-oss-object-acl', copy_source: 'x-oss-copy-source' }) # sign configs sign_configs = { resource: "/#{bucket_name}", content_type: 'application/x-www-form-urlencoded', oss_headers: Util.oss_headers_to_s(, { metadata_directive: 'x-oss-metadata-directive', 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', encryption: 'x-oss-server-side-encryption', acl: 'x-oss-object-acl', copy_source: 'x-oss-copy-source' }) } @xml_obj = client.put( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object}", headers: headers, sign_configs: sign_configs, ) { last_modify: @xml_obj.xpath('CopyObjectResult/LastModified').text, etag: @xml_obj.xpath('CopyObjectResult/ETag').text, } end |
#delete_multiple_objects(bucket_name, objects = []) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/oss/object.rb', line 183 def delete_multiple_objects(bucket_name, objects = []) # build payload xml payload = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do Delete do Quiet 'false' objects.each do |obj| Object do Key obj end end end end xml_obj = client.post( host: "#{bucket_name}.#{client.endpoint}", path: "/?delete", sign_configs: {resource: "/#{bucket_name}", content_type: 'application/x-www-form-urlencoded'}, content_md5_check: true, content_length_check: true, payload: payload.to_xml ) # parse return deleted keys deleted = Array.new xml_obj.xpath('DeleteResult/Deleted').each do |obj| deleted << obj.xpath('Key').text end deleted end |
#delete_object(bucket_name, object_name) ⇒ Object
params:
-
bucket_name
-
object_name
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/oss/object.rb', line 172 def delete_object(bucket_name, object_name) client.delete( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object_name}", sign_configs: {resource: "/#{bucket_name}"} ) true end |
#get_object(bucket_name, object, options = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/oss/object.rb', line 99 def get_object(bucket_name, object, = {}) # set header headers = Util.hash_filter(, { range: 'Range', if_modified_since: 'If-Modified-Since', if_unmodified_since: 'If-Unmodified-Since', if_match: 'If-Match', if_none_match: 'If-None-Match' }) # request params query string query_string = Util.hash_filter(, { response_content_type: 'response-content-type', response_content_language: 'response-content-language', response_expires: 'response-expires', response_cache_control: 'response-cache-control', response_content_disposition: 'response-content-disposition', response_content_encoding: 'response-content-encoding' }) client.get( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object}", headers: headers, sign_configs: { resource: "/#{bucket_name}", sign_query_string: true }, query_string: query_string, as: :raw ) end |
#get_object_acl(bucket_name, object_name) ⇒ Object
params:
-
bucket_name
-
object_name
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/oss/object.rb', line 259 def get_object_acl(bucket_name, object_name) xml_obj = client.get( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object_name}?acl", sign_configs: {resource: "/#{bucket_name}"} ).xpath('AccessControlPolicy') { grant: xml_obj.xpath('AccessControlList/Grant').text, owner: { id: xml_obj.xpath('Owner/ID').text, display_name: xml_obj.xpath('Owner/DisplayName').text } } end |
#head_object(bucket_name, object_name, options = {}) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/oss/object.rb', line 214 def head_object(bucket_name, object_name, = {}) # set header headers = Util.hash_filter(, { if_modified_since: 'If-Modified-Since', if_unmodified_since: 'If-Unmodified-Since', if_match: 'If-Match', if_none_match: 'If-None-Match' }) resp = client.head( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object_name}", headers: headers, sign_configs: {resource: "/#{bucket_name}"}, as: :raw ) # head request returns hole response headers resp.headers end |
#post_object(bucket_name, key, options = {}) ⇒ Object
params:
-
bucket_name
-
key
-
options
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/oss/object.rb', line 279 def post_object(bucket_name, key, = {}) # build form upload info hash form_hash = { 'key' => key, 'action' => "http://#{bucket_name}.#{client.endpoint}/"} .each do |k, v| # need Signature if k == :policy # json policy string to base64 policy string form_hash['policy'] = Base64.encode64(v).gsub("\n", '') # create signature digest = OpenSSL::Digest.new('sha1') h = OpenSSL::HMAC.digest(digest, client.access_key_secret, form_hash['policy']) # base64 result form_hash['Signature'] = Base64.encode64(h).gsub("\n", '') # add access key id form_hash['OSSAccessKeyId'] = client.access_key_id else form_hash[k.to_s] = v end end # return a hash for render a html upload form form_hash end |
#put_object(bucket_name, object, file, options = {}) ⇒ Object
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 49 50 51 52 |
# File 'lib/oss/object.rb', line 18 def put_object(bucket_name, object, file, = {}) # set header headers = Util.hash_filter(, { expires: 'Expires', content_control: 'Cache-Control', content_encoding: 'Content-Encoding', content_type: 'Content-Type', content_md5: 'Content-MD5', content_disposition: 'Content-Disposition', encryption: 'x-oss-server-side-encryption', acl: 'x-oss-object-acl', }) # sign configs sign_configs = { resource: "/#{bucket_name}", content_type: [:content_type], content_md5_check: [:content_md5_check], content_length_check: [:content_length_check], oss_headers: Util.oss_headers_to_s(, { acl: 'x-oss-object-acl', encryption: 'x-oss-server-side-encryption', }) } client.put( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object}", headers: headers, sign_configs: sign_configs, payload: file, ) true end |
#put_object_acl(bucket_name, object_name, acl) ⇒ Object
params:
-
bucket_name
-
object_name
-
acl
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/oss/object.rb', line 239 def put_object_acl(bucket_name, object_name, acl) # sign configs sign_configs = Hash.new sign_configs[:resource] = "/#{bucket_name}" sign_configs[:oss_headers] = "x-oss-object-acl:#{acl}" sign_configs[:content_type] = 'application/x-www-form-urlencoded' @xml_obj = client.put( host: "#{bucket_name}.#{client.endpoint}", path: "/#{object_name}?acl", headers: {'x-oss-object-acl' => acl}, sign_configs: sign_configs ) true end |