Commit 25be0b25 by Tute Costa

Paperclip::Storage::S3 simplification

We don't support AWS SDK v1 any longer.
parent 4adcbeda
When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field| When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
definition = Paperclip::AttachmentRegistry.definitions_for(User)[field.downcase.to_sym] definition = Paperclip::AttachmentRegistry.definitions_for(User)[field.downcase.to_sym]
path = if defined?(::AWS) path = "https://paperclip.s3-us-west-2.amazonaws.com#{definition[:path]}"
"https://paperclip.s3.amazonaws.com#{definition[:path]}"
else
"https://paperclip.s3-us-west-2.amazonaws.com#{definition[:path]}"
end
path.gsub!(':filename', File.basename(file_path)) path.gsub!(':filename', File.basename(file_path))
path.gsub!(/:([^\/\.]+)/) do |match| path.gsub!(/:([^\/\.]+)/) do |match|
"([^\/\.]+)" "([^\/\.]+)"
end end
FakeWeb.register_uri(:put, Regexp.new(path), :body => defined?(::AWS) ? "OK" : "<xml></xml>") FakeWeb.register_uri(:put, Regexp.new(path), :body => "<xml></xml>")
step "I attach the file \"#{file_path}\" to \"#{field}\"" step "I attach the file \"#{file_path}\" to \"#{field}\""
end end
......
...@@ -113,33 +113,24 @@ module Paperclip ...@@ -113,33 +113,24 @@ module Paperclip
module S3 module S3
def self.extended base def self.extended base
unless defined?(AWS_CLASS) begin
begin require 'aws-sdk'
require 'aws-sdk' const_set 'DEFAULT_PERMISSION', :"public-read"
const_set('AWS_CLASS', defined?(::Aws) ? ::Aws : ::AWS)
const_set('AWS_BASE_ERROR', rescue LoadError => e
defined?(::Aws) ? Aws::Errors::ServiceError : AWS::Errors::Base) e.message << " (You may need to install the aws-sdk gem)"
const_set('DEFAULT_PERMISSION', raise e
defined?(::AWS) ? :public_read : :'public-read')
rescue LoadError => e
e.message << " (You may need to install the aws-sdk gem)"
raise e
end
if Gem::Version.new(AWS_CLASS::VERSION) >= Gem::Version.new(2) && Gem::Version.new(AWS_CLASS::VERSION) <= Gem::Version.new("2.0.33")
raise LoadError, "paperclip does not support aws-sdk versions 2.0.0 - 2.0.33. Please upgrade aws-sdk to a newer version."
end
end end
# Overriding log formatter to make sure it return a UTF-8 string # Overriding log formatter to make sure it return a UTF-8 string
if defined?(AWS_CLASS::Core::LogFormatter) if defined?(::Aws::Core::LogFormatter)
AWS_CLASS::Core::LogFormatter.class_eval do ::Aws::Core::LogFormatter.class_eval do
def summarize_hash(hash) def summarize_hash(hash)
hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',') hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
end end
end end
elsif defined?(AWS_CLASS::Core::ClientLogging) elsif defined?(::Aws::Core::ClientLogging)
AWS_CLASS::Core::ClientLogging.class_eval do ::Aws::Core::ClientLogging.class_eval do
def sanitize_hash(hash) def sanitize_hash(hash)
hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',') hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
end end
...@@ -266,7 +257,7 @@ module Paperclip ...@@ -266,7 +257,7 @@ module Paperclip
def obtain_s3_instance_for(options) def obtain_s3_instance_for(options)
instances = (Thread.current[:paperclip_s3_instances] ||= {}) instances = (Thread.current[:paperclip_s3_instances] ||= {})
instances[options] ||= AWS_CLASS::S3::Resource.new(options) instances[options] ||= ::Aws::S3::Resource.new(options)
end end
def s3_bucket def s3_bucket
...@@ -323,7 +314,7 @@ module Paperclip ...@@ -323,7 +314,7 @@ module Paperclip
else else
false false
end end
rescue AWS_BASE_ERROR => e rescue Aws::Errors::ServiceError => e
false false
end end
...@@ -381,10 +372,10 @@ module Paperclip ...@@ -381,10 +372,10 @@ module Paperclip
write_options.merge!(@s3_headers) write_options.merge!(@s3_headers)
s3_object(style).upload_file(file.path, write_options) s3_object(style).upload_file(file.path, write_options)
rescue AWS_CLASS::S3::Errors::NoSuchBucket rescue ::Aws::S3::Errors::NoSuchBucket
create_bucket create_bucket
retry retry
rescue AWS_CLASS::S3::Errors::SlowDown rescue ::Aws::S3::Errors::SlowDown
retries += 1 retries += 1
if retries <= 5 if retries <= 5
sleep((2 ** retries) * 0.5) sleep((2 ** retries) * 0.5)
...@@ -407,7 +398,7 @@ module Paperclip ...@@ -407,7 +398,7 @@ module Paperclip
begin begin
log("deleting #{path}") log("deleting #{path}")
s3_bucket.object(path.sub(%r{\A/}, "")).delete s3_bucket.object(path.sub(%r{\A/}, "")).delete
rescue AWS_BASE_ERROR => e rescue Aws::Errors::ServiceError => e
# Ignore this. # Ignore this.
end end
end end
...@@ -421,7 +412,7 @@ module Paperclip ...@@ -421,7 +412,7 @@ module Paperclip
local_file.write(chunk) local_file.write(chunk)
end end
end end
rescue AWS_BASE_ERROR => e rescue Aws::Errors::ServiceError => e
warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}") warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
false false
end end
......
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