Class: B2::Bucket
- Inherits:
-
Object
- Object
- B2::Bucket
- Defined in:
- lib/b2/bucket.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
Instance Method Summary collapse
- #delete!(key) ⇒ Object
- #download(key, to = nil, &block) ⇒ Object
- #file(key) ⇒ Object
- #get_upload_token ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(attrs, connection) ⇒ Bucket
constructor
A new instance of Bucket.
- #upload_file(key, io_or_string, mime_type: nil, sha1: nil, content_disposition: nil, info: {}) ⇒ Object
Constructor Details
#initialize(attrs, connection) ⇒ Bucket
Returns a new instance of Bucket.
6 7 8 9 10 11 12 13 14 |
# File 'lib/b2/bucket.rb', line 6 def initialize(attrs, connection) @id = attrs['bucketId'] @name = attrs['bucketName'] @account_id = attrs['accountId'] @revision = attrs['revision'] @connection = connection end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
4 5 6 |
# File 'lib/b2/bucket.rb', line 4 def account_id @account_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/b2/bucket.rb', line 4 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/b2/bucket.rb', line 4 def name @name end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
4 5 6 |
# File 'lib/b2/bucket.rb', line 4 def revision @revision end |
Instance Method Details
#delete!(key) ⇒ Object
74 75 76 |
# File 'lib/b2/bucket.rb', line 74 def delete!(key) file(key).delete! end |
#download(key, to = nil, &block) ⇒ Object
70 71 72 |
# File 'lib/b2/bucket.rb', line 70 def download(key, to=nil, &block) @connection.download(@name, key, to, &block) end |
#file(key) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/b2/bucket.rb', line 59 def file(key) file = @connection.post('/b2api/v1/b2_list_file_names', { bucketId: @id, startFileName: key, maxFileCount: 1, prefix: key })['files'].first file ? B2::File.new(file.merge({'bucketId' => @id}), @connection) : nil end |
#get_upload_token ⇒ Object
16 17 18 |
# File 'lib/b2/bucket.rb', line 16 def get_upload_token @connection.post("/b2api/v1/b2_get_upload_url", { bucketId: @id }) end |
#has_key?(key) ⇒ Boolean
50 51 52 53 54 55 56 57 |
# File 'lib/b2/bucket.rb', line 50 def has_key?(key) !@connection.post('/b2api/v1/b2_list_file_names', { bucketId: @id, startFileName: key, maxFileCount: 1, prefix: key })['files'].empty? end |
#upload_file(key, io_or_string, mime_type: nil, sha1: nil, content_disposition: nil, info: {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/b2/bucket.rb', line 20 def upload_file(key, io_or_string, mime_type: nil, sha1: nil, content_disposition: nil, info: {}) upload = get_upload_token uri = URI.parse(upload['uploadUrl']) conn = Net::HTTP.new(uri.host, uri.port) conn.use_ssl = uri.scheme == 'https' chunker = sha1 ? io_or_string : B2::UploadChunker.new(io_or_string) req = Net::HTTP::Post.new(uri.path) req['Authorization'] = upload['authorizationToken'] req['X-Bz-File-Name'] = B2::File.encode_filename(key) req['Content-Type'] = mime_type || 'b2/x-auto' req['X-Bz-Content-Sha1'] = sha1 ? sha1 : 'hex_digits_at_end' req['X-Bz-Info-b2-content-disposition'] = content_disposition if content_disposition info.each do |key, value| req["X-Bz-Info-#{key}"] = value end req['Content-Length'] = chunker.size req.body_stream = chunker resp = conn.start { |http| http.request(req) } result = if resp.is_a?(Net::HTTPSuccess) JSON.parse(resp.body) else raise "Error connecting to B2 API" end B2::File.new(result, @connection) end |