Module: HasHelpers::S3Utils

Included in:
DataMigration::Base
Defined in:
lib/has_helpers/utils/s3_utils.rb

Instance Method Summary collapse

Instance Method Details

#get_s3_bucket(bucket_name) ⇒ Object

Returns the requested bucket. Raises a RuntimeError if there is an error getting that bucket.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/has_helpers/utils/s3_utils.rb', line 7

def get_s3_bucket(bucket_name)
  @s3_client ||= ::HasHelpers::Aws.new_s3_client(credentials_conf: ::HasHelpers::Aws.credentials_conf)
  @s3_resource ||= ::Aws::S3::Resource.new(client: @s3_client)

  bucket = @s3_resource.bucket(bucket_name)

  unless bucket.exists?
    raise "Cannot access S3 bucket: #{bucket_name}"
  end

  bucket
end

#get_s3_file(bucket_name, s3_file_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/has_helpers/utils/s3_utils.rb', line 20

def get_s3_file(bucket_name, s3_file_path)
  s3_object = get_s3_bucket(bucket_name).object(s3_file_path)

  begin
    file_name = File.basename(s3_object.key)
    ::Tempfile.new(file_name).tap do |temp_file|
      s3_object.get do |chunk|
        temp_file.write(chunk.dup.force_encoding("UTF-8"))
      end
      temp_file.rewind
    end
  rescue => e
    Rails.logger.error("Fetching file(/#{bucket_name}/#{s3_file_path}) in S3 encountered an issue with message: #{e.message}")
    nil
  end
end