Simple File Upload to Amazon S3 From Ruby


/ Published in: Ruby
Save to your folder(s)

Just name it s3cp (or similar), chmod it, and then you can do stuff like:

s3cp ~/somefile.whatever bucket


Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'aws/s3'
  5.  
  6. local_file = ARGV[0]
  7. bucket = ARGV[1]
  8. mime_type = ARGV[2] || "application/octet-stream"
  9.  
  10. AWS::S3::Base.establish_connection!(
  11. :access_key_id => 'REPLACE_ME',
  12. :secret_access_key => 'REPLACE_ME'
  13. )
  14.  
  15. base_name = File.basename(local_file)
  16.  
  17. puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"
  18.  
  19. AWS::S3::S3Object.store(
  20. base_name,
  21. File.open(local_file),
  22. bucket,
  23. :content_type => mime_type
  24. )
  25.  
  26. puts "Uploaded!"

URL: http://www.rubyinside.com/simple-file-upload-to-amazon-s3-from-ruby-313.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.