class BFS::Blob

Blobs are references to single blob objects within a bucket.

Attributes

path[R]

Public Class Methods

new(url) click to toggle source
# File lib/bfs/blob.rb, line 19
def initialize(url)
  url = url.is_a?(::URI) ? url.dup : URI.parse(url)
  @path = BFS.norm_path(url.path)

  url.path = '/'
  @bucket = BFS.resolve(url)

  BFS.defer(self, :close)
end
open(url) { |blob| ... } click to toggle source

Behaves like new, but accepts an optional block. If a block is given, blobs are automatically closed after the block is yielded.

# File lib/bfs/blob.rb, line 8
def self.open(url)
  blob = new(url)
  return blob unless block_given?

  begin
    yield blob
  ensure
    blob.close
  end
end

Public Instance Methods

close() click to toggle source

Closes the underlying bucket connection.

# File lib/bfs/blob.rb, line 71
def close
  @bucket.close
end
create(**opts, &block) click to toggle source

Creates the blob and opens it for writing. If a block is passed the writer is automatically committed in the end. If no block is passed, you must manually call commit to persist the result.

# File lib/bfs/blob.rb, line 38
def create(**opts, &block)
  @bucket.create(path, **opts, &block)
end
info(**opts) click to toggle source

Info returns the blob info.

# File lib/bfs/blob.rb, line 30
def info(**opts)
  @bucket.info(path, **opts)
end
mv(dst, **opts) click to toggle source

Moves blob to dst.

# File lib/bfs/blob.rb, line 64
def mv(dst, **opts)
  dst = BFS.norm_path(dst)
  @bucket.mv(path, dst, **opts)
  @path = dst
end
open(**opts, &block) click to toggle source

Opens the blob for reading. May raise BFS::FileNotFound.

# File lib/bfs/blob.rb, line 44
def open(**opts, &block)
  @bucket.open(path, **opts, &block)
end
read(**opts) click to toggle source

Shortcut method to read the contents of the blob.

# File lib/bfs/blob.rb, line 54
def read(**opts)
  self.open(**opts, &:read)
end
rm(**opts) click to toggle source

Deletes the blob.

# File lib/bfs/blob.rb, line 49
def rm(**opts)
  @bucket.rm(path, **opts)
end
write(data, **opts) click to toggle source

Shortcut method to write data to blob.

# File lib/bfs/blob.rb, line 59
def write(data, **opts)
  create(**opts) {|f| f.write data }
end