Module: Aliyun::Oss::Api::BucketObjects

Included in:
Client
Defined in:
lib/aliyun/oss/api/bucket_objects.rb

Instance Method Summary collapse

Instance Method Details

#bucket_append_object(key, file, position = 0, headers = {}) ⇒ Response

Append data to a object, will create Appendable object

Parameters:

  • key (String)

    object name

  • file (file, bin data)

    the data to append

  • position (Integer) (defaults to: 0)

    append to position of object

  • headers (Hash) (defaults to: {})

    a customizable set of options

Options Hash (headers):

  • :Content-Type (String) — default: 'application/x-www-form-urlencoded'

    Specify Content-Type for the object

  • :Cache-Control (String)

    Specify the caching behavior when download from browser, ref RFC2616

  • :Content-Disposition (String)

    Specify the name when download, ref RFC2616

  • :Content-Encoding (String)

    Specify the content encoding when download, ref RFC2616

  • :Content-MD5 (String)

    RFC 1864 according to the agreement of the message Content (not including head) are calculated MD5 value 128 bits number, the number is base64 encoding for the Content of a message - MD5 value.The legality of the examination of the request headers can be used for information (a message content is consistent with send).Although the request header is optional, OSS recommend that users use the end-to-end check request header.

  • :Expires (Integer)

    Specify the expiration time (milliseconds)

  • :x-oss-server-side-encryption (String)

    Specify the oss server-side encryption algorithm when the object was created. supported value: ‘AES256’

  • :x-oss-object-acl (String)

    Specify the oss access when the object was created. supported value: public-read-write | public-read | private

  • other (Hash)

    options will insert into headers when upload, such as user meta headers, eg: headers with prefix: x-oss-meta-

Returns:

  • (Response)

Raises:

See Also:



88
89
90
91
92
93
94
95
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 88

def bucket_append_object(key, file, position = 0, headers = {})
  Utils.stringify_keys!(headers)

  query = { 'append' => true, 'position' => position }
  body = Utils.to_data(file)

  http.post("/#{key}", query: query, headers: headers, body: body, bucket: bucket, key: key)
end

#bucket_copy_object(key, source_bucket, source_key, headers = {}) ⇒ Response

Copy an existing object in OSS into another object

Parameters:

  • key (String)

    the object name

  • source_bucket (String)

    the source bucket name

  • source_key (String)

    the source object name

  • headers (Hash) (defaults to: {})

Options Hash (headers):

  • :x-oss-copy-source-if-match (String)

    If the specified ETag match the source object ETag, normal transfer and return 200; Otherwise return 412(precondition)

  • :x-oss-copy-source-if-none-match (String)

    If the specified ETag not match the source object ETag, normal transfer and return 200; Otherwise return 304(Not Modified)

  • :x-oss-copy-source-if-unmodified-since (String)

    If the specified time is equal to or later than the source object last modification time, normal transfer ans return 200; Otherwise returns 412(precondition)

  • :x-oss-copy-source-if-modified-since (String)

    If the specified time is earlier than the source object last modification time, normal transfer ans return 200; Otherwise returns 304(not modified)

  • :x-oss-metadata-directive (String) — default: 'COPY'

    supported value: COPY, REPLACE;

  • :x-oss-server-side-encryption (String)

    supported value: AES256

  • :x-oss-object-acl (String)

    supported value: public-read, private, public-read-write

Returns:

  • (Response)

Raises:

See Also:



66
67
68
69
70
71
72
73
74
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 66

def bucket_copy_object(key, source_bucket, source_key, headers = {})
  fail('source_bucket must be not empty!') if source_bucket.nil? || source_bucket.empty?
  fail('source_key must be not empty!') if source_key.nil? || source_key.empty?

  Utils.stringify_keys!(headers)
  headers.merge!('x-oss-copy-source' => "/#{source_bucket}/#{source_key}")

  http.put("/#{key}", headers: headers, bucket: bucket, key: key)
