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

.fetch(options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bos_client/object.rb', line 78

def self.fetch(options = {})
  bucket = options[:bucket]
  filename = options[:filename]
  path = options[:path] || ''
  storage_class = options[:storage_class] || 'STANDARD'
  fetch_mode = options[:fetch_mode] || 'sync'
  fetch_source = options[:fetch_source]
  headers = options[:headers] || {}

  params = {
    fetch: ''
  }

  headers['x-bce-storage-class'] = storage_class
  headers['x-bce-fetch-mode'] = fetch_mode
  headers['x-bce-fetch-source'] = fetch_source
  url = "#{bucket.bucket_host}/#{File.join(path, filename)}?fetch"
  request = BosClient::Request.new  url,
                                    method: :post,
                                    params: params,
                                    headers: headers
  response = request.run
  response[:result]
end

.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(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['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

#destoryObject



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

#saveObject



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