Sha256: 95ea0c3495aa50ba8cfc4951f84f9b9e3d33c7f56bc185efa66be6729ed6f9a0

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require "rubygems"
require "aws"
require "proxifier/env"
require "thor"

module S3Cmd
  require "s3cmd/version"

  class Main < Thor
    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)
    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)
    end

    desc "put bucket key file", "puts a file for the key in the bucket"
    def put(bucket, key, file)
      bucket = s3.bucket(bucket)
      File.open(file, "r") { |f| bucket.put(key, f) }
    end

    private
      def s3
        @s3 ||= begin
          access_key, secret_key = nil, nil

          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

          unless access_key
            $stderr.puts "AWS_CREDENTIAL_FILE must containt AWSAccessKeyId or AWS_ACCESS_KEY must be set"
            exit 1
          end
          unless secret_key
            $stderr.puts "AWS_CREDENTIAL_FILE must containt AWSSecretKey or AWS_SECRET_KEY must be set"
            exit 1
          end

          logger = Logger.new(STDERR)
          logger.level = Logger::FATAL

          Aws::S3.new(access_key, secret_key, :logger => logger)
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
s3cmd-1.0.0 lib/s3cmd.rb