Skip to content

Instantly share code, notes, and snippets.

@youyo
Created January 22, 2013 04:06
Show Gist options
  • Save youyo/4591976 to your computer and use it in GitHub Desktop.
Save youyo/4591976 to your computer and use it in GitHub Desktop.
Store S3-storage script.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'aws/s3'
include AWS::S3
Access_key = '********'
Secret_key = '********'
DEFAULT_HOST.replace "s3-ap-northeast-1.amazonaws.com"
MyBucket = 'bucket_name'
CF_Base_URL = 'http://cloudfront.youyo.info'
begin
raise "Source file is required!\n#{$0} source-file [destination]" if ARGV[0].nil?
source = ARGV[0]
destination = ARGV[1]
destination = '/' if destination.nil?
source_file = source.split('/').pop
if /\/$/ =~ destination
format_dist = destination
else
format_dist = "#{destination}/"
end
Base.establish_connection!(
:access_key_id => Access_key,
:secret_access_key => Secret_key
)
raise "#{source_file} is already exist!" if S3Object.exists? "#{format_dist}#{source_file}", MyBucket
S3Object.store("#{format_dist}#{source_file}", open(source), MyBucket)
raise "#{source_file} upload error!" unless S3Object.exists? "#{format_dist}#{source_file}", MyBucket
puts "Upload success!\nURL: #{CF_Base_URL}#{format_dist}#{source_file}\n"
rescue => err
puts err
exit 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment