Class: Bosh::Blobstore::SimpleBlobstoreClient

Inherits:
BaseClient show all
Defined in:
lib/blobstore_client/simple_blobstore_client.rb

Constant Summary

Constants inherited from Client

Client::PROVIDER_NAMES, Client::VERSION

Instance Method Summary collapse

Methods inherited from BaseClient

#create, #delete, #exists?, #get

Methods inherited from Client

create, safe_create

Constructor Details

#initialize(options) ⇒ SimpleBlobstoreClient

Returns a new instance of SimpleBlobstoreClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 10

def initialize(options)
  super(options)
  @client = HTTPClient.new
  @endpoint = @options[:endpoint]
  @bucket = @options[:bucket] || 'resources'
  @headers = {}
  user = @options[:user]
  password = @options[:password]
  if user && password
    @headers['Authorization'] = 'Basic ' +
      Base64.encode64("#{user}:#{password}").strip
  end
end

Instance Method Details

#create_file(id, file) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 28

def create_file(id, file)
  response = @client.post(url(id), { content: file }, @headers)
  if response.status != 200
    raise BlobstoreError,
          "Could not create object, #{response.status}/#{response.content}"
  end
  response.content
end

#delete_object(id) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 48

def delete_object(id)
  response = @client.delete(url(id), @headers)
  if response.status != 204
    raise BlobstoreError,
          "Could not delete object, #{response.status}/#{response.content}"
  end
end

#get_file(id, file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 37

def get_file(id, file)
  response = @client.get(url(id), {}, @headers) do |block|
    file.write(block)
  end

  if response.status != 200
    raise BlobstoreError,
          "Could not fetch object, #{response.status}/#{response.content}"
  end
end

#object_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 56

def object_exists?(id)
  response = @client.head(url(id), header: @headers)
  if response.status == 200
    true
  elsif response.status == 404
    false
  else
    raise BlobstoreError, "Could not get object existence, #{response.status}/#{response.content}"
  end
end

#url(id = nil) ⇒ Object



24
25
26
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 24

def url(id = nil)
  ["#{@endpoint}/#{@bucket}", id].compact.join('/')
end