Skip to content

Instantly share code, notes, and snippets.

@dennismclaugh
Last active April 8, 2019 19:59
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 dennismclaugh/659cd9416ca6e50222545cece595475f to your computer and use it in GitHub Desktop.
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/
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