Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Created October 9, 2012 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mechamogera/3858584 to your computer and use it in GitHub Desktop.
Save mechamogera/3858584 to your computer and use it in GitHub Desktop.
mulitpartでのS3アップロードスクリプト
gem 'aws-sdk'
gem 'mime-types'
require 'aws'
require 'mime/types'
require 'optparse'
access_key_id = nil
secret_access_key = nil
endpoint = 's3-ap-northeast-1.amazonaws.com'
bucket = nil
upload_file = __FILE__
acl = "private"
# http://aws.typepad.com/aws/2010/11/amazon-s3-multipart-upload.html
# によると最小サイズは5MBらしい
multipart_size = 5048576
opt = OptionParser.new
opt.on('-a', '--access-key-id=VAL') { |v| access_key_id = v }
opt.on('-s', '--secret-access-key=VAL') { |v| secret_access_key = v }
opt.on('-b', '--bucket=VAL') { |v| bucket = v }
opt.on('-e', '--endpoint=VAL', "Default:#{endpoint}") { |v| endpoint = v }
opt.on('-f', '--file=VAL', "Default:#{upload_file}") { |v| upload_file = v }
opt.on('-c', '--acl=VAL', "Default:#{acl}") { |v| acl = v }
opt.on('-m', '--multipart-size=VAL') { |v| multipart_size = v.to_i }
opt.parse!(ARGV)
AWS.config(:access_key_id => access_key_id, :secret_access_key => secret_access_key) if access_key_id && secret_access_key
s3 = AWS::S3.new(
:s3_endpoint => endpoint,
:proxy_uri => ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
)
bucket = s3.buckets[bucket]
open(upload_file) do |file|
object = bucket.objects[File.basename(upload_file)]
object.multipart_upload(:acl => acl.to_sym,
:content_type => MIME::Types.type_for(upload_file)[0]) do |upload|
while !file.eof?
upload.add_part(file.read multipart_size)
p 'Aborted' if upload.aborted?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment