Heroku上的django-import-export出现Tmpfile错误

时间:2017-02-25 01:26:00

标签: python django heroku django-import-export

我正在使用django-import-export来处理上传到我的Django管理网站的CSV文件。

当我在本地机器上运行Django时,一切正常。

当我将我的应用程序部署到Heroku时,我开始收到与tmpfile访问相关的错误:

Feb 24 14:35:12 test-staging app/web.3:  ERROR 2017-02-24 22:35:12,143 base 14 139687073408832 Internal Server Error: 
....
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/admin.py", line 163, in process_import 
Feb 24 14:35:12 test-staging app/web.3:      data = tmp_storage.read(input_format.get_read_mode()) 
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/tmp_storages.py", line 42, in read 
Feb 24 14:35:12 test-staging app/web.3:      with self.open(mode=mode) as file: 
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/tmp_storages.py", line 31, in open 
Feb 24 14:35:12 test-staging app/web.3:      return open(self.get_full_path(), mode) 
Feb 24 14:35:12 test-staging app/web.3:  IOError: [Errno 2] No such file or directory: u'/tmp/tmpvCUtrP' 

我已经阅读了关于Heroku短暂存储的内容,看起来这应该可行。我已经验证了我可以通过heroku run使用我的代码在heroku dyno上创建,查看和修改/ tmp中的文件。

django-import-export有一个允许你重载tempfile创建机制的模块 - 但是我甚至不确定这里有什么问题(或者更确切地说,为什么/ tmp / tmpvCUtrP没有被创建或者不是可见)。

1 个答案:

答案 0 :(得分:5)

django-import-export将导入过程分成几个请求到服务器。

Heroku可以使用多个dynos,它们不共享/ tmp中的公共文件系统。

初始请求可以得到dyno A并在/ tmp / tmpfilename中创建tmp文件,然后稍后的请求可以转到dyno B,而django-import-export需要/ tmp / tmpfilename存在 - 但是它不是。

为了解决这个问题,我改用了django-import-export的CacheStorage临时存储方法:

from import_export.tmp_storages import CacheStorage

class CustomAdminForm(ImportExportModelAdmin):
    tmp_storage_class = CacheStorage