2010-09-14 288 views
0

當使用回形針+ 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配置。

回答

0

請記住,存儲到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 
+0

聽起來很酷,但這完全停止工作,所以我知道這不是一個呃逆。你的圖書館是開源的嗎?會很高興檢查出來。 – 2010-09-15 15:56:39

+0

這不是什麼圖書館,請參閱修改後的答案。 – 2010-09-15 17:50:09

0

我有兩個應用程序與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中,你需要有包括創業板或者你包括寶石。

相關問題