Module: Paperclip::Storage::AwsCore

Defined in:
lib/paperclip/aws/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/paperclip/aws/core.rb', line 6

def self.extended base
  begin
    require 'aws-core-sdk'
  rescue LoadError => e
    e.message << " (You may need to install the aws-core-sdk gem)"
    raise e
  end unless defined?(Aws)


  base.instance_eval do
    @s3_options     = @options[:s3_options]     || {}
  
    @s3_endpoint    = @options[:s3_endpoint]
    
    if @options[:credentials].present?
      credentials = @options[:credentials].symbolize_keys
      ::Aws::config[:credentials] = ::Aws::Credentials.new(credentials[:access_key_id], credentials[:secret_access_key])
      ::Aws.config[:region] = credentials[:region]
    end
      
    @aws_credentials = @options[:credentials]   || {}

    @s3_protocol    = @options[:s3_protocol]    ||
      Proc.new do |style, attachment|
        permission  = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
        permission  = permission.call(attachment, style) if permission.respond_to?(:call)
        (permission == :public_read) ? 'http' : 'https'
      end

    unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host"
      @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system/, '')
      @options[:url]  = ":s3_path_url"
    end
    @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)

    @http_proxy = @options[:http_proxy] || nil
  end

  Paperclip.interpolates(:s3_alias_url) do |attachment, style|
    "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
    "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_path_url
  Paperclip.interpolates(:s3_domain_url) do |attachment, style|
    "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_domain_url
  Paperclip.interpolates(:asset_host) do |attachment, style|
    "#{attachment.path(style).gsub(%r{\A/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :asset_host
end

Instance Method Details

#bucket_nameObject



89
90
91
92
93
# File 'lib/paperclip/aws/core.rb', line 89

def bucket_name
  @bucket = @options[:bucket] || s3_credentials[:bucket]
  @bucket = @bucket.call(self) if @bucket.respond_to?(:call)
  @bucket or raise ArgumentError, "missing required :bucket option"
end

#copy_to_local_file(style, local_dest_path) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/paperclip/aws/core.rb', line 178

def copy_to_local_file(style, local_dest_path)

  log("copying #{path(style)} to local file #{local_dest_path}")
  local_file = ::File.open(local_dest_path, 'wb')
  file = s3_interface.get_object(bucket: bucket_name, key: path(style))
  file.body.pos = 0 
  local_file.write(file.body.read)
  local_file.close
rescue AWS::Errors::Base => e
  warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
  false
end

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
# File 'lib/paperclip/aws/core.rb', line 113

def exists?(style = default_style)
  if original_filename
    s3_interface.get_object(bucket: bucket_name, key: style)
    true
  else
    false
  end
rescue => e
  false
end

#expiring_url(time = 3600, style_name = default_style) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/paperclip/aws/core.rb', line 58

def expiring_url(time = 3600, style_name = default_style)
  if path(style_name)
    base_options = { :expires => time, :secure => use_secure_protocol?(style_name) }
    s3_object(style_name).url_for(:read, base_options.merge(s3_url_options)).to_s
  else
    url(style_name)
  end
end

#flush_deletesObject

:nodoc:



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/paperclip/aws/core.rb', line 165

def flush_deletes #:nodoc:
  @queued_for_delete.each do |path|
    begin
      log("deleting #{path}")
      s3_interface.delete_object(bucket: bucket_name, key: path.sub(%r{\A/},''))
    rescue Aws::Errors::Base => e
      # Ignore this.
    end
  end
  @queued_for_delete = []
end

#flush_writesObject

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/paperclip/aws/core.rb', line 136

def flush_writes #:nodoc:
  @queued_for_write.each do |style, file|
    begin
      log("saving #{path(style)}")

      write_options = {
        bucket: bucket_name, 
        key: path(style), 
        acl: 'public-read', 
        body: File.read(file.path), 
        content_type: file.content_type
      }

      
      s3_interface.put_object(write_options)
    rescue => e
      log("Error: #{e.inspect}")
      # create_bucket
      # retry
    ensure
      file.rewind
    end
  end

  after_flush_writes # allows attachment to clean up temp files

  @queued_for_write = {}
end

#obtain_s3_instance_for(options) ⇒ Object



103
104
105
106
# File 'lib/paperclip/aws/core.rb', line 103

def obtain_s3_instance_for(options)
  instances = (Thread.current[:paperclip_s3_instances] ||= {})
  instances[options] ||= s3_endpoint.present? ? ::Aws::S3.new(endpoint: s3_endpoint) : ::Aws.s3
end

#s3_credentialsObject



67
68
69
# File 'lib/paperclip/aws/core.rb', line 67

def s3_credentials
  # @s3_credentials ||= parse_credentials(@options[:s3_credentials])
end

#s3_endpointObject



108
109
110
# File 'lib/paperclip/aws/core.rb', line 108

def s3_endpoint
  @s3_endpoint
end

#s3_host_aliasObject



78
79
80
81
# File 'lib/paperclip/aws/core.rb', line 78

def s3_host_alias
  @s3_host_alias = @options[:host_alias] || "#{s3_host_name}/#{bucket_name}"
  @s3_host_alias
end

#s3_host_nameObject



71
72
73
74
75
76
# File 'lib/paperclip/aws/core.rb', line 71

def s3_host_name
  host_name = @options[:s3_host_name] ||  "s3.amazonaws.com"
  # host_name = host_name.call(self) if host_name.is_a?(Proc)
  # 
  # host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
end

#s3_interfaceObject



95
96
97
98
99
100
101
# File 'lib/paperclip/aws/core.rb', line 95

def s3_interface
  @s3_interface ||= begin
    config = { } # :s3_endpoint => s3_host_name }

    obtain_s3_instance_for(config.merge(@s3_options))
  end
end

#s3_protocol(style = default_style, with_colon = false) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/paperclip/aws/core.rb', line 125

def s3_protocol(style = default_style, with_colon = false)
  protocol = @s3_protocol
  protocol = protocol.call(style, self) if protocol.respond_to?(:call)

  if with_colon && !protocol.empty?
    "#{protocol}:"
  else
    protocol.to_s
  end
end

#s3_url_optionsObject



83
84
85
86
87
# File 'lib/paperclip/aws/core.rb', line 83

def s3_url_options
  s3_url_options = @options[:s3_url_options] || {}
  s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
  s3_url_options
end