Class: AmazonS3::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject

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_nameObject

Returns the value of attribute bucket_name.



7
8
9
# File 'lib/amazon_s3.rb', line 7

def bucket_name
  @bucket_name
end

#bucket_pathObject

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_keyObject

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

#bucketObject



26
27
28
# File 'lib/amazon_s3.rb', line 26

def bucket
  client.buckets[self.bucket_name]
end

#clientObject



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_envObject



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