Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to save a file to an AWS S3 bucket. https://www.dennismclaughlin.tech/how-to-save-a-file-to-aws-s3/
require 'aws-sdk-s3'
# Saves a file to an AWS S3 bucket.
def store_file_in_s3
Aws.config.update({
region: 'us-east-1'
credentials: Aws::Credentials.new('AWS_ACCESS_KEY_GOES_HERE', 'AWS_SECRET_KEY_GOES_HERE')
})
bucketname = 'my_s3_bucket'
localfile = '/tmp/HelloWorld.txt'
s3file = 'HelloWorldS3File.txt'
s3 = Aws::S3::Resource.new
begin
s3.bucket(bucketname).object(s3file).upload_file(localfile)
rescue StandardError => e
puts 'Couldn\'t save file to S3. ' + e.message.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment