Class: IPASend::S3Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/ipasend/s3_uploader.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
's3-eu-west-1.amazonaws.com'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3Uploader

Returns a new instance of S3Uploader.



7
8
9
10
11
12
13
14
# File 'lib/ipasend/s3_uploader.rb', line 7

def initialize(options)
  @s3 = AWS::S3.new(
    :s3_endpoint => options.aws_endpoint || DEFAULT_ENDPOINT,
    :access_key_id => options.aws_key,
    :secret_access_key => options.aws_secret
  )
  @s3_bucket = @s3.buckets[options.aws_bucket]
end

Instance Method Details

#upload(key, data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ipasend/s3_uploader.rb', line 16

def upload(key, data)
  s3_object = @s3_bucket.objects[key]

  pbar = ProgressBar.create(:title => "Uploading '#{key}'", :total => data.size, :format => '%e |%b>>%i| %p%% %t')

  s3_object.write(:content_length => data.size, :acl => :public_read) do |buffer, bytes|
    buffer.write(data.read(bytes))
    pbar.progress += bytes rescue nil
  end

  pbar.finish

  s3_object
end