class Gridium::GridiumS3

Public Class Methods

new(project_name, subdirectory_path='screenshots') click to toggle source
# File lib/s3.rb, line 5
def initialize(project_name, subdirectory_path='screenshots')
  Log.debug("[GRIDIUM::S3] initializing GridiumS3 with #{project_name} and #{subdirectory_path}")
  Aws.config.update({ credentials: Aws::Credentials.new(ENV['S3_ACCESS_KEY_ID'], ENV['S3_SECRET_ACCESS_KEY']) , region: ENV['S3_DEFAULT_REGION']})
  _validate_string(project_name)
  _validate_string(subdirectory_path)

  @project_name = _sanitize_string(project_name)
  @subdirectory_path = _sanitize_string(subdirectory_path)
  @bucket = Aws::S3::Resource.new.bucket(ENV['S3_ROOT_BUCKET'])
end

Public Instance Methods

_sanitize_string(input_string) click to toggle source

@note Strips whitespace, split and join to collapse contiguous white space,

replace whitespace and special chars (not '.', '/') with an underscore
# File lib/s3.rb, line 49
def _sanitize_string(input_string)
  input_string.strip.split.join(" ").gsub(/[^\w.\/]/i, '_')
end
_validate_path(path_to_file) click to toggle source
# File lib/s3.rb, line 60
def _validate_path(path_to_file)
  Log.debug("[GRIDIUM::S3]  attempting to validate #{path_to_file} as a legitimate path")
  unless File.exist? path_to_file
    raise(ArgumentError, "[GRIDIUM::S3] this path doesn't resolve #{path_to_file}")
  end
end
_validate_string(input_string) click to toggle source
# File lib/s3.rb, line 53
def _validate_string(input_string)
  Log.debug("[GRIDIUM::S3] attempting to validate #{input_string} for use as a name")
  if input_string.empty? || input_string.strip.empty?
    raise(ArgumentError, "[GRIDIUM::S3] empty and/or whitespace file names are not wanted here.")
  end
end
_verify_upload(s3_name, local_absolute_path) click to toggle source
# File lib/s3.rb, line 67
def _verify_upload(s3_name, local_absolute_path)
  upload_size = @bucket.object(s3_name).content_length
  local_size = File.size local_absolute_path
  Log.debug("[GRIDIUM::S3] file upload verified: #{upload_size == local_size}. upload size is #{upload_size} and local size is #{local_size}")
  upload_size == local_size
end
create_s3_name(file_name) click to toggle source
# File lib/s3.rb, line 39
def create_s3_name(file_name)
  _validate_string(file_name)
  file_name = _sanitize_string(file_name)
  [@project_name, @subdirectory_path, file_name].join("/")
end
save_file(local_path) click to toggle source

Save local file to S3 bucket @param [String] local_path @return [String] s3 public url

# File lib/s3.rb, line 19
def save_file(local_path)
  Log.debug("[GRIDIUM::S3] attempting to save '#{local_path}' to s3")
  _validate_path(local_path)
  file_name = File.basename(local_path)
  destination_name = create_s3_name(file_name)
  begin
    @bucket.object(destination_name).upload_file(local_path)
    @bucket.object(destination_name).wait_until_exists
    _verify_upload(destination_name, local_path)
    # @bucket.object(s3_name).presigned_url(:get, expires_in: 3600) #uncomment this if public url ends up not working out OPREQ-83850
    return @bucket.object(destination_name).public_url
  rescue Aws::S3::Errors::InvalidAccessKeyId
    Log.error("[GRIDIUM::S3] unable to save file to s3 due to Aws::S3::Errors::InvalidAccessKeyId")
  rescue Seahorse::Client::NetworkingError => error
    Log.error("[GRIDIUM::S3] unable to save file to s3 due to underlying network error: #{error}")
  rescue StandardError => error
    Log.error("[GRIDIUM::S3] unable to save file to s3 due to unexpected error: #{error}")
  end
end