end

#bucket_create_object(key, file, headers = {}) ⇒ Response

Upload file to bucket

Parameters:

  • key (String)

    Specify object name

  • file (File, Bin data)

    Specify need upload resource

  • headers (Hash) (defaults to: {})

    Specify other options

Options Hash (headers):

  • :Content-Type (String) — default: 'application/x-www-form-urlencoded'

    Specify Content-Type for the object

  • :Cache-Control (String)

    Specify the caching behavior when download from browser, ref RFC2616

  • :Content-Disposition (String)

    Specify the name when download, ref RFC2616

  • :Content-Encoding (String)

    Specify the content encoding when download, ref RFC2616

  • :Content-MD5 (String)

    RFC 1864 according to the agreement of the message Content (not including head) are calculated MD5 value 128 bits number, the number is base64 encoding for the Content of a message - MD5 value.The legality of the examination of the request headers can be used for information (a message content is consistent with send).Although the request header is optional, OSS recommend that users use the end-to-end check request header.

  • :Expires (Integer)

    Specify the expiration time (milliseconds)

  • :x-oss-server-side-encryption (String)

    Specify the oss server-side encryption algorithm when the object was created. supported value: ‘AES256’

  • :x-oss-object-acl (String)

    Specify the oss access when the object was created. supported value: public-read-write | public-read | private

  • other (Hash)

    options will insert into headers when upload, such as user meta headers, eg: headers with prefix: x-oss-meta-

Returns:

  • (Response)

See Also:



42
43
44
45
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 42

def bucket_create_object(key, file, headers = {})
  Utils.stringify_keys!(headers)
  http.put("/#{key}", headers: headers, body: Utils.to_data(file), bucket: bucket, key: key)
end

#bucket_delete_object(key) ⇒ Response

Delete object from bucket

Parameters:

  • key (String)

    the object name

Returns:

  • (Response)

See Also:



152
153
154
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 152

def bucket_delete_object(key)
  http.delete("/#{key}", bucket: bucket, key: key)
end

#bucket_delete_objects(keys, quiet = false) ⇒ Response

Delete multiple objects, at max 1000 at once

Parameters:

  • keys (Array<String>)

    the object names

  • quiet (Boolean) (defaults to: false)

    Specify response mode: false(Quiet) return results for error objects, true(Verbose) return results of every objects

Returns:

  • (Response)

See Also:



164
165
166
167
168
169
170
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 164

def bucket_delete_objects(keys, quiet = false)
  query = { 'delete' => true }

  body = XmlGenerator.generate_delete_objects_xml(keys, quiet)

  http.post('/', query: query, body: body, bucket: bucket)
end

#bucket_get_meta_object(key, headers = {}) ⇒ Response

Get meta information of object

Parameters:

  • key (String)

    object name

  • headers (Hash) (defaults to: {})

    headers

Options Hash (headers):

  • :If-Modified-Since (String)

    If the specified time is earlier than the file last modification time, return 200 OK; Otherwise returns 304(not modified)

  • :If-Unmodified-Since (String)

    If the specified time is equal to or later than the file last modification time, normal transfer ans return 200; Otherwise returns 412(precondition)

  • :If-Match (String)

    If the specified ETag match the object ETag, normal transfer and return 200; Otherwise return 412(precondition)

  • :If-None-Match (String)

    If the specified ETag not match the object ETag, normal transfer and return 200; Otherwise return 304(Not Modified)

Returns:

  • (Response)

Raises:

See Also:



186
187
188
189
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 186

def bucket_get_meta_object(key, headers = {})
  Utils.stringify_keys!(headers)
  http.head("/#{key}", headers: headers, bucket: bucket, key: key)
end

#bucket_get_object(key, query = {}, headers = {}) ⇒ Response

Get the object

Parameters:

  • key (String)

    the object name

  • query (Hash) (defaults to: {})

    query params

  • headers (Hash) (defaults to: {})

    headers

Options Hash (query):

  • :response-content-type (String)

    Specify the header Content-Type in response

  • :response-content-language (String)

    Specify the header Content-Language in response

  • :response-expires (String)

    Specify the header Expires in response

  • :response-cache-control (String)

    Specify the header Cache-Control in response

  • :response-content-disposition (String)

    Specify the header Content-Disposition in response

  • :response-content-encoding (String)

    Specify the header Content-encoding in response

Options Hash (headers):

  • :Range (String)

    Specify the range of the file. Such as “bytes=0-9” means the 10 characters from 0 to 9.

  • :If-Modified-Since (String)

    If the specified time is earlier than the file last modification time, return 200 OK; Otherwise returns 304(not modified)

  • :If-Unmodified-Since (String)

    If the specified time is equal to or later than the file last modification time, normal transfer ans return 200; Otherwise returns 412(precondition)

  • :If-Match (String)

    If the specified ETag match the object ETag, normal transfer and return 200; Otherwise return 412(precondition)

  • :If-None-Match (String)

    If the specified ETag not match the object ETag, normal transfer and return 200; Otherwise return 304(Not Modified)

Returns:

  • (Response)

See Also:



117
118
119
120
121
122
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 117

def bucket_get_object(key, query = {}, headers = {})
  Utils.stringify_keys!(query)
  Utils.stringify_keys!(headers)

  http.get("/#{key}", query: query, headers: headers, bucket: bucket, key: key)
end

#bucket_get_object_acl(key) ⇒ Response

Get access of object

Parameters:

  • key (String)

    object name

Returns:

  • (Response)

Raises:

See Also:



200
201
202
203
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 200

def bucket_get_object_acl(key)
  query = { 'acl' => true }
  http.get("/#{key}", query: query, bucket: bucket, key: key)
end

Get object share link

Parameters:

  • key (String)

    the Object name

  • expired_in_seconds (Integer)

    expire after specify seconds

Returns:

  • (String)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 130

def bucket_get_object_share_link(key, expired_in_seconds)
  expire_time = Time.now.to_i + expired_in_seconds

  signature = Authorization.get_temporary_signature(
    @secret_key,
    expire_time,
    verb: 'GET',
    bucket: bucket,
    key: key
  )

  Utils.get_endpoint(bucket, @options[:host]) + "#{key}?" \
    "OSSAccessKeyId=#{@access_key}&Expires=#{expire_time}&Signature=#{signature}"
end

#bucket_list_objects(options = {}) ⇒ Response

List objects in the bucket

Parameters:

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :prefix (String)

    Filter objects with prefix

  • :marker (String)

    Result should after marker in alphabetical order

  • :max-keys (Integer) — default: 100

    Limit number of objects, the maxinum should <= 1000

  • :delimiter (String)

    Used to group objects with delimiter

  • :encoding-type (String)

    Encoding type used for unsupported character

Returns:

  • (Response)

See Also:



17
18
19
20
21
22
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 17

def bucket_list_objects(options = {})
  Utils.stringify_keys!(options)
  accepted_keys = ['prefix', 'marker', 'max-keys', 'delimiter', 'encoding-type']
  query = Utils.hash_slice(options, *accepted_keys)
  http.get('/', query: query, bucket: bucket)
end

#bucket_set_object_acl(key, acl) ⇒ Response

Set access of object

Parameters:

  • key (String)

    object name

  • acl (String)

    access value, supported value: private, public-read, public-read-write

Returns:

  • (Response)

Raises:

See Also:



215
216
217
218
219
# File 'lib/aliyun/oss/api/bucket_objects.rb', line 215

def bucket_set_object_acl(key, acl)
  query = { 'acl' => true }
  headers = { 'x-oss-object-acl' => acl }
  http.put("/#{key}", query: query, headers: headers, bucket: bucket, key: key)
end