Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Only establish an S3 connection if required.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelley Reynolds authored and sikachu committed Oct 21, 2011
1 parent 75ba69d commit 774cee1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/paperclip/storage/s3.rb
Expand Up @@ -97,11 +97,6 @@ def self.extended base
if @http_proxy
@s3_options.merge!({:proxy => @http_proxy})
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
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
Expand All @@ -118,7 +113,7 @@ def self.extended base
end

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

def s3_credentials
Expand Down Expand Up @@ -178,7 +173,7 @@ def parse_credentials creds

def exists?(style = default_style)
if original_filename
AWS::S3::S3Object.exists?(path(style), bucket_name)
s3_object.exists?(path(style), bucket_name)
else
false
end
Expand Down Expand Up @@ -207,20 +202,20 @@ def to_file style = default_style
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.write(s3_object.value(path(style), bucket_name))
file.rewind
return file
end

def create_bucket
AWS::S3::Bucket.create(bucket_name)
s3_bucket.create(bucket_name)
end

def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
AWS::S3::S3Object.store(path(style),
s3_object.store(path(style),
file,
bucket_name,
{:content_type => file.content_type.to_s.strip,
Expand All @@ -243,7 +238,7 @@ def flush_deletes #:nodoc:
@queued_for_delete.each do |path|
begin
log("deleting #{path}")
AWS::S3::S3Object.delete(path, bucket_name)
s3_object.delete(path, bucket_name)
rescue AWS::S3::ResponseError
# Ignore this.
end
Expand All @@ -265,6 +260,25 @@ def find_credentials creds
end
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
2 changes: 2 additions & 0 deletions test/storage/s3_test.rb
Expand Up @@ -476,6 +476,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError

Dummy.delete_all
@dummy = Dummy.new
@dummy.avatar.send(:establish_connection!)
end

should "parse the credentials" do
Expand All @@ -499,6 +500,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
Dummy.delete_all

@dummy = Dummy.new
@dummy.avatar.send(:establish_connection!)
end

should "run the file through ERB" do
Expand Down

0 comments on commit 774cee1

Please sign in to comment.