Class: BosClient::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/bos_client/object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Object

Returns a new instance of Object.



6
7
8
9
10
11
12
13
# File 'lib/bos_client/object.rb', line 6

def initialize(options = {})
  @bucket = options[:bucket]
  @size = options[:size]
  @file = options[:key]
  @path, @name =  File.split(options[:key])
  @last_modified = options[:last_modified]
  @storage_class = options[:storage_class]
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



4
5
6
# File 'lib/bos_client/object.rb', line 4

def bucket
  @bucket
end

#last_modifiedObject

Returns the value of attribute last_modified.



4
5
6
# File 'lib/bos_client/object.rb', line 4

def last_modified
  @last_modified
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/bos_client/object.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/bos_client/object.rb', line 4

def path
  @path
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/bos_client/object.rb', line 4

def size
  @size
end

#storage_classObject

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bos_client/object.rb', line 51

def self.upload options = {}
  bucket = options[:bucket]
  file = options[:file]
  origin_file_name = File.basename(file)
  filename = options[:filename] || origin_file_name
  path = options[:path] || ''
  storage_class = options[:storage_class] || "STANDARD"
  headers = options[:headers] || {}

  params = {
    append: ""
  }
  headers.merge!({"x-bce-storage-class" => storage_class})
  request = BosClient::Request.new "#{bucket.bucket_host}/#{File.join(path,filename)}?append", method: :post, params:params, headers: headers, body: File.open(file,"r").read
  response = request.run
  response[:result]
end

Instance Method Details

#destoryObject



45
46
47
48
49
# File 'lib/bos_client/object.rb', line 45

def destory
  request = BosClient::Request.new "#{@bucket.bucket_host}/#{@file}", method: :delete
  response = request.run
  response[:result]
end

#saveObject



16
17
18
# File 'lib/bos_client/object.rb', line 16

def save
  save_to nil
end

#save_as(name) ⇒ Object



20
21
22
# File 'lib/bos_client/object.rb', line 20

def save_as name
  save_to nil, name
end

#save_to(path, name = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bos_client/object.rb', line 24

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.authorize_request(request)

  request.on_complete do |response|
    if path
      flie = "#{path}/#{name || @name}"
    else
      flie = "#{name || @name}"
    end
    write_file flie, response.body
  end
  request.run
end