Class: Filestorage::AmazonS3

Inherits:
Object
  • Object
show all
Defined in:
lib/filestorage/amazon_s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket, access_key_id, secret_access_key) ⇒ AmazonS3

Returns a new instance of AmazonS3.



10
11
12
13
14
# File 'lib/filestorage/amazon_s3.rb', line 10

def initialize(bucket, access_key_id, secret_access_key)
  @s3 = AWS::S3.new(:access_key_id     => access_key_id,
                    :secret_access_key => secret_access_key)
  @bucket = @s3.buckets[bucket]
end

Instance Method Details

#delete(path) ⇒ Object

Raises:



36
37
38
39
40
41
# File 'lib/filestorage/amazon_s3.rb', line 36

def delete(path)
  obj = @bucket.objects[path]
  raise NotExist.new("Not exist #{path}") unless obj.exists?
  obj.delete
  path
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def exist?(path)
  obj = @bucket.objects[path]
  obj.exists?
end

#filesObject



48
49
50
51
52
53
54
# File 'lib/filestorage/amazon_s3.rb', line 48

def files
  files = []
  @bucket.objects.each do |obj|
    files << obj.key
  end
  files
end

#get(path) ⇒ Object

Raises:



30
31
32
33
34
# File 'lib/filestorage/amazon_s3.rb', line 30

def get(path)
  obj = @bucket.objects[path]
  raise NotExist.new("Not exist #{path}") unless obj.exists?
  obj
end

#store(path, file) ⇒ Object

Raises:



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

def store(path, file)
  obj = @bucket.objects[path]
  raise AlreadyExist.new("Already exist #{path}") if obj.exists?
  content = if file.instance_of?(Pathname)
    File.open(file, "rb"){|f| f.read}
  elsif file.instance_of?(File)
    file.read
  else
    file
  end
  obj.write(content, :acl => :public_read)
  path
end