lib/s3cmd.rb in s3cmd-1.0.1 vs lib/s3cmd.rb in s3cmd-1.1.0
- old
+ new
@@ -1,74 +1,87 @@
-require "rubygems"
require "aws"
+require "logger"
require "mime/types"
require "proxifier/env"
+require "rubygems"
require "thor"
module S3Cmd
require "s3cmd/version"
- class Main < Thor
+ class CLI < Thor
+ class_option :access_key, :aliases => :a, :desc => "AWS access key to use"
+ class_option :credentials_file, :aliases => :c, :desc => "AWS credentials file containing the access and secret key", :default => File.expand_path("~/.aws-credentials")
+ class_option :secret_key, :aliases => :s, :desc => "AWS secret key to use"
+
desc "list-buckets", "lists all of your buckets"
def list_buckets
puts s3.buckets
end
desc "create-bucket name", "creates a bucket"
method_option :region, :default => "US"
def create_bucket(name)
- bucket = s3.bucket(name, true)
+ s3.bucket(name, true)
end
desc "list-keys bucket", "list the keys of a bucket"
def list_keys(bucket)
puts s3.bucket(bucket).keys
end
desc "get bucket key", "get the file for the key from the bucket"
def get(bucket, key)
- bucket = s3.bucket(bucket)
- $stdout << bucket.get(key)
+ puts s3.bucket(bucket).get(key)
end
desc "put bucket key file", "puts a file for the key in the bucket"
- method_option :type, :desc => "override the content type of the file", :type => :string
+ option :type, :desc => "override the content type of the file"
def put(bucket, key, file)
bucket = s3.bucket(bucket)
type = options[:type] || MIME::Types.of(file).first.to_s
File.open(file, "r") { |f| bucket.put(key, f, {}, nil, { "content-type" => type }) }
end
private
- def s3
- @s3 ||= begin
- access_key, secret_key = nil, nil
+ def credentials
+ return @credentials if defined?(@credentials)
- if ENV["AWS_CREDENTIAL_FILE"]
- File.open(ENV["AWS_CREDENTIAL_FILE"]) do |file|
- file.lines.each do |line|
- access_key = $1 if line =~ /^AWSAccessKeyId=(.*)$/
- secret_key = $1 if line =~ /^AWSSecretKey=(.*)$/
- end
- end
- elsif ENV["AWS_ACCESS_KEY"] || ENV["AWS_SECRET_KEY"]
- access_key = ENV["AWS_ACCESS_KEY"]
- secret_key = ENV["AWS_SECRET_KEY"]
- end
+ access_key = secret_key = nil
- unless access_key
- $stderr.puts "AWS_CREDENTIAL_FILE must containt AWSAccessKeyId or AWS_ACCESS_KEY must be set"
- exit 1
+ if options[:access_key] || options[:secret_key]
+ access_key = options[:access_key]
+ secret_key = options[:secret_key]
+ else
+ File.open(options[:credentials_file]) do |file|
+ file.lines.each do |line|
+ access_key = $1 if line =~ /^AWSAccessKeyId=(.*)$/
+ secret_key = $1 if line =~ /^AWSSecretKey=(.*)$/
end
- unless secret_key
- $stderr.puts "AWS_CREDENTIAL_FILE must containt AWSSecretKey or AWS_SECRET_KEY must be set"
- exit 1
- end
+ end
+ end
- logger = Logger.new(STDERR)
- logger.level = Logger::FATAL
+ @credentials = [access_key, secret_key]
+ end
- Aws::S3.new(access_key, secret_key, :logger => logger)
- end
+ def s3
+ return @s3 if defined?(@s3)
+
+ access_key, secret_key = credentials
+
+ unless access_key
+ $stderr.puts("No access key provided")
+ exit 1
end
+
+ unless secret_key
+ $stderr.puts("No secret key provided")
+ exit 1
+ end
+
+ logger = Logger.new(STDERR)
+ logger.level = Logger::FATAL
+
+ @s3 = Aws::S3.new(access_key, secret_key, :logger => logger)
+ end
end
end