Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Created August 9, 2012 06:08
Show Gist options
  • Save mechamogera/3301513 to your computer and use it in GitHub Desktop.
Save mechamogera/3301513 to your computer and use it in GitHub Desktop.
AWSの簡易S3ダウンロードRubyスクリプト
require 'rubygems'
gem 'aws-sdk'
require 'aws'
require 'optparse'
access_key_id = nil
secret_access_key = nil
endpoint = 's3-ap-northeast-1.amazonaws.com'
bucket = nil
object_key = nil
output_file = nil
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('-k', '--object-key=VAL') { |v| object_key = v }
opt.on('-o', '--output-file=VAL', "Default:object-key basename") { |v| output_file = v }
opt.on('-e', '--endpoint=VAL', "Default:#{endpoint}") { |v| endpoint = v }
opt.parse!(ARGV)
output_file = File.basename(object_key) unless output_file
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]
object = bucket.objects[object_key]
File.open(output_file, 'w') do |file|
object.read do |chunk|
file.write chunk
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment