Class: Bosh::Blobstore::BaseClient

Inherits:
Client
  • Object
show all
Defined in:
lib/blobstore_client/base.rb

Constant Summary

Constants inherited from Client

Client::PROVIDER_NAMES, Client::VERSION

Instance Method Summary collapse

Methods inherited from Client

create, safe_create

Constructor Details

#initialize(options) ⇒ BaseClient

Returns a new instance of BaseClient.

Parameters:

  • options (Hash)

    blobstore specific options



11
12
13
# File 'lib/blobstore_client/base.rb', line 11

def initialize(options)
  @options = Bosh::Common.symbolize_keys(options)
end

Instance Method Details

#create(contents, id = nil) ⇒ String #create(file, id = nil) ⇒ String

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

Overloads:

  • #create(contents, id = nil) ⇒ String

    Parameters:

    • contents (String)

      contents to upload

    • id (String) (defaults to: nil)

      suggested object id, if nil a uuid is generated

  • #create(file, id = nil) ⇒ String

    Parameters:

    • file (File)

      file to upload

    • id (String) (defaults to: nil)

      suggested object id, if nil a uuid is generated

Returns:

  • (String)

    object id of the created blobstore object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ void

This method returns an undefined value.



68
69
70
# File 'lib/blobstore_client/base.rb', line 68

def delete(oid)
  delete_object(oid)
end

#exists?(oid) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/blobstore_client/base.rb', line 73

def exists?(oid)
  object_exists?(oid)
end

#get(id, file = nil, options = {}) ⇒ String

Get an object from the blobstore.

Parameters:

  • id (String)

    object id

  • file (File) (defaults to: nil)

    where to store the fetched object

  • options (Hash) (defaults to: {})

    for individual request configuration

Returns:

  • (String)

    the object contents if the file parameter is nil



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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