Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Last active March 15, 2018 22:46
Show Gist options
  • Save mechamogera/3618389 to your computer and use it in GitHub Desktop.
Save mechamogera/3618389 to your computer and use it in GitHub Desktop.
AWSの簡易S3アップロードRubyスクリプト
# A sample Gemfile
source "https://rubygems.org"
gem 'aws-sdk'
gem 'mime-types'
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"
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('-r', '--region=VAL', "Default:#{region}") { |v| region = v }
opt.on('-f', '--file=VAL', "Default:#{upload_file}") { |v| upload_file = v }
opt.on('-c', '--acl=VAL', "Default:#{acl}") { |v| acl = v }
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(
:region => region,
:proxy_uri => ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
)
bucket = s3.buckets[bucket]
object = bucket.objects[File.basename(upload_file)]
object.write(:file => upload_file,
:content_type => MIME::Types.type_for(upload_file)[0],
:acl => acl.to_sym,
:storage_class => :reduced_redundancy
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment