Class: Aliyun::Oss::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyunoss/bucket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Bucket

Returns a new instance of Bucket.



7
8
9
10
11
12
13
# File 'lib/aliyunoss/bucket.rb', line 7

def initialize(params = {})
  params.each_pair {|k,v| send("#{k.to_s}=", v) }
  if String === @creation_date
    @creation_date =~ /(\d+) ([a-zA-Z]+) (\d+) (\d\d):(\d\d):(\d\d)/
    @creation_date = Time.gm($3, $2, $1, $4, $5, $6)
  end
end

Instance Attribute Details

#creation_dateObject

Returns the value of attribute creation_date.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def creation_date
  @creation_date
end

#domainObject

Returns the value of attribute domain.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def domain
  @domain
end

#extranet_endpointObject

Returns the value of attribute extranet_endpoint.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def extranet_endpoint
  @extranet_endpoint
end

#intranet_endpointObject

Returns the value of attribute intranet_endpoint.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def intranet_endpoint
  @intranet_endpoint
end

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def location
  @location
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/aliyunoss/bucket.rb', line 5

def name
  @name
end

Class Method Details

.allObject

Class method - List all buckets in my account



18
19
20
# File 'lib/aliyunoss/bucket.rb', line 18

def self.all
  Aliyun::Oss::API.list_bucket.raise_unless(Net::HTTPOK).to_buckets
end

.create(name, location = 'oss-cn-hangzhou') ⇒ Object

Class method - Create a new bucket



25
26
27
28
# File 'lib/aliyunoss/bucket.rb', line 25

def self.create(name, location = 'oss-cn-hangzhou')
  Aliyun::Oss::API.put_bucket(name, location).raise_unless(Net::HTTPOK)
  Bucket.new(:name => name,  :location=> location, :creation_date => Time.now)
end

Instance Method Details

#delete(path) ⇒ Object

Delete remote file



111
112
113
# File 'lib/aliyunoss/bucket.rb', line 111

def delete(path)
  Aliyun::Oss::API.delete_object(self, path).raise_unless(Net::HTTPNoContent)
end

#delete!Object

delete this bucket



170
171
172
# File 'lib/aliyunoss/bucket.rb', line 170

def delete!
  Aliyun::Oss::API.delete_bucket(self).raise_unless(Net::HTTPNoContent)
end

#direct_upload_headers(path, filename: nil, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/aliyunoss/bucket.rb', line 91

def direct_upload_headers(path, filename: nil, 
                       content_type:, content_length:, checksum:, custom_metadata: {})
  Aliyun::Oss::API.headers_for_upload(self, path, 
                                      filename: filename,
                                      content_type: content_type,
                                      content_length: content_length,
                                      checksum: checksum,
                                      custom_metadata: )
end

#disable_loggingObject



174
175
176
# File 'lib/aliyunoss/bucket.rb', line 174

def disable_logging
  Aliyun::Oss::API.delete_logging(self).raise_unless(Net::HTTPNoContent)
end

#disable_website_accessObject



186
187
188
# File 'lib/aliyunoss/bucket.rb', line 186

def disable_website_access
  Aliyun::Oss::API.delete_website(self).raise_unless(Net::HTTPNoContent)
end

#download(path, options = {}) ⇒ Object

Download file from remote server



47
48
49
50
51
52
53
54
# File 'lib/aliyunoss/bucket.rb', line 47

def download(path, options = {})
  response = Aliyun::Oss::API.get_object(self, path, options)
  if response.code.to_i >= 200 and response.code.to_i < 300
    response.body
  else
    raise OssException.new(response)
  end
end

#enable_logging(target_bucket_name, log_prefix) ⇒ Object



178
179
180
# File 'lib/aliyunoss/bucket.rb', line 178

def enable_logging(target_bucket_name, log_prefix)
  Aliyun::Oss::API.enable_bucket_logging(self, target_bucket_name, log_prefix).raise_unless(Net::HTTPOK)
end

#enable_website_access(index_page, error_page) ⇒ Object



190
191
192
# File 'lib/aliyunoss/bucket.rb', line 190

def enable_website_access(index_page, error_page)
  Aliyun::Oss::API.put_bucket_website(self, index_page, error_page).raise_unless(Net::HTTPOK)
end

#exist?(path) ⇒ Boolean

Test if a file exist return true/false

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aliyunoss/bucket.rb', line 71

def exist?(path)
  begin
    Aliyun::Oss::API.head_object(self, path).raise_unless(Net::HTTPOK)
    true
  rescue OssException => ex
    if (ex.message.include? 'Net::HTTPNotFound') 
      false
    else
      raise ex
    end
  end
end

#get_aclObject



202
203
204
# File 'lib/aliyunoss/bucket.rb', line 202

