Module: Cloudphoto::Aws

Defined in:
lib/cloudphoto/aws/aws.rb

Class Method Summary collapse

Class Method Details

.bucketObject



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

def bucket
  @bucket ||= require_env("CLOUDPHOTO_BUCKET")
end

.download_object(key:, to:) ⇒ Object



32
33
34
# 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_objectsObject



17
18
19
# 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) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
# 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_clientObject



5
6
7
# File 'lib/cloudphoto/aws/aws.rb', line 5

def s3_client
  @s3_client ||= ::Aws::S3::Client.new(region: require_env("AWS_REGION"))
end

.s3_resourceObject



9
10
11
# 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:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# 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