class AWSUploader

Constants

AWS_REGION
BUCKET_NAME_DEFAULT
CACHE_CONTROL_MAX_AGE_DEFAULT
DOMAIN_NAME_CLOUD_FRONT
DOMAIN_NAME_S3

Attributes

cloud_front[R]
s3[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 15
def initialize(options = {})
  credentials = {}

  credentials[:access_key_id]     = get_var_from_env('AWS_ACCESS_KEY_ID')
  credentials[:secret_access_key] = get_var_from_env('AWS_SECRET_ACCESS_KEY')
  credentials[:region]            = AWS_REGION

  @s3           = Aws::S3::Client.new(credentials)
  @s3_resources = Aws::S3::Resource.new(credentials)
  @cloud_front  = Aws::CloudFront::Client.new(credentials)
end

Public Instance Methods

check_s3_object_exists(url, bucket_name = BUCKET_NAME_DEFAULT) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 213
def check_s3_object_exists(url, bucket_name = BUCKET_NAME_DEFAULT)

  key = get_key_from_url(url, bucket_name)

  s3_object = @s3_resources.bucket(bucket_name).object(key)

  s3_object.exists?
end
delete_file_from_s3(bucket_name, key) click to toggle source

remove file from s3

# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 124
def delete_file_from_s3(bucket_name, key)
  bucket_name ||= BUCKET_NAME_DEFAULT

  return unless check_s3_object_exists(key, bucket_name)

  s3_object = @s3_resources.bucket(bucket_name).object(key)

  s3_object.delete

  nil
end
delete_file_from_s3_by_uri(bucket_name, url) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 114
def delete_file_from_s3_by_uri(bucket_name, url)
  bucket_name ||= BUCKET_NAME_DEFAULT

  key = get_key_from_url(url, bucket_name)

  delete_file_from_s3(bucket_name, key)
end
get_cloud_front_url(url, use_ssl = true) click to toggle source

CloudFront

# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 142
def get_cloud_front_url(url, use_ssl = true)
  result = url.sub URI.parse(url).host, DOMAIN_NAME_CLOUD_FRONT

  if use_ssl
    result.sub! 'http:/', 'https:/'
  else
    result.sub! 'https:/', 'http:/'
  end

  result
end
get_distribution_id_by(bucket_name = BUCKET_NAME_DEFAULT) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 156
def get_distribution_id_by(bucket_name = BUCKET_NAME_DEFAULT)
  result = nil

  response = @cloud_front.list_distributions

  list_distributions = response[:items]

  list_distributions.each do |distribution|
    origin_settings    = distribution[:origins]
    origin_item_first  = origin_settings[:items].first
    origin_domain_name = origin_item_first[:domain_name]
    origin_name        = origin_domain_name.split('.s3.').first

    if origin_name == bucket_name
      result = distribution[:id]
      break
    end
  end

  result
end
get_s3_url(url, bucket_name = BUCKET_NAME_DEFAULT) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 204
def get_s3_url(url, bucket_name = BUCKET_NAME_DEFAULT)
  result = url.sub URI.parse(url).host, "#{bucket_name}.#{DOMAIN_NAME_S3}"

  result.sub! 'http:/', 'https:/'

  result
end
invalidate_distribution(distribution_id, keys) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 179
def invalidate_distribution(distribution_id, keys)

  assert(!distribution_id.nil?, 'aws:invalidate_distribution. distribution_id must be non nil')
  assert(keys.is_a?(Array), 'aws:invalidate_distribution. distribution_id must be non nil')

  keys = keys.map do |k|
    k.start_with?('/') ? k : '/' + k
  end

  paths            = {}
  paths[:items]    = keys
  paths[:quantity] = keys.size

  invalidation_batch                    = {}
  invalidation_batch[:caller_reference] = Time.now.to_i.to_s
  invalidation_batch[:paths]            = paths

  options                      = {}
  options[:distribution_id]    = distribution_id
  options[:invalidation_batch] = invalidation_batch

  response = @cloud_front.create_invalidation(options)
end
upload_data_to_s3(data_to_upload, bucket_name, path, desired_name = nil) click to toggle source

upload file to s3 and returns instance of AWSFileInfo

# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 73
def upload_data_to_s3(data_to_upload, bucket_name, path, desired_name = nil)
  upload_to_s3_internal(data_to_upload, bucket_name, path, desired_name)
end
upload_file_to_s3(file_to_upload, bucket_name, key) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 61
def upload_file_to_s3(file_to_upload, bucket_name, key)
  result = nil

  File.open(file_to_upload) do |data_to_upload|
    result = upload_to_s3_internal(data_to_upload, bucket_name, key, file_to_upload)
  end

  result
end
upload_files_to_s3(files_to_upload, bucket_name, keys) click to toggle source

upload files to s3 and returns urls map

# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 29
def upload_files_to_s3(files_to_upload, bucket_name, keys)
  bucket_name ||= BUCKET_NAME_DEFAULT

  assert(!files_to_upload.nil?, 'aws:upload_files_to_s3. Files to upload must be non nil')
  assert(!bucket_name.nil?, 'aws:upload_files_to_s3. Bucket name must be non nil')
  assert(!keys.nil? && keys.size == files_to_upload.size, 'aws:upload_files_to_s3. Desired names must be non nil and its count must be equal count of files to upload')

  result = []

  begin

    [0..files_to_upload.size - 1].each do |i|
      file_to_upload = files_to_upload[i]

      s3_key = keys[i]

      result << upload_file_to_s3(file_to_upload, bucket_name, s3_key)
    end

  rescue Exception => e

    result.each do |file_info|
      delete_file_from_s3_by_uri(bucket_name, file_info.url_s3)
    end

    assert(false, e.to_s)
  end

  result
end
upload_url_file_to_s3(url_to_upload, bucket_name, key) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 78
def upload_url_file_to_s3(url_to_upload, bucket_name, key)
  result = nil

  open(url_to_upload) do |data_to_upload|
    result = upload_to_s3_internal(data_to_upload, bucket_name, key, url_to_upload)
  end

  result.source = url_to_upload

  result
end

Private Instance Methods

get_key_from_url(url, bucket_name = nil) click to toggle source
# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 223
def get_key_from_url(url, bucket_name = nil)
  bucket_name ||= BUCKET_NAME_DEFAULT

  URI(url).path.gsub(bucket_name, '').gsub(/\/+/, '/').gsub(/^\/+/, '')
end
upload_to_s3_internal(data_to_upload, bucket_name, s3_key, source = nil) click to toggle source

@source_type - possible values :file, :data

# File lib/mrpin/core/uploaders/aws/aws_uploader.rb, line 92
def upload_to_s3_internal(data_to_upload, bucket_name, s3_key, source = nil)
  result = AWSFileInfo.new

  bucket_name ||= BUCKET_NAME_DEFAULT

  s3_object_options                 = {}
  s3_object_options[:body]          = data_to_upload
  s3_object_options[:acl]           = 'public-read'
  s3_object_options[:cache_control] = "max-age=#{CACHE_CONTROL_MAX_AGE_DEFAULT}"

  s3_object = @s3_resources.bucket(bucket_name).object(s3_key)

  s3_object.put(s3_object_options)

  result.source          = source
  result.url_s3          = s3_object.public_url
  result.url_cloud_front = get_cloud_front_url(result.url_s3)

  result
end