Module: Simples3

Defined in:
lib/simples3.rb,
lib/simples3/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.about(path, bucket = nil) ⇒ Object



21
22
23
24
# File 'lib/simples3.rb', line 21

def self.about( path, bucket = nil )
  ensure_connection
  AWS::S3::S3Object.about( path, bucket || @@config[:bucket_name] )
end

.change_content_type(path = '', new_content_type = '', bucket = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/simples3.rb', line 55

def self.change_content_type( path = '', new_content_type = '', bucket = nil)
  ensure_connection
  object = AWS::S3::S3Object.find(path, bucket || @@config[:bucket_name])
  object.content_type = new_content_type
  object.store
end

.delete(path, bucket = nil) ⇒ Object



26
27
28
29
30
# File 'lib/simples3.rb', line 26

def self.delete(path, bucket = nil)
  ensure_connection
  Rails.logger.info "Deleting #{path} from S3..."
  AWS::S3::S3Object.delete(  path, bucket || @@config[:bucket_name] )
end

.ensure_connectionObject



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

def self.ensure_connection
  raise Config.new "#{Rails.root}/config/simples3.yml not found." unless File.exists?("#{Rails.root}/config/simples3.yml")
  @@config = YAML.load(ERB.new(File.read("#{Rails.root}/config/simples3.yml")).result)[RAILS_ENV].symbolize_keys
  @@connection = AWS::S3::Base.establish_connection!(
    :access_key_id     => @@config[:access_key_id],
    :secret_access_key => @@config[:secret_access_key]
  ) if AWS::S3::Base.connections.empty?
end

.file_exists?(path, bucket = nil) ⇒ Boolean



16
17
18
19
# File 'lib/simples3.rb', line 16

def self.file_exists?(path, bucket = nil)
  ensure_connection
  AWS::S3::S3Object.exists?( path, bucket || @@config[:bucket_name] )
end

.list(path = '', bucket = nil) ⇒ Object



43
44
45
46
# File 'lib/simples3.rb', line 43

def self.list( path = '', bucket = nil)
  ensure_connection
  AWS::S3::Bucket.objects( bucket || @@config[:bucket_name] ).collect{|o| o.path.gsub("/#{self.bucket_name}",'')}.select{|o| o.match(/^\/#{path}\//)}
end

.make_public(path = '', bucket = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/simples3.rb', line 48

def self.make_public( path = '', bucket = nil)
  ensure_connection
  object = AWS::S3::S3Object.find(path, bucket || @@config[:bucket_name])
  object.acl.grants << AWS::S3::ACL::Grant.grant(:public_read)
  object.acl( object.acl )
end

.move(from, to, bucket = nil) ⇒ Object



38
39
40
41
# File 'lib/simples3.rb', line 38

def self.move(from, to, bucket = nil)
  ensure_connection
  AWS::S3::S3Object.rename(from, to, bucket || @@config[:bucket_name])
end

.save(path, data, bucket = nil) ⇒ Object



32
33
34
35
36
# File 'lib/simples3.rb', line 32

def self.save(path, data, bucket = nil)
  ensure_connection
  Rails.logger.info "Saving #{path} to S3..."
  AWS::S3::S3Object.store( path, data, bucket || @@config[:bucket_name], :access => :public_read, 'cache-control' => 'public', "expires" => (Time.now+20.years).httpdate )
end