Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Last active August 23, 2017 09:12
Show Gist options
  • Save amitpatelx/e010054d604c792bc14d9ea9cf4709c4 to your computer and use it in GitHub Desktop.
Save amitpatelx/e010054d604c792bc14d9ea9cf4709c4 to your computer and use it in GitHub Desktop.
Upload a file to Amazon S3
# assumption
# Following Environment variables are already set
# AWS_SECRET_ACCESS_KEY, AWS_REGION
# Alternative way is mentioned https://github.com/aws/aws-sdk-ruby#configuration-options
file_name = 'sample.xml'
upload_file = '/path/to/sample.xml'
# Create an instance of the Aws::S3::Resource class
s3 = Aws::S3::Resource.new
# Reference the target object by bucket name and key.
# Objects live in a bucket and have unique keys that identify the object.
obj = s3.bucket('your-bucket-name').object(file_name)
obj.upload_file(upload_file, { acl: 'public-read' }) # http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
# Returns Public URL to the file
obj.public_url
# Reference http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpRuby.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment