class Bosh::Blobstore::BaseClient

Public Class Methods

new(options) click to toggle source

@param [Hash] options blobstore specific options

# File lib/blobstore_client/base.rb, line 11
def initialize(options)
  @options = Bosh::Common.symbolize_keys(options)
end

Public Instance Methods

create(contents, id = nil) click to toggle source

Saves a file or a string to the blobstore. if it is a String, it writes it to a temp file then calls #create_file() with the (temp) file @overload create(contents, id=nil)

@param [String] contents contents to upload
@param [String] id suggested object id, if nil a uuid is generated

@overload create(file, id=nil)

@param [File] file file to upload
@param [String] id suggested object id, if nil a uuid is generated

@return [String] object id of the created blobstore object

# File lib/blobstore_client/base.rb, line 25
def create(contents, id = nil)
  if contents.kind_of?(File)
    create_file(id, contents)
  else
    temp_path do |path|
      File.open(path, 'w') do |file|
        file.write(contents)
      end
      return create_file(id, File.open(path, 'r'))
    end
  end
rescue BlobstoreError => e
  raise e
rescue Exception => e
  raise BlobstoreError,
        sprintf('Failed to create object, underlying error: %s %s', e.inspect, e.backtrace.join("\n"))
end
delete(oid) click to toggle source

@return [void]

# File lib/blobstore_client/base.rb, line 68
def delete(oid)
  delete_object(oid)
end
exists?(oid) click to toggle source

@return [Boolean]

# File lib/blobstore_client/base.rb, line 73
def exists?(oid)
  object_exists?(oid)
end
get(id, file = nil, options = {}) click to toggle source

Get an object from the blobstore. @param [String] id object id @param [File] file where to store the fetched object @param [Hash] options for individual request configuration @return [String] the object contents if the file parameter is nil

# File lib/blobstore_client/base.rb, line 48
def get(id, file = nil, options = {})
  if file
    get_file(id, file)
    file.flush
  else
    result = nil
    temp_path do |path|
      File.open(path, 'w') { |f| get_file(id, f) }
      result = File.open(path, 'r') { |f| f.read }
    end
    result
  end
rescue BlobstoreError => e
  raise e
rescue Exception => e
  raise BlobstoreError,
        sprintf('Failed to fetch object, underlying error: %s %s', e.inspect, e.backtrace.join("\n"))
end

Protected Instance Methods

create_file(id, file) click to toggle source

@return [String] the id

# File lib/blobstore_client/base.rb, line 80
def create_file(id, file)
  # needs to be implemented in each subclass
  not_supported
end
delete_object(oid) click to toggle source
# File lib/blobstore_client/base.rb, line 90
def delete_object(oid)
  # needs to be implemented in each subclass
  not_supported
end
generate_object_id() click to toggle source
# File lib/blobstore_client/base.rb, line 100
def generate_object_id
  SecureRandom.uuid
end
get_file(id, file) click to toggle source
# File lib/blobstore_client/base.rb, line 85
def get_file(id, file)
  # needs to be implemented in each subclass
  not_supported
end
object_exists?(oid) click to toggle source
# File lib/blobstore_client/base.rb, line 95
def object_exists?(oid)
  # needs to be implemented in each subclass
  not_supported
end
temp_path() { |path| ... } click to toggle source
# File lib/blobstore_client/base.rb, line 104
def temp_path
  path = File.join(Dir.tmpdir, "temp-path-#{SecureRandom.uuid}")
  begin
    yield path if block_given?
    path
  ensure
    FileUtils.rm_f(path) if block_given?
  end
end

Private Instance Methods

not_supported() click to toggle source
# File lib/blobstore_client/base.rb, line 116
def not_supported
  raise NotImplemented, 'not supported by this blobstore'
end