Class: Breadbox::S3Client

Inherits:
Client
  • Object
show all
Defined in:
lib/breadbox/s3_client.rb

Instance Attribute Summary

Attributes inherited from Client

#configuration

Instance Method Summary collapse

Methods inherited from Client

#client, #initialize, #root_path

Constructor Details

This class inherits a constructor from Breadbox::Client

Instance Method Details

#bucketObject



6
7
8
# File 'lib/breadbox/s3_client.rb', line 6

def bucket
  configuration.s3_bucket
end

#s3_bucket_objectObject



10
11
12
# File 'lib/breadbox/s3_client.rb', line 10

def s3_bucket_object
  @bucket ||= Aws::S3::Resource.new(client: s3_client_object).bucket(bucket)
end

#s3_client_objectObject



29
30
31
32
33
34
35
36
37
# File 'lib/breadbox/s3_client.rb', line 29

def s3_client_object
  @client ||= Aws::S3::Client.new(
    region: configuration.s3_region,
    credentials: Aws::Credentials.new(
      configuration.s3_access_key_id,
      configuration.s3_secret_access_key,
    )
  )
end

#upload(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/breadbox/s3_client.rb', line 14

def upload(options = {})
  path          = options[:path]
  file          = options[:file]
  acl           = options[:public] ? :public_read : :private
  content_type  = options[:content_type]
  filepath      = filepath_from_paths_and_file(root_path, path, file)[1..-1]
  s3_object     = s3_bucket_object.object(filepath)

  result = s3_object.put(body: file, acl: acl, content_type: content_type)

  if result && result.successful?
    s3_object.public_url.to_s
  end
end