Class: AmazonS3::Handler
- Inherits:
-
Object
- Object
- AmazonS3::Handler
- Defined in:
- lib/amazon_s3.rb
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
-
#bucket_path ⇒ Object
Returns the value of attribute bucket_path.
-
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
Instance Method Summary collapse
- #bucket ⇒ Object
- #client ⇒ Object
- #dir_with_env ⇒ Object
- #download_file(file_name) ⇒ Object
- #get_file(file_name) ⇒ Object
- #get_image(file_name) ⇒ Object
-
#initialize(access_key_id, secret_access_key, bucket_name, bucket_path = nil) ⇒ Handler
constructor
A new instance of Handler.
- #upload_file(file_path) ⇒ Object
Constructor Details
#initialize(access_key_id, secret_access_key, bucket_name, bucket_path = nil) ⇒ Handler
Returns a new instance of Handler.
9 10 11 12 13 14 15 16 17 |
# File 'lib/amazon_s3.rb', line 9 def initialize(access_key_id, secret_access_key, bucket_name, bucket_path = nil) raise "S3 credentials must be present" if access_key_id.blank? || secret_access_key.blank? raise "Busket name must be present" if bucket_name.blank? self.access_key_id = access_key_id self.secret_access_key = secret_access_key self.bucket_path = bucket_path self.bucket_name = bucket_name end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
7 8 9 |
# File 'lib/amazon_s3.rb', line 7 def access_key_id @access_key_id end |
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
7 8 9 |
# File 'lib/amazon_s3.rb', line 7 def bucket_name @bucket_name end |
#bucket_path ⇒ Object
Returns the value of attribute bucket_path.
7 8 9 |
# File 'lib/amazon_s3.rb', line 7 def bucket_path @bucket_path end |
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
7 8 9 |
# File 'lib/amazon_s3.rb', line 7 def secret_access_key @secret_access_key end |
Instance Method Details
#bucket ⇒ Object
26 27 28 |
# File 'lib/amazon_s3.rb', line 26 def bucket client.buckets[self.bucket_name] end |
#client ⇒ Object
19 20 21 22 23 24 |
# File 'lib/amazon_s3.rb', line 19 def client c = AWS::S3.new({ :access_key_id => self.access_key_id, :secret_access_key => self.secret_access_key }) end |
#dir_with_env ⇒ Object
68 69 70 |
# File 'lib/amazon_s3.rb', line 68 def dir_with_env self.bucket_path end |
#download_file(file_name) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/amazon_s3.rb', line 39 def download_file(file_name) object_path = [self.bucket_path, file_name].compact.join('/') s3_file = bucket.objects[object_path] ext = Pathname.new(file_name).extname file = Tempfile.new [file_name.sub(ext, ''), ext], Dir.tmpdir, :encoding => 'ascii-8bit' file.write s3_file.read file.rewind file end |
#get_file(file_name) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/amazon_s3.rb', line 59 def get_file(file_name) s3_object = client.buckets[self.bucket_path].objects[file_name] ext = Pathname.new(file_name).extname file = Tempfile.new [file_name.sub(ext, ''), ext], Dir.tmpdir, :encoding => 'ascii-8bit' file.write s3_object.read file.rewind file end |
#get_image(file_name) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/amazon_s3.rb', line 49 def get_image(file_name) object_path = [self.bucket_path, file_name].compact.join('/') s3_image = bucket.objects[object_path] ext = Pathname.new(file_name).extname file = Tempfile.new [file_name.sub(ext, ''), ext], Dir.tmpdir, :encoding => 'ascii-8bit' file.write s3_image.read file.rewind file end |
#upload_file(file_path) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/amazon_s3.rb', line 30 def upload_file(file_path) ext = Pathname.new(file_path).extname file_name = [SecureRandom.hex, ext].join object_path = [self.bucket_name, self.bucket_path, file_name].compact.join('/') s3_file = bucket.objects[object_path] s3_file.write Pathname.new(file_path) file_name end |