Created
October 24, 2010 17:48
-
-
Save thesurlydev/643716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'aws/s3' | |
require 'fileutils' | |
ACCESS_KEY_ID = "put yours here" | |
SECRET_ACCESS_KEY = "put yours here" | |
ARCHIVE_DIR = "/Users/shane/upload_archive" | |
UPLOAD_DIR = "/Users/shane/upload" | |
BUCKET = "static.digitalsanctum.com" | |
KEY_PREFIX = "images/" | |
class Upload | |
def initialize | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => ACCESS_KEY_ID, | |
:secret_access_key => SECRET_ACCESS_KEY | |
) | |
end | |
def put(local_file) | |
base_name = File.basename(local_file) | |
mime_type = mime_type(local_file) | |
AWS::S3::S3Object.store( | |
"#{KEY_PREFIX}#{base_name}", | |
File.open(local_file), | |
BUCKET, | |
:content_type => mime_type, | |
:access => :public_read | |
) | |
puts "Upload completed" | |
end | |
def mime_type(f) | |
mimetype = `file --mime-type --brief #{f}` | |
end | |
end | |
Dir.entries(UPLOAD_DIR).each do |f| | |
next if (f == "." || f == "..") | |
f_path = File.expand_path(File.join(UPLOAD_DIR, f)) | |
puts "uploading #{f_path}" | |
Upload.new().put(f_path) | |
archive_path = File.expand_path(File.join(ARCHIVE_DIR, f)) | |
puts "archiving #{f_path} to #{archive_path}" | |
FileUtils.mv(f_path, archive_path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment