Class: LogStash::Outputs::Gcs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/gcs/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket, json_key_path, logger) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
# File 'lib/logstash/outputs/gcs/client.rb', line 17

def initialize(bucket, json_key_path, logger)
  @logger = logger
  @bucket = bucket

  # create client
  @storage = initialize_storage(json_key_path)
end

Instance Method Details

#initialize_storage(json_key_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/logstash/outputs/gcs/client.rb', line 41

def initialize_storage(json_key_path)
  @logger.info("Initializing Google API client, key: #{json_key_path}")

  StorageOptions.newBuilder
      .setCredentials(credentials(json_key_path))
      .setHeaderProvider(http_headers)
      .setRetrySettings(retry_settings)
      .build
      .getService
end

#upload_object(file_path, content_encoding, content_type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/logstash/outputs/gcs/client.rb', line 25

def upload_object(file_path, content_encoding, content_type)
  input = FileInputStream.new(file_path)

  blob_name = ::File.basename(file_path)
  blob_info = com.google.cloud.storage.BlobInfo.newBuilder(@bucket, blob_name)
                  .setContentEncoding(content_encoding)
                  .setContentType(content_type)
                  .build

  @logger.info("Uploading file to #{@bucket}/#{blob_name}")
  @storage.create(blob_info, input)

  input.close
  @logger.info("Uploaded file to #{@bucket}/#{blob_name}")
end