Module: S3Secure::AwsServices::S3

Extended by:
Memoist
Included in:
S3Secure::AwsServices
Defined in:
lib/s3_secure/aws_services/s3.rb

Constant Summary collapse

@@s3_clients =

holds cached s3 regional clients cache

{}
@@region_map =

bucket to region map cache

{}

Instance Method Summary collapse

Instance Method Details

#check_bucket!Object



36
37
38
39
40
41
42
# File 'lib/s3_secure/aws_services/s3.rb', line 36

def check_bucket!
  # IMPORANT: The class that includes this module must set @bucket before using the s3 method.
  unless @bucket
    raise "@bucket #{@bucket.inspect} is not set. The class must set @bucket before using the any client method."
  end
  region_map # triggers building region map for specific @bucket
end

#new_s3_regional_clientObject



18
19
20
21
22
23
24
25
26
# File 'lib/s3_secure/aws_services/s3.rb', line 18

def new_s3_regional_client
  options = {}
  options[:endpoint] = "https://s3.#{region}.amazonaws.com"
  options[:region] = region
  Aws::S3::Client.new(options)
rescue Aws::STS::Errors::RegionDisabledException
  puts "ERROR: Fail to establish client connection to region #{region}".color(:red)
  raise
end

#regionObject



57
58
59
# File 'lib/s3_secure/aws_services/s3.rb', line 57

def region
  region_map[@bucket]
end

#region_mapObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/s3_secure/aws_services/s3.rb', line 45

def region_map
  region = @@region_map[@bucket]
  return @@region_map if region # return cache

  # build cache
  resp = s3_client.get_bucket_location(bucket: @bucket)
  region = resp.location_constraint
  region = 'us-east-1' if region.empty? # "" means us-east-1
  @@region_map[@bucket] = region
  @@region_map
end

#s3Object



6
7
8
9
# File 'lib/s3_secure/aws_services/s3.rb', line 6

def s3
  check_bucket!
  @@s3_clients[@bucket] ||= new_s3_regional_client
end

#s3_clientObject

Generic s3 client. Will be configured to whatever region user has locally configured in ~/.aws/config Used to call get_bucket_location to get each specific bucket’s location. Generally use the s3_regional_client instead of this.



31
32
33
# File 'lib/s3_secure/aws_services/s3.rb', line 31

def s3_client
  Aws::S3::Client.new
end

#s3_regional_client(bucket) ⇒ Object



11
12
13
14
15
16
# File 'lib/s3_secure/aws_services/s3.rb', line 11

def s3_regional_client(bucket)
  temp = @bucket
  @bucket = bucket
  @@s3_clients[bucket] ||= new_s3_regional_client
  @bucket = temp
end