Class: Bosh::Blobstore::S3cliBlobstoreClient

Inherits:
BaseClient show all
Defined in:
lib/blobstore_client/s3cli_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) ⇒ S3cliBlobstoreClient

Note:

If access_key_id and secret_access_key are not present, the blobstore client operates in read only mode

Blobstore client for S3, using s3cli Go version

Parameters:

  • options (Hash)

    S3connection options

Options Hash (options):

  • bucket_name (Symbol)

    key that is applied before the object is sent to S3

  • access_key_id (Symbol, optional)
  • secret_access_key (Symbol, optional)
  • s3cli_path (Symbol)

    path to s3cli binary

  • s3cli_config_path (Symbol, optional)

    path to store configuration files



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
49
50
51
52
53
# File 'lib/blobstore_client/s3cli_blobstore_client.rb', line 24

def initialize(options)
  super(options)

  @s3cli_path = @options.fetch(:s3cli_path)
  unless Kernel.system("#{@s3cli_path} --v", out: "/dev/null", err: "/dev/null")
    raise BlobstoreError, "Cannot find s3cli executable. Please specify s3cli_path parameter"
  end

  @s3cli_options = {
    bucket_name: @options[:bucket_name],
    use_ssl: @options.fetch(:use_ssl, true),
    host: @options[:host],
    port: @options[:port],
    region: @options[:region],
    ssl_verify_peer:  @options.fetch(:ssl_verify_peer, true),
    credentials_source: @options.fetch(:credentials_source, 'none'),
    access_key_id: @options[:access_key_id],
    secret_access_key: @options[:secret_access_key],
    signature_version: @options[:signature_version]
  }

  @s3cli_options.reject! {|k,v| v.nil?}

  if  @options[:access_key_id].nil? &&
      @options[:secret_access_key].nil?
        @options[:credentials_source] = 'none'
  end

  @config_file = write_config_file(@options.fetch(:s3cli_config_path, nil))
end

Instance Method Details

#create_file(object_id, file) ⇒ Object

Parameters:

  • file (File)

    file to store in S3



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

def create_file(object_id, file)
  object_id ||= generate_object_id
  # in Ruby 1.8 File doesn't respond to :path
  path = file.respond_to?(:path) ? file.path : file

  store_in_s3(path, full_oid_path(object_id))

  object_id
end

#delete_object(object_id) ⇒ Object

Parameters:

  • object_id (String)

    object id to delete

Raises:



78
79
80
81
82
83
84
85
# File 'lib/blobstore_client/s3cli_blobstore_client.rb', line 78

def delete_object(object_id)
  begin
    out, err, status = Open3.capture3("#{@s3cli_path} -c #{@config_file} delete #{object_id}")
  rescue Exception => e
    raise BlobstoreError, e.inspect
  end
  raise BlobstoreError, "Failed to delete S3 object, code #{status.exitstatus}, output: '#{out}', error: '#{err}'" unless status.success?
end

#get_file(object_id, file) ⇒ Object

Parameters:

  • object_id (String)

    object id to retrieve

  • file (File)

    file to store the retrived object in

Raises:



68
69
70
71
72
73
74
75
# File 'lib/blobstore_client/s3cli_blobstore_client.rb', line 68

def get_file(object_id, file)
  begin
  out, err, status = Open3.capture3("#{@s3cli_path} -c #{@config_file} get #{object_id} #{file.path}")
  rescue Exception => e
    raise BlobstoreError, e.inspect
  end
  raise BlobstoreError, "Failed to download S3 object, code #{status.exitstatus}, output: '#{out}', error: '#{err}'" unless status.success?
end

#object_exists?(object_id) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/blobstore_client/s3cli_blobstore_client.rb', line 87

def object_exists?(object_id)
  begin
    out, err, status = Open3.capture3("#{@s3cli_path} -c #{@config_file} exists #{object_id}")
    if status.exitstatus == 0
      return true
    end
    if status.exitstatus == 3
      return false
    end
  rescue Exception => e
    raise BlobstoreError, e.inspect
  end
  raise BlobstoreError, "Failed to check existence of S3 object, code #{status.exitstatus}, output: '#{out}', error: '#{err}'" unless status.success?
end