Skip to content

Instantly share code, notes, and snippets.

@vikas027
Last active August 29, 2015 14:20
Show Gist options
  • Save vikas027/be1010dd13f70ecd2f5e to your computer and use it in GitHub Desktop.
Save vikas027/be1010dd13f70ecd2f5e to your computer and use it in GitHub Desktop.
Upload file on AWS S3 using Ruby SDK v2
#!/usr/local/rvm/rubies/ruby-2.1.5/bin/ruby
# This needs to be run as ./script_name source/file/path
# I recommend using relative paths than absolute paths
require 'aws-sdk' # gem install aws-sdk
# Change these variables as per your environment
bucket_name = 'vikas-bucket'
sub_bucket_name = 'vikas/reachvikas.com'
myregion = 'us-west-1'
access_key = 'YOUR_ACCESS_KEY'
secret_access_key = 'YOUR_SECRET_ACCESS_KEY'
file_name = ARGV[0]
file_name_key = [ sub_bucket_name, file_name].join("/")
s3 = Aws::S3::Resource.new(
credentials: Aws::Credentials.new(access_key, secret_access_key),
region: myregion
)
puts "Uploading #{file_name} of #{File.size(file_name)} byte(s) to #{bucket_name}/#{sub_bucket_name} ...\n"
obj = s3.bucket(bucket_name).object(file_name_key)
obj.upload_file(file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment