Class: Filestorage::AmazonS3
- Inherits:
-
Object
- Object
- Filestorage::AmazonS3
- Defined in:
- lib/filestorage-s3/amazon_s3.rb
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #exist?(path) ⇒ Boolean
- #files ⇒ Object
- #get(path) ⇒ Object
-
#initialize(bucket, access_key_id, secret_access_key) ⇒ AmazonS3
constructor
A new instance of AmazonS3.
- #store(path, file) ⇒ Object
Constructor Details
#initialize(bucket, access_key_id, secret_access_key) ⇒ AmazonS3
Returns a new instance of AmazonS3.
11 12 13 14 15 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 11 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
37 38 39 40 41 42 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 37 def delete(path) obj = @bucket.objects[path] raise NotExist.new("Not exist #{path}") unless obj.exists? obj.delete path end |
#exist?(path) ⇒ Boolean
44 45 46 47 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 44 def exist?(path) obj = @bucket.objects[path] obj.exists? end |
#files ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 49 def files files = [] @bucket.objects.each do |obj| files << obj.key end files end |
#get(path) ⇒ Object
31 32 33 34 35 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 31 def get(path) obj = @bucket.objects[path] raise NotExist.new("Not exist #{path}") unless obj.exists? obj end |
#store(path, file) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/filestorage-s3/amazon_s3.rb', line 17 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 |