Heroku删除我上传的文件,为什么?

时间:2013-01-25 09:13:22

标签: ruby-on-rails ruby-on-rails-3 heroku yaml

如果我在Heroku上的应用程序中加载文件,一切正常,但当我再次尝试重新加载应用程序时,它会给我以下错误:

2013-01-25T08:48:31+00:00 app[web.1]:   app/controllers/main_controller.rb:20:in `index'
2013-01-25T08:48:31+00:00 app[web.1]: 
2013-01-25T08:48:31+00:00 app[web.1]: 
2013-01-25T08:48:31+00:00 app[web.1]: Errno::ENOENT (No such file or directory - /app/config/cases/casesID6.yml):
2013-01-25T08:48:31+00:00 app[web.1]:   app/controllers/main_controller.rb:20:in `read'

本地工作很精细!

主控制器:

# importo yaml di configurazione
require 'yaml'
if Survey.exists?(1)
    @idOfSurvey = Survey.find {|s| s['active_state'] == true}['id']
    nameOfSurvey = "casesID"+String(@idOfSurvey)+".yml"
    @survey = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config/cases', nameOfSurvey))).result)
else
    render :action => 'noYaml' and return
end

上传控制器:

#inserisco il nuovo questionario
            survey = Survey.new
            if Survey.exists?(1)
                n = String(Integer(Survey.maximum("Id"))+1)
                survey.name = "casesID#{n}"
            else
                survey.name = "casesID1"
            end
            File.open(Rails.root.join('config/cases', survey.name+".yml"), 'wb+') do |file|
                file.write(uploaded_io.read)
            end
            survey.save()

我相信我上传到Heroku的文件可能会被平台清除,例如内存问题,或者因为它将它们保存为临时文件,这可能吗?方案!

我再说一遍,一切都在当地运作良好:(

1 个答案:

答案 0 :(得分:9)

虽然您可以在扩展,重新启动或推送新版本的代码后写入Heroku文件系统,但文件将会消失。

您需要使用持久性文件存储,例如Amazon S3,Rackspace等 - 在https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem阅读更多内容并使用像CarrierWave或Paperclip这样的宝石,这样可以使S3更加简单。

相关问题