Class: QcloudCos::ObjectManager

Inherits:
Object
  • Object
show all
Defined in:
lib/qcloud_cos/object_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket: nil, region: nil, access_id: nil, access_key: nil, token: nil) ⇒ ObjectManager

Returns a new instance of ObjectManager.



5
6
7
8
9
10
11
12
# File 'lib/qcloud_cos/object_manager.rb', line 5

def initialize(bucket: nil, region: nil, access_id: nil, access_key: nil, token: nil)
  @bucket = bucket
  @region = region
  @access_id = access_id
  @access_key = access_key
  @token = token
  @http = QcloudCos::Http.new(access_id, access_key, token: token)
end

Instance Attribute Details

#access_idObject

Returns the value of attribute access_id.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def access_id
  @access_id
end

#access_keyObject

Returns the value of attribute access_key.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def access_key
  @access_key
end

#bucketObject

Returns the value of attribute bucket.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def bucket
  @bucket
end

#httpObject

Returns the value of attribute http.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def http
  @http
end

#regionObject

Returns the value of attribute region.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def region
  @region
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/qcloud_cos/object_manager.rb', line 4

def token
  @token
end

Instance Method Details

#compute_url(path) ⇒ Object



41
42
43
# File 'lib/qcloud_cos/object_manager.rb', line 41

def compute_url(path)
  URI.join("https://#{host}", path).to_s
end

#copy_object(path, copy_source, headers = {}) ⇒ Object



19
20
21
22
23
# File 'lib/qcloud_cos/object_manager.rb', line 19

def copy_object(path, copy_source, headers = {})
  headers['x-cos-copy-source'] = copy_source
  body = http.put(compute_url(path), nil, headers).body
  ActiveSupport::HashWithIndifferentAccess.new(ActiveSupport::XmlMini.parse(body))
end

#delete_object(path) ⇒ Object



25
26
27
# File 'lib/qcloud_cos/object_manager.rb', line 25

def delete_object(path)
  http.delete(compute_url(path))
end

#delete_objects(pathes, quiet = false) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/qcloud_cos/object_manager.rb', line 29

def delete_objects(pathes, quiet = false)
  data = {
    "Quiet" => quiet,
    "Object" => pathes.map { |p| { "Key" => p } },
  }
  http.post(compute_url("/?delete"), data.to_xml(root: "Delete", skip_instruct: true, skip_types: false), "Content-Type" => "application/xml")
end

#hostObject



37
38
39
# File 'lib/qcloud_cos/object_manager.rb', line 37

def host
  "#{bucket}.cos.#{region}.myqcloud.com"
end

#put_object(path, file_or_bin, headers = {}) ⇒ Object



14
15
16
17
# File 'lib/qcloud_cos/object_manager.rb', line 14

def put_object(path, file_or_bin, headers = {})
  data = file_or_bin.respond_to?(:read) ? IO.binread(file_or_bin) : file_or_bin
  http.put(compute_url(path), data, headers)
end