lib/paperclip/storage.rb in thoughtbot-paperclip-2.3.0 vs lib/paperclip/storage.rb in thoughtbot-paperclip-2.3.1
- old
+ new
@@ -31,11 +31,10 @@
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
@queued_for_write[style] || (File.new(path(style), 'rb') if exists?(style))
end
- alias_method :to_io, :to_file
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
file.close
FileUtils.mkdir_p(File.dirname(path(style)))
@@ -126,21 +125,31 @@
# to interpolate. Keys should be unique, like filenames, and despite the fact that
# S3 (strictly speaking) does not support directories, you can still use a / to
# separate parts of your file name.
module S3
def self.extended base
- require 'right_aws'
+ begin
+ require 'aws/s3'
+ rescue LoadError => e
+ e.message << " (You may need to install the aws-s3 gem)"
+ raise e
+ end
+
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
- @s3_permissions = @options[:s3_permissions] || 'public-read'
- @s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
+ @s3_permissions = @options[:s3_permissions] || :public_read
+ @s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
@url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/)
+ 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}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
end
Paperclip.interpolates(:s3_path_url) do |attachment, style|
@@ -149,20 +158,10 @@
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
"#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
end
end
- def s3
- @s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id],
- @s3_credentials[:secret_access_key],
- @s3_options)
- end
-
- def s3_bucket
- @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
- end
-
def bucket_name
@bucket
end
def s3_host_alias
@@ -173,45 +172,53 @@
creds = find_credentials(creds).stringify_keys
(creds[RAILS_ENV] || creds).symbolize_keys
end
def exists?(style = default_style)
- s3_bucket.key(path(style)) ? true : false
+ if original_filename
+ AWS::S3::S3Object.exists?(path(style), bucket_name)
+ else
+ false
+ end
end
def s3_protocol
@s3_protocol
end
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
- @queued_for_write[style] || s3_bucket.key(path(style))
+ return @queued_for_write[style] if @queued_for_write[style]
+ file = Tempfile.new(path(style))
+ file.write(AWS::S3::S3Object.value(path(style), bucket_name))
+ file.rewind
+ return file
end
- alias_method :to_io, :to_file
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
- key = s3_bucket.key(path(style))
- key.data = file
- key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers))
- rescue RightAws::AwsError => e
+ AWS::S3::S3Object.store(path(style),
+ file,
+ bucket_name,
+ {:content_type => instance_read(:content_type),
+ :access => @s3_permissions,
+ }.merge(@s3_headers))
+ rescue AWS::S3::ResponseError => e
raise
end
end
@queued_for_write = {}
end
def flush_deletes #:nodoc:
@queued_for_delete.each do |path|
begin
log("deleting #{path}")
- if file = s3_bucket.key(path)
- file.delete
- end
- rescue RightAws::AwsError
+ AWS::S3::S3Object.delete(path, bucket_name)
+ rescue AWS::S3::ResponseError
# Ignore this.
end
end
@queued_for_delete = []
end