Last active
February 8, 2021 21:46
-
-
Save andersonfernandes/51fa44f3f604928ccbdc22e8785c449f to your computer and use it in GitHub Desktop.
AWS S3 storage wrapper using aws-sdk gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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