Class: Dabcup::Storage::Driver::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/dabcup/storage/driver/s3.rb

Overview

Amazon S3

Instance Attribute Summary

Attributes inherited from Base

#uri

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
11
# File 'lib/dabcup/storage/driver/s3.rb', line 6

def initialize(uri)
  super(uri)
  require 'aws/s3'
rescue LoadError => ex
  raise Dabcup::Error.new("The library aws-s3 is missing. Get it via 'gem install aws-s3' and set RUBYOPT=rubygems.")
end

Instance Method Details

#bucketObject



55
56
57
# File 'lib/dabcup/storage/driver/s3.rb', line 55

def bucket
  @bucket ||= uri.host.split('.').first
end

#connectObject



45
46
47
48
49
# File 'lib/dabcup/storage/driver/s3.rb', line 45

def connect
  return if AWS::S3::Base.connected?
  AWS::S3::Base.establish_connection!(:access_key_id => uri.user, :secret_access_key => uri.password)
  create_bucket
end

#create_bucketObject



63
64
65
66
67
# File 'lib/dabcup/storage/driver/s3.rb', line 63

def create_bucket
  AWS::S3::Bucket.list.each do |bucket|
    return if bucket.name == bucket
  end
end

#delete(file_name) ⇒ Object



40
41
42
43
# File 'lib/dabcup/storage/driver/s3.rb', line 40

def delete(file_name)
  connect
  AWS::S3::S3Object.delete(file_name, bucket)
end

#disconnectObject



51
52
53
# File 'lib/dabcup/storage/driver/s3.rb', line 51

def disconnect
  AWS::S3::Base.disconnect!
end

#get(remote_path, local_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/dabcup/storage/driver/s3.rb', line 24

def get(remote_path, local_path)
  connect
  File.open(local_path, 'w') do |file|
    AWS::S3::S3Object.stream(remote_path, bucket) do |stream|
      file.write(stream)
    end
  end
end

#listObject



33
34
35
36
37
38
# File 'lib/dabcup/storage/driver/s3.rb', line 33

def list
  connect
  AWS::S3::Bucket.find(bucket).objects.collect do |obj|
    Dump.new(:name => obj.key.to_s, :size => obj.size)
  end
end

#local?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dabcup/storage/driver/s3.rb', line 59

def local?
  false
end

#protocolObject



13
14
15
# File 'lib/dabcup/storage/driver/s3.rb', line 13

def protocol
  's3'
end

#put(local_path, remote_path) ⇒ Object



17
18
19
20
21
22
# File 'lib/dabcup/storage/driver/s3.rb', line 17

def put(local_path, remote_path)
  connect
  File.open(local_path) do |file|
    AWS::S3::S3Object.store(remote_path, file, bucket)
  end
end