Class: Contraption::S3Uploader

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

Instance Method Summary collapse

Constructor Details

#initialize(connection_args = {}, target_bucket, path) ⇒ S3Uploader

Returns a new instance of S3Uploader.



6
7
8
9
10
# File 'lib/contraption/s3_uploader.rb', line 6

def initialize connection_args={}, target_bucket, path
  AWS::S3::Base.establish_connection! connection_args
  @target_bucket = target_bucket
  @path = path.cd "drafts"
end

Instance Method Details

#handle(request) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/contraption/s3_uploader.rb', line 16

def handle request
  local_path = Pathname.new(@path.path) + request.last
  if local_path.exist?
    remote_path = "#{Time.now.year}" + "/#{"%02d" % Time.now.mon}/" + request.last.split('/').last
    upload local_path, remote_path
    ["http:", "#{@target_bucket}/#{remote_path}"]
  else
    raise "#{local_path.expand_path} does not exist"
  end
end

#protocolObject



12
13
14
# File 'lib/contraption/s3_uploader.rb', line 12

def protocol
  :s3
end

#upload(local_path, remote_path) ⇒ Object



27
28
29
30
# File 'lib/contraption/s3_uploader.rb', line 27

def upload local_path, remote_path
  puts "Uploading #{local_path} to #{remote_path}"
  AWS::S3::S3Object.store(remote_path, open(local_path), @target_bucket, :access => :public_read, 'Cache-Control' => 'max-age=3136000')
end