Class: AwsClient::S3Wrapper

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/s3_wrapper.rb

Constant Summary collapse

BUCKET_DEFINITION_FILE =
".aws_buckets"
PATH_TO_BUCKETS_DEFINITION =
File.join(Dir.home, BUCKET_DEFINITION_FILE)

Instance Attribute Summary

Attributes inherited from Wrapper

#client

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ S3Wrapper

Returns a new instance of S3Wrapper.



7
8
9
10
11
# File 'lib/s3_wrapper.rb', line 7

def initialize(*params)
  super
  @credentials_key = params[0][:credentials_key]
  @aws_region = params[0][:region]
end

Instance Method Details

#bucket_name_for_function(bucket_function) ⇒ Object



58
59
60
# File 'lib/s3_wrapper.rb', line 58

def bucket_name_for_function(bucket_function)
  return "#{bucket_function}.#{@credentials_key}.#{@aws_region}"  
end

#bucket_object_for_bucket_prefix(bucket_prefix_code) ⇒ Object



17
18
19
20
# File 'lib/s3_wrapper.rb', line 17

def bucket_object_for_bucket_prefix(bucket_prefix_code)
  bucket = find_or_create_bucket_for(bucket_prefix_code)
  return bucket.objects.collect{|object| object.object } # 'object.object' The S3 object belonging to the S3 resource    
end

#bucket_prefixesObject



62
63
64
# File 'lib/s3_wrapper.rb', line 62

def bucket_prefixes
  YAML.load_file(PATH_TO_BUCKETS_DEFINITION)
end

#bucketsObject



48
49
50
# File 'lib/s3_wrapper.rb', line 48

def buckets
  return resource.buckets
end

#find_bucket_by(bucket_name) ⇒ Object



22
23
24
# File 'lib/s3_wrapper.rb', line 22

def find_bucket_by(bucket_name)
  buckets.select{|bucket| bucket.name == bucket_name }.first
end

#find_or_create_bucket_for(bucket_prefix_code) ⇒ Object



52
53
54
55
56
# File 'lib/s3_wrapper.rb', line 52

def find_or_create_bucket_for(bucket_prefix_code)
  bucket_prefix = bucket_prefixes[bucket_prefix_code]
  full_bucket_name = bucket_name_for_function(bucket_prefix) 
  return find_or_create_bucket(full_bucket_name)
end

#resourceObject



13
14
15
# File 'lib/s3_wrapper.rb', line 13

def resource
  @resource ||= Aws::S3::Resource.new(client: client)
end

#write_file_to_bucket(bucket_prefix_code, file_path) ⇒ Object



36
37
38
39
40
# File 'lib/s3_wrapper.rb', line 36

def write_file_to_bucket(bucket_prefix_code, file_path)
  bucket = find_or_create_bucket_for(bucket_prefix_code)
  s3_object = write_to_bucket(bucket, file_path)
  return s3_object
end

#write_file_to_explicit_bucket_name(full_bucket_name, file_path) ⇒ Object



42
43
44
45
46
# File 'lib/s3_wrapper.rb', line 42

def write_file_to_explicit_bucket_name(full_bucket_name, file_path)
  bucket = find_or_create_bucket(full_bucket_name)
  s3_object = write_to_bucket(bucket, file_path)
  return s3_object
end

#write_to_bucket(bucket, file_path) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/s3_wrapper.rb', line 26

def write_to_bucket(bucket, file_path)
  s3_object = bucket.put_object(
    :key => File.basename(file_path),
    :body => IO.read(file_path)
  )
  puts "\nRemote file"
  puts "#{s3_object.public_url}"
  return s3_object
end