def get_acl
  Aliyun::Oss::API.get_bucket_acl(self).raise_unless(Net::HTTPOK).to_acl_status
end

#get_file_info(path) ⇒ Object

Get file info



59
60
61
62
63
64
65
# File 'lib/aliyunoss/bucket.rb', line 59

def get_file_info(path)
  hash = Hash.new
  Aliyun::Oss::API.head_object(self, path).raise_unless(Net::HTTPOK).each_header do |k,v|
    hash[k] = v
  end
  hash
end

#list_files(options = {}) ⇒ Object

List all files in an bucket



33
34
35
# File 'lib/aliyunoss/bucket.rb', line 33

def list_files(options = {})
  Aliyun::Oss::API.list_object(self, options).raise_unless(Net::HTTPOK).to_objects
end

#logging_statusObject



182
183
184
# File 'lib/aliyunoss/bucket.rb', line 182

def logging_status
  Aliyun::Oss::API.get_bucket_logging(self).raise_unless(Net::HTTPOK).to_logging_status
end

#multipart_abort(path, upload_id) ⇒ Object



124
125
126
127
# File 'lib/aliyunoss/bucket.rb', line 124

def multipart_abort(path, upload_id)
  Aliyun::Oss::API.multipart_upload_abort(self, path, upload_id)
    .raise_unless(Net::HTTPNoContent)
end

#multipart_copy_from(source_bucket, source_path, size, range = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/aliyunoss/bucket.rb', line 139

def multipart_copy_from(source_bucket, source_path, size, range = nil)
  response = Aliyun::Oss::API.multipart_upload_from_copy(@multipart_id,
                                                         source_bucket, source_path,
                                                         self, @multipart_path,
                                                         @multipart_sequence,
                                                         size, range)
  response.raise_unless(Net::HTTPOK)
  @multipart_list[@multipart_sequence] = response['ETag']
  @multipart_sequence = @multipart_sequence + 1        
end

#multipart_pendingObject

Multipart upload and copy



118
119
120
121
122
# File 'lib/aliyunoss/bucket.rb', line 118

def multipart_pending
  Aliyun::Oss::API.multipart_upload_unfinished_task(self)
    .raise_unless(Net::HTTPOK)
    .to_mutlipart_task
end

#multipart_upload(data) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/aliyunoss/bucket.rb', line 129

def multipart_upload(data)
  raise 'You must call multipart_upload_initiate before upload data.' unless @multipart_path
  response = Aliyun::Oss::API.multipart_upload_part(self, @multipart_path,
                                         @multipart_id,
                                         data, @multipart_sequence)
  response.raise_unless(Net::HTTPOK)
  @multipart_list[@multipart_sequence] = response['ETag']
  @multipart_sequence = @multipart_sequence + 1
end

#multipart_upload_completeObject



159
160
161
162
163
164
165
# File 'lib/aliyunoss/bucket.rb', line 159

def multipart_upload_complete
  Aliyun::Oss::API.multipart_upload_complete(self, @multipart_path,
                                             @multipart_id,
                                             @multipart_list)
    .raise_unless(Net::HTTPOK)
  @multipart_path = nil
end

#multipart_upload_initiate(path) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/aliyunoss/bucket.rb', line 150

def multipart_upload_initiate(path)
  @multipart_path = path
  @multipart_id = Aliyun::Oss::API.multipart_upload_initiate(self, path)
                  .raise_unless(Net::HTTPOK)
                  .to_multipart_id
  @multipart_list = {}
  @multipart_sequence = 1
end

#public_url(path) ⇒ Object

Generate public url for path



104
105
106
# File 'lib/aliyunoss/bucket.rb', line 104

def public_url(path)
  "https://#{name}.#{location}.aliyuncs.com#{path}"
end

#set_acl(permission) ⇒ Object



198
199
200
# File 'lib/aliyunoss/bucket.rb', line 198

def set_acl(permission)
  Aliyun::Oss::API.put_bucket_acl(self, permission).raise_unless(Net::HTTPOK)
end

#share(path, expires_in = 3600) ⇒ Object

Generate a url that can be shared to others



87
88
89
# File 'lib/aliyunoss/bucket.rb', line 87

def share(path, expires_in = 3600)
  Aliyun::Oss::API.generate_share_url(self, path, expires_in)
end

#upload(data, path, options = {}) ⇒ Object

Upload data to bucket



40
41
42
# File 'lib/aliyunoss/bucket.rb', line 40

def upload(data, path, options = {})
  Aliyun::Oss::API.put_object(self, path, data, options).raise_unless(Net::HTTPOK)
end

#website_access_statusObject



194
195
196
# File 'lib/aliyunoss/bucket.rb', line 194

def website_access_status
  Aliyun::Oss::API.get_bucket_website(self).raise_unless(Net::HTTPOK).to_website_status
end