lib/paperclip-aws.rb in paperclip-aws-1.6.1 vs lib/paperclip-aws.rb in paperclip-aws-1.6.5
- old
+ new
@@ -16,16 +16,16 @@
end unless defined?(AWS::Core)
attr_accessor :s3_credentials, :s3_bucket, :s3_permissions, :s3_options, :s3_protocol, :s3_host_alias
base.instance_eval do
- self.setup_credentials
- self.setup_bucket
- self.setup_permissions
- self.setup_s3_protocol
+ self.setup_s3_credentials
+ self.setup_s3_bucket
+ self.setup_s3_permissions
+ self.setup_s3_protocol
self.setup_s3_options
- self.setup_s3_host_alias
+ self.setup_s3_host_alias
end
end
def s3
@s3 ||= AWS::S3.new(
@@ -56,17 +56,38 @@
url = "#{choose_protocol(style, options)}://#{@s3_endpoint}/#{@s3_bucket}/#{path(style).gsub(%r{^/}, "")}"
end
return URI.escape(url)
end
end
+
+ def bucket
+ @bucket ||= self.s3.buckets[@s3_bucket]
+ end
+
+ def s3_bucket=(value)
+ @bucket = nil
+
+ @s3_bucket = value
+ @s3_bucket = @s3_bucket.call(self) if @s3_bucket.is_a?(Proc)
+ end
+
+ def versions(style = default_style)
+ begin
+ return nil unless self.bucket.versioning_enabled?
+ self.bucket.objects[path(style)].versions
+ rescue AWS::S3::Errors::Base => e
+ log "ERROR: Error occured while fetching versions for `#{path(style)}`. Probably versions are disabled or suspended for `#{self.bucket.name}` bucket."
+ return nil
+ end
+ end
def exists?(style = default_style)
if path(style).nil? || path(style).to_s.strip == ""
return false
end
begin
- return self.s3.buckets[@s3_bucket].objects[path(style)].exists?
+ return self.bucket.objects[path(style)].exists?
rescue AWS::S3::Errors::Base
return false
end
end
@@ -85,11 +106,11 @@
filename = path(style)
extname = File.extname(filename)
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
- file.write(self.s3.buckets[@s3_bucket].objects[path(style)].read)
+ file.write(self.bucket.objects[path(style)].read)
file.rewind
return file
end
def create_bucket
@@ -97,11 +118,11 @@
end
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
- log("saving #{path(style)}")
+ log "Saving #{path(style)}"
self.s3.buckets[@s3_bucket].objects[path(style)].write({
:file => file.path,
:acl => @s3_permissions[:style.to_sym] || @s3_permissions[:default],
:content_type => file.content_type.to_s.strip
@@ -117,34 +138,34 @@
end
def flush_deletes #:nodoc:
@queued_for_delete.each do |path|
begin
- log("deleting #{path}")
+ log "Deleting #{path}"
self.s3.buckets[@s3_bucket].objects[path].delete
rescue AWS::Errors::Base => e
raise
end
end
@queued_for_delete = []
end
# PRIVATE METHODS
- def setup_credentials
+ def setup_s3_credentials
if @options[:s3_credentials].present?
- @s3_credentials = self.parse_credentials(@options[:s3_credentials]).stringify_keys
+ @s3_credentials = self.parse_s3_credentials(@options[:s3_credentials]).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
@s3_credentials = (@s3_credentials[env] || @s3_credentials).symbolize_keys
@s3_endpoint = @s3_credentials[:endpoint] || 's3.amazonaws.com'
else
raise ArgumentError, "missing required :s3_credentials option"
end
end
- protected :setup_credentials
+ protected :setup_s3_credentials
- def parse_credentials(creds)
+ def parse_s3_credentials(creds)
case creds
when File
YAML::load(ERB.new(File.read(creds.path)).result)
when String, Pathname
YAML::load(ERB.new(File.read(creds)).result)
@@ -152,35 +173,38 @@
creds
else
raise ArgumentError, "Credentials are not a path, file, or hash."
end
end
- protected :parse_credentials
+ protected :parse_s3_credentials
- def setup_bucket
+ def setup_s3_bucket
if @options[:bucket].present?
- @s3_bucket = @options[:bucket] || @s3_credentials[:bucket]
- @s3_bucket = @s3_bucket.call(self) if @s3_bucket.is_a?(Proc)
+ log "DEPRECATION WARNING: Passing `:bucket` options to paperclip is deprecated. Please use `:s3_bucket` instead."
+ end
+
+ if @options[:s3_bucket].present? || @s3_credentials[:bucket].present? || @options[:bucket].present?
+ self.s3_bucket = @options[:s3_bucket] || @s3_credentials[:bucket] || @options[:bucket]
else
- raise ArgumentError, "missing required :bucket option"
+ raise ArgumentError, "missing required :s3_bucket option"
end
end
- protected :setup_bucket
+ protected :setup_s3_bucket
- def setup_permissions
- @s3_permissions = self.parse_permissions(@options[:s3_permissions])
+ def setup_s3_permissions
+ @s3_permissions = self.parse_s3_permissions(@options[:s3_permissions])
end
- protected :setup_permissions
+ protected :setup_s3_permissions
- def parse_permissions(permissions)
+ def parse_s3_permissions(permissions)
if permissions.is_a?(Hash)
permissions[:default] = permissions[:default] || :public_read
else
permissions = { :default => permissions || :public_read }
end
permissions
end
- protected :parse_permissions
+ protected :parse_s3_permissions
def setup_s3_protocol
@s3_protocol = @options[:s3_protocol] ||
Proc.new do |style, attachment|
permission = (@s3_permissions[style.to_sym] || @s3_permissions[:default])