Class: RailsS3Uploader::S3Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_s3_uploader.rb

Instance Method Summary collapse

Constructor Details

#initializeS3Uploader

Returns a new instance of S3Uploader.



4
5
6
7
8
9
10
11
# File 'lib/rails_s3_uploader.rb', line 4

def initialize
  @s3_client = Aws::S3::Client.new(
    access_key_id: RailsS3Uploader.configuration.access_key_id,
    secret_access_key: RailsS3Uploader.configuration.secret_access_key,
    region: RailsS3Uploader.configuration.region
  )
  @bucket = RailsS3Uploader.configuration.bucket
end

Instance Method Details

#upload(file, key) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/rails_s3_uploader.rb', line 13

def upload(file, key)
  obj = Aws::S3::Object.new(bucket_name: @bucket, key: key, client: @s3_client)
  obj.upload_file(file.path)
  obj.public_url
rescue StandardError => e
  puts "Error uploading file: #{e.message}"
  nil
end