Class: S3ClientFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/s3/client_factory.rb

Overview

not needed - Mutex is part of core lib: require ‘thread’

Instance Method Summary collapse

Constructor Details

#initialize(logger, options, aws_options_hash) ⇒ S3ClientFactory

Returns a new instance of S3ClientFactory.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/logstash/inputs/s3/client_factory.rb', line 6

def initialize(logger, options, aws_options_hash)
  @logger = logger
  @aws_options_hash = aws_options_hash
  @s3_default_options = Hash[options[:s3_default_options].map { |k, v| [k.to_sym, v] }]
  @aws_options_hash.merge!(@s3_default_options) unless @s3_default_options.empty?
  @sts_client = Aws::STS::Client.new(region: options[:aws_region])
  @credentials_by_bucket = options[:s3_credentials_by_bucket]
  @region_by_bucket = options[:s3_region_by_bucket]
  @logger.debug("Credentials by Bucket", :credentials => @credentials_by_bucket)
  @default_session_name = options[:s3_role_session_name]
  @clients_by_bucket = {}
  @creation_mutex = Mutex.new
end

Instance Method Details

#get_s3_client(bucket_name) {|| ... } ⇒ Object

Yields:

  • ()


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logstash/inputs/s3/client_factory.rb', line 20

def get_s3_client(bucket_name)
  bucket_symbol = bucket_name.to_sym
  @creation_mutex.synchronize do
    if @clients_by_bucket[bucket_symbol].nil?
      options = @aws_options_hash.clone
      unless @credentials_by_bucket[bucket_name].nil?
        options.merge!(credentials: get_s3_auth(@credentials_by_bucket[bucket_name]))
      end
      unless @region_by_bucket[bucket_name].nil?
        options.merge!(region: @region_by_bucket[bucket_name])
      end
      @clients_by_bucket[bucket_symbol] = Aws::S3::Client.new(options)
      @logger.debug("Created a new S3 Client", :bucket_name => bucket_name, :client => @clients_by_bucket[bucket_symbol], :used_options => options)
    end
  end
  # to be thread-safe, one uses this method like this:
  # s3_client_factory.get_s3_client(my_s3_bucket) do
  #   ... do stuff ...
  # end
  yield @clients_by_bucket[bucket_symbol]
end