AWS :: S3 :: NoConnection使用Rails中的aws-s3 gem建立的错误
问题描述:
当使用回形针+ s3尝试下载文件时,出现AWS :: S3 :: NoConnectionEstablished异常。我可以启动s3sh并使用我的配置中的s3凭证创建连接。调试此问题的最佳下一步是什么?这是我的模型看起来像:AWS :: S3 :: NoConnection使用Rails中的aws-s3 gem建立的错误
has_attached_file :file,
:storage => :s3,
:s3_permssions => :private,
:path => lambda { |attachment| ":id_partition/:basename.:extension" },
:url => lambda { |attachment| "products/:id/:basename.:extension" },
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:bucket => "products.mycompany.com"
,且错误发生在这里:
def temporary_s3_url(options={})
options.reverse_merge! :expires_in => 10.minutes #, :use_ssl => true
hard_url = AWS::S3::S3Object.url_for file.path, file.options[:bucket], options
# Use our vanity URL
hard_url.gsub("http://s3.amazonaws.com/products.mycompany.com","http://products.mycompany.com")
end
我试着硬编码作为temporary_s3_url
方法第一线的连接,但我得到没有找到一个“水桶“错误。我认为问题肯定是回形针有问题初始化我的S3配置。
答
请记住,存储到S3是不可靠的 - 连接可能会丢失,店内完成之前失败,等等
我创造了我自己的库程序试图做实体店的,但赶上各种错误。对于没有连接错误,我重新连接。对于其他存储错误,我重试(最多三次)。您可能还想在重试之间等待一秒钟。
新增
下面是库例程我使用AWS调用。
您需要添加/修改救援条款以捕捉您遇到的错误。您的connection_reset和错误报告方法也将特定于您的sw。
# Usage example:
# aws_repeat("Storing #{bucket}/#{obj}"){
# AWS::S3::S3Object.store(obj, data, bucket, opt)}
def aws_repeat(description = nil)
# Calls the block up to 3 times, allowing for AWS connection reset problems
for i in 1..3
begin
yield
rescue Errno::ECONNRESET => e
ok = false
ActiveRecord::Base.logger.error \
"AWS::S3 *** Errno::ECONNRESET => sleeping"
sleep(1)
if i == 1
# reset connection
connect_to_aws # re-login in to AWS
ActiveRecord::Base.logger.error \
"AWS::S3 *** Errno::ECONNRESET => reset connection"
end
else
ok = true
break
end
end
unless ok
msg = "AWS::S3 *** FAILURE #{description.to_s}"
ActiveRecord::Base.logger.error msg
security_log(msg)
end
ok
end
############################################
############################################
def connect_to_aws
# load params. Cache at class (app) level
@@s3_config_path ||= RAILS_ROOT + '/config/amazon_s3.yml'
@@s3_config ||=
YAML.load_file(@@s3_config_path)[ENV['RAILS_ENV']].symbolize_keys
AWS::S3::Base.establish_connection!(
:access_key_id => @@s3_config[:access_key_id],
:secret_access_key => @@s3_config[:secret_access_key],
:server => @@s3_config[:server],
:port => @@s3_config[:port],
:use_ssl => @@s3_config[:use_ssl],
:persistent => false # from http://www.ruby-forum.com/topic/110842
)
true
end
答
我有两个应用程序与S3和Heroku的回形针。这是对我工作:
在你的模式:
has_attached_file :image,
:styles => { :thumb => "250x250>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "username/:attachment/:style/:id.:extension"
在配置
/s3.yml
development:
bucket: name
access_key_id: xyz
secret_access_key: xyz
test:
bucket: name
access_key_id: xyz
secret_access_key: xyz
production:
bucket: name
access_key_id: xyz
secret_access_key: xyz
,当然在你的environment.rb中,你需要有包括创业板或者你包括宝石。
听起来很酷,但这完全停止工作,所以我知道这不是一个呃逆。你的图书馆是开源的吗?会很高兴检查出来。 – 2010-09-15 15:56:39
这不是什么图书馆,请参阅修改后的答案。 – 2010-09-15 17:50:09