module Cloudphoto::Aws

Public Class Methods

bucket() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 13
def bucket
  @bucket ||= require_env("CLOUDPHOTO_BUCKET")
end
download_object(key:, to:) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 32
def download_object(key:, to:)
  s3_client.get_object({ bucket: bucket, key: key }, target: to)
end
list_objects() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 17
def list_objects
  s3_client.list_objects_v2(bucket: bucket).contents.map(&:key)
end
require_env(key) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 36
def require_env(key)
  return ENV[key] if ENV[key]

  raise ArgumentError.new("Missing environment variable #{key}")
end
s3_client() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 5
def s3_client
  @s3_client ||= ::Aws::S3::Client.new(region: require_env("AWS_REGION"))
end
s3_resource() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 9
def s3_resource
  @s3_resource ||= ::Aws::S3::Resource.new(client: s3_client)
end
upload_object(file_path, to:) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 21
def upload_object(file_path, to:)
  s3_client.put_object(bucket: bucket, key: to)
  object = s3_resource.bucket(bucket).object(to)
  File.open(file_path, 'rb') do |file|
    object.put(body: file)
  end
rescue StandardError => e
  puts e
  false
end

Private Instance Methods

bucket() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 13
def bucket
  @bucket ||= require_env("CLOUDPHOTO_BUCKET")
end
download_object(key:, to:) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 32
def download_object(key:, to:)
  s3_client.get_object({ bucket: bucket, key: key }, target: to)
end
list_objects() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 17
def list_objects
  s3_client.list_objects_v2(bucket: bucket).contents.map(&:key)
end
require_env(key) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 36
def require_env(key)
  return ENV[key] if ENV[key]

  raise ArgumentError.new("Missing environment variable #{key}")
end
s3_client() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 5
def s3_client
  @s3_client ||= ::Aws::S3::Client.new(region: require_env("AWS_REGION"))
end
s3_resource() click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 9
def s3_resource
  @s3_resource ||= ::Aws::S3::Resource.new(client: s3_client)
end
upload_object(file_path, to:) click to toggle source
# File lib/cloudphoto/aws/aws.rb, line 21
def upload_object(file_path, to:)
  s3_client.put_object(bucket: bucket, key: to)
  object = s3_resource.bucket(bucket).object(to)
  File.open(file_path, 'rb') do |file|
    object.put(body: file)
  end
rescue StandardError => e
  puts e
  false
end