Heroku上的django-import-export出现临时文件错误

yquaqz18  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(134)

我正在使用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有一个模块,允许你重载临时文件的创建机制--但是我甚至不确定这里出了什么问题(或者,更确切地说,为什么/tmp/tmpvCUtrP没有被创建或者不可见)。

yb3bgrhw

yb3bgrhw1#

django-import-export将导入过程拆分为几个对服务器的请求。
Heroku可以使用多个dynos,它们不共享/tmp中的公共文件系统。
一个初始的请求可能会在dyno A上执行,并在/tmp/tmpfilename中创建一个临时文件,然后一个后续的请求可能会在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
yptwkmov

yptwkmov2#

  • deadcode* 回答中的根本原因分析是正确的。在高速缓存配置不正确但default_storage正确的环境中(例如,对于S3),您也可以使用以下方法:
from import_export.tmp_storages import MediaStorage

@admin.register(MyModel)
class MyModelAdmin(ImportExportMixin, admin.ModelAdmin):
    tmp_storage_class = MediaStorage

相关问题