Commit 774cee15 by Kelley Reynolds Committed by Prem Sichanugrist

Only establish an S3 connection if required.

parent 75ba69da
...@@ -97,11 +97,6 @@ module Paperclip ...@@ -97,11 +97,6 @@ module Paperclip
if @http_proxy if @http_proxy
@s3_options.merge!({:proxy => @http_proxy}) @s3_options.merge!({:proxy => @http_proxy})
end end
AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => s3_credentials[:access_key_id],
:secret_access_key => s3_credentials[:secret_access_key]
))
end end
Paperclip.interpolates(:s3_alias_url) do |attachment, style| Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}" "#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
...@@ -118,7 +113,7 @@ module Paperclip ...@@ -118,7 +113,7 @@ module Paperclip
end end
def expiring_url(time = 3600, style_name = default_style) def expiring_url(time = 3600, style_name = default_style)
AWS::S3::S3Object.url_for(path(style_name), bucket_name, :expires_in => time, :use_ssl => (s3_protocol(style_name) == 'https')) s3_object.url_for(path(style_name), bucket_name, :expires_in => time, :use_ssl => (s3_protocol(style_name) == 'https'))
end end
def s3_credentials def s3_credentials
...@@ -178,7 +173,7 @@ module Paperclip ...@@ -178,7 +173,7 @@ module Paperclip
def exists?(style = default_style) def exists?(style = default_style)
if original_filename if original_filename
AWS::S3::S3Object.exists?(path(style), bucket_name) s3_object.exists?(path(style), bucket_name)
else else
false false
end end
...@@ -207,20 +202,20 @@ module Paperclip ...@@ -207,20 +202,20 @@ module Paperclip
basename = File.basename(filename, extname) basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname]) file = Tempfile.new([basename, extname])
file.binmode file.binmode
file.write(AWS::S3::S3Object.value(path(style), bucket_name)) file.write(s3_object.value(path(style), bucket_name))
file.rewind file.rewind
return file return file
end end
def create_bucket def create_bucket
AWS::S3::Bucket.create(bucket_name) s3_bucket.create(bucket_name)
end end
def flush_writes #:nodoc: def flush_writes #:nodoc:
@queued_for_write.each do |style, file| @queued_for_write.each do |style, file|
begin begin
log("saving #{path(style)}") log("saving #{path(style)}")
AWS::S3::S3Object.store(path(style), s3_object.store(path(style),
file, file,
bucket_name, bucket_name,
{:content_type => file.content_type.to_s.strip, {:content_type => file.content_type.to_s.strip,
...@@ -243,7 +238,7 @@ module Paperclip ...@@ -243,7 +238,7 @@ module Paperclip
@queued_for_delete.each do |path| @queued_for_delete.each do |path|
begin begin
log("deleting #{path}") log("deleting #{path}")
AWS::S3::S3Object.delete(path, bucket_name) s3_object.delete(path, bucket_name)
rescue AWS::S3::ResponseError rescue AWS::S3::ResponseError
# Ignore this. # Ignore this.
end end
...@@ -265,6 +260,25 @@ module Paperclip ...@@ -265,6 +260,25 @@ module Paperclip
end end
private :find_credentials private :find_credentials
def s3_object
establish_connection!
AWS::S3::S3Object
end
private :s3_object
def s3_bucket
establish_connection!
AWS::S3::Bucket
end
private :s3_bucket
def establish_connection!
@connection ||= AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => s3_credentials[:access_key_id],
:secret_access_key => s3_credentials[:secret_access_key]
))
end
private :establish_connection!
end end
end end
end end
...@@ -476,6 +476,7 @@ class S3Test < Test::Unit::TestCase ...@@ -476,6 +476,7 @@ class S3Test < Test::Unit::TestCase
Dummy.delete_all Dummy.delete_all
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar.send(:establish_connection!)
end end
should "parse the credentials" do should "parse the credentials" do
...@@ -499,6 +500,7 @@ class S3Test < Test::Unit::TestCase ...@@ -499,6 +500,7 @@ class S3Test < Test::Unit::TestCase
Dummy.delete_all Dummy.delete_all
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar.send(:establish_connection!)
end end
should "run the file through ERB" do should "run the file through ERB" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment