Last active
April 8, 2019 19:59
-
-
Save dennismclaugh/659cd9416ca6e50222545cece595475f to your computer and use it in GitHub Desktop.
How to save a file to an AWS S3 bucket. https://www.dennismclaughlin.tech/how-to-save-a-file-to-aws-s3/
This file contains 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-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