Sha256: 8741616fdd266e97e85ccc85d464ec789df78b19e720a8af1a42f77738275225

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module S3Stream
  class Main < Thor
    desc "fetch bucket filename", "download/stream the file from S3 to stdout"
    def fetch(bucket_name, filename)
      require 'open-uri'
      object = s3object(bucket_name, filename)
      uri = object.url_for(:read, :secure => true, :expires => 60 * 60) # 1 hour
      uri.open do |stream|
        buffer = ""
        until stream.eof?
          stream.readpartial(4096, buffer)
          $stdout.write(buffer)
        end
      end
    end
    
    desc "store bucket filename", "upload/stream the file from stdin to S3"
    def store(bucket_name, filename)
      $stdout.sync = true
      object = s3object(bucket_name, filename)
      S3Stream::Upload.to(:s3object => object, :log_to => $stdout) do |out|
        buffer = ""
        until $stdin.eof?
          $stdin.readpartial(4096, buffer)
          out.write(buffer)
        end
      end
    end

    private

    def s3
      @s3 ||= AWS::S3.new(S3Stream::CREDENTIALS)
    end

    def s3object(bucket_name, key)
      bucket = s3.buckets[bucket_name]
      bucket.objects[key]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
s3stream-0.0.9 lib/s3stream/main.rb
s3stream-0.0.8 lib/s3stream/main.rb
s3stream-0.0.7 lib/s3stream/main.rb