Class: Lcms::Engine::S3Service

Inherits:
Object
  • Object
show all
Defined in:
app/services/lcms/engine/s3_service.rb

Class Method Summary collapse

Class Method Details

.create_object(key) ⇒ Object



8
9
10
11
12
13
# File 'app/services/lcms/engine/s3_service.rb', line 8

def self.create_object(key)
  ::Aws::S3::Resource
    .new(region: ENV.fetch('AWS_REGION'))
    .bucket(ENV.fetch('AWS_S3_BUCKET_NAME'))
    .object(key)
end

.upload(key, data, options = {}) ⇒ String

Upload data to the specified resource by key

Parameters:

  • key (String)

    Key of the object. Usually represents the full path inside a bucket

  • data (IO|StringIO)

    The data to be uploaded

  • options (Hash) (defaults to: {})

    Additional options to be passed to Aws::S3::Object#put method

Returns:

  • (String)

    The final URL of the uploaded object



24
25
26
27
28
29
30
31
# File 'app/services/lcms/engine/s3_service.rb', line 24

def self.upload(key, data, options = {})
  object = create_object key
  options = options.merge(
    body: data
  )
  object.put(options)
  object.public_url
end

.url_for(key) ⇒ Object



33
34
35
# File 'app/services/lcms/engine/s3_service.rb', line 33

def self.url_for(key)
  create_object(key).public_url
end