Skip to content

Instantly share code, notes, and snippets.

@usahg
Last active December 15, 2015 16:39
Show Gist options
  • Save usahg/5290698 to your computer and use it in GitHub Desktop.
Save usahg/5290698 to your computer and use it in GitHub Desktop.
Using only Ruby and not the Rails environment : how to upload a file from the local filesystem to a predetermined location in Amazon S3
#requiring official aws-sdk ruby gem
# stable and widely used : https://github.com/aws/aws-sdk-ruby
# last authored 8 hours from now
require 'aws-sdk'
s3 = AWS::S3.new
s3.config(
:access_key_id => ENV['ACCESS_KEY_ID'], #storing access keys on remote server as ENV Variables is a good practice
:secret_access_key => ENV['SECRET_ACCESS_KEY'] #because comitting keys to code itself is bad security
)
bucket = s3.buckets.create("foo_upload_bucket") # every S3 bucket has a name | this would be the "predetermined location"
# there are no "predetermined locations" inside a S3 bucket per se like folders
basename = File.basename(file_name)
o = b.objects[basename]
o.write(:file => file_name, :acl => :public_read) # acl is access control parameter - we can change it to private
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment