Skip to content

Instantly share code, notes, and snippets.

@andersonfernandes
Last active February 8, 2021 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfernandes/51fa44f3f604928ccbdc22e8785c449f to your computer and use it in GitHub Desktop.
Save andersonfernandes/51fa44f3f604928ccbdc22e8785c449f to your computer and use it in GitHub Desktop.
AWS S3 storage wrapper using aws-sdk gem
require 'aws-sdk'
credentials = Aws::Credentials.new(ENV['S3_ACCESS_KEY'], ENV['S3_SECRET_ACCESS_KEY'])
Aws.config.update(
region: ENV['S3_REGION'],
credentials: credentials
)
s3 = Aws::S3::Resource.new(ENV['S3_REGION'], credentials: credentials)
S3_BUCKET = s3.bucket(ENV['S3_BUCKET'])
require 'aws-sdk'
module Utils
class AwsStorage
def upload_and_get_key(file)
key = generate_object_key
object = S3_BUCKET.object("#{bucket_folder}/#{key}")
object.upload_file(file)
key
end
def file_url(file_key)
object = S3_BUCKET.object("store/#{file_key}")
object.presigned_url(:get, expires_in: 604800)
end
private
def generate_object_key
::SecureRandom.hex(32)
end
def bucket_folder
'store'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment