Class: S3

Inherits:
Storage show all
Defined in:
lib/attach_it/storage/s3.rb

Instance Method Summary collapse

Methods inherited from Storage

#transform

Constructor Details

#initialize(credentials_file = nil, environment = nil) ⇒ S3

Returns a new instance of S3.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/attach_it/storage/s3.rb', line 7

def initialize(credentials_file = nil, environment = nil)
  begin
    credentials = YAML::load_file(credentials_file)
    @access_key_id = credentials[environment]['access_key_id']
    @secret_access_key = credentials[environment]['secret_access_key']
    @bucket = credentials[environment]['bucket']
    @permission = credentials[environment]['permission'].nil? ? :public_read : credentials[environment]['permission'].to_sym
    check_credencials
  rescue Exception => exception
    return exception.to_s
  end
end

Instance Method Details

#base64(attach_options = nil, style = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/attach_it/storage/s3.rb', line 58

def base64(attach_options = nil, style = nil)
  url_s3 = URI.parse(attach_options.url(style))

  Net::HTTP.start(url_s3.host, url_s3.port) do |http|
    resp = http.get(url_s3.path)
    resp.body
  end
end

#flush_delete(queued_for_delete = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/attach_it/storage/s3.rb', line 41

def flush_delete(queued_for_delete = nil)
  queued_for_delete.each do |file|
    begin
      AWS::S3::S3Object.delete(reajust_url(file), @bucket)
    rescue AWS::S3::NoSuchBucket => exception
      nil
    rescue Exception => exception
      nil
    end
  end
end

#flush_write(attach_options = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/attach_it/storage/s3.rb', line 20

def flush_write(attach_options = nil)
  attach_options.styles.each do |style_name, style_value|
    begin
      temp_file = create_temp_file
      transform(style_value, attach_options.assigned_file.path).write(temp_file.path)
      AWS::S3::S3Object.store(attach_options.short_url(style_name), File.open(temp_file.path, 'rb'), @bucket, :access => @permission)
      #temp_file.unlink
    rescue Exception => exception
      attach_options.add_error(exception.to_s)
    end
  end

  unless attach_options.styles.has_key?(:original)
    begin
      AWS::S3::S3Object.store(attach_options.short_url, File.open(attach_options.assigned_file.path, 'rb'), @bucket, :access => @permission)
    rescue Exception => exception
      attach_options.add_error(exception.to_s)
    end
  end
end

#set_queued_for_delete(attach_options = nil, styles = {}) ⇒ Object



67
68
69
70
71
# File 'lib/attach_it/storage/s3.rb', line 67

def set_queued_for_delete(attach_options = nil, styles = {})
  [styles.keys, :original].flatten.map do |style_name|
    attach_options.url(style_name)
  end.compact
end

#url(attach_options = nil, url = '', style = nil) ⇒ Object



53
54
55
56
# File 'lib/attach_it/storage/s3.rb', line 53

def url(attach_options = nil, url = '', style = nil)
  file = reajust_url(url)
  AWS::S3::S3Object.url_for(attach_options.interpolate(url, style), @bucket)
end