aws-s3.rb
---------
#!/usr/bin/env ruby
require 'rubygems'
require 'aws/s3'
# File to upload
local_file = ARGV[0]
# bucket & mime-type
if local_file =~ /mp3$/
bucket = 'doug_mp3'
mime_type = 'audio/mpeg'
elsif local_file =~ /wav$/
bucket = 'doug_wav'
mime_type = 'audio/x-wav'
elsif local_file =~ /aif[c|f]?$/
bucket = 'doug_aiff'
mime_type = 'audio/x-aiff'
else
bucket = ARGV[1]
mime_type = ARGV[2] || "application/octet-stream"
end
if bucket.nil?
puts "need a bucket"
exit
end
puts "file: #{local_file}"
puts "bucket: #{bucket}"
puts "mime_type: #{mime_type}"
AWS::S3::Base.establish_connection!(
:access_key_id => 'REPLACE_ME',
:secret_access_key => 'REPLACE_ME'
)
base_name = File.basename(local_file)
puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"
AWS::S3::S3Object.store(
base_name,
File.open(local_file),
bucket,
:content_type => mime_type
)
puts "Uploaded!"
I can call the script a couple ways:
# with defaults based on extension
$ ruby aws-s3.rb mysong.mp3
# or explicitly (file, bucket, mime-type)
$ ruby aws-s3.rb mysong.mp3 doug_mp3 'audio/mp3'
No comments:
Post a Comment