Class: BosClient::Object
- Inherits:
-
Object
- Object
- BosClient::Object
- Defined in:
- lib/bos_client/object.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#size ⇒ Object
Returns the value of attribute size.
-
#storage_class ⇒ Object
Returns the value of attribute storage_class.
Class Method Summary collapse
Instance Method Summary collapse
- #destory ⇒ Object
-
#initialize(options = {}) ⇒ Object
constructor
A new instance of Object.
- #save ⇒ Object
- #save_as(name) ⇒ Object
- #save_to(path, name = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/bos_client/object.rb', line 6 def initialize( = {}) @bucket = [:bucket] @size = [:size] @file = [:key] @path, @name = File.split([:key]) @last_modified = [:last_modified] @storage_class = [:storage_class] end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def bucket @bucket end |
#last_modified ⇒ Object
Returns the value of attribute last_modified.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def last_modified @last_modified end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def path @path end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def size @size end |
#storage_class ⇒ Object
Returns the value of attribute storage_class.
4 5 6 |
# File 'lib/bos_client/object.rb', line 4 def storage_class @storage_class end |
Class Method Details
.upload(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 |
# File 'lib/bos_client/object.rb', line 54 def self.upload( = {}) bucket = [:bucket] file = [:file] origin_file_name = File.basename(file) filename = [:filename] || origin_file_name path = [:path] || '' storage_class = [:storage_class] || 'STANDARD' headers = [:headers] || {} params = { append: '' } headers['x-bce-storage-class'] = storage_class url = "#{bucket.bucket_host}/#{File.join(path, filename)}?append" request = BosClient::Request.new url, method: :post, params: params, headers: headers, body: File.open(file, 'r').read response = request.run response[:result] end |
Instance Method Details
#destory ⇒ Object
47 48 49 50 51 52 |
# File 'lib/bos_client/object.rb', line 47 def destory request = BosClient::Request.new "#{@bucket.bucket_host}/#{@file}", method: :delete response = request.run response[:result] end |
#save ⇒ Object
15 16 17 |
# File 'lib/bos_client/object.rb', line 15 def save save_to nil end |
#save_as(name) ⇒ Object
19 20 21 |
# File 'lib/bos_client/object.rb', line 19 def save_as(name) save_to nil, name end |
#save_to(path, name = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bos_client/object.rb', line 23 def save_to(path, name = nil) headers = { 'host' => @bucket.bucket_host } params = {} url = URI.encode("http://#{@bucket.bucket_host}/#{@file}") request = Typhoeus::Request.new url, method: :get, headers: headers, params: params request = BosClient::Authable.(request) request.on_complete do |response| flie = if path "#{path}/#{name || @name}" else (name || @name).to_s end write_file flie, response.body end request.run end |