【问题标题】:django on heroku: celery worker gets 403 forbidden when accessing s3 media to read and process media filesheroku 上的 django:celery worker 在访问 s3 媒体以读取和处理媒体文件时被禁止 403
【发布时间】:2020-10-05 09:07:02
【问题描述】:

我真的坚持这个,因为我不知道从哪里开始:

我的 Django 项目允许用户上传电子表格,然后应用程序会处理和汇总上传的数据。

使用标准表单和带有 FileField 的 Django 模型将文件上传到 MEDIA_URL。 上传后,celery worker 访问文件并对其进行处理,将输出写入另一个模型。

这在本地工作正常,但在生产中不起作用。我正在部署到heroku,并使用cookiecutter-django 项目模板。我已经设置了一个 s3 存储桶,并且正在使用 django-storages 库。

文件上传没有问题 - 我可以在 Django 管理员以及 s3 存储桶中访问和删除它们。

但是,当 celery worker 尝试读取文件时,我得到一个 HTTP Error 403: Forbidden。 我不确定如何解决这个问题,因为我不确定堆栈的哪一部分包含我的错误。会不会是我的tasks.py 模块、heroku:redis 插件或settings.py 模块?

【问题讨论】:

    标签: django heroku amazon-s3 celery django-media


    【解决方案1】:

    有必要告诉 celery 从哪里获取它的配置(使用哪个设置文件)。部署时我没有将配置更新为生产设置。

    这是我固定的celery_app.py

    import os
    
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
    
    app = Celery("<project_name>")
    
    # Using a string here means the worker doesn't have to serialize
    # the configuration object to child processes.
    # - namespace='CELERY' means all celery-related configuration keys
    #   should have a `CELERY_` prefix.
    app.config_from_object("django.conf:settings", namespace="CELERY")
    
    # Load task modules from all registered Django app configs.
    app.autodiscover_tasks()
    

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2019-12-22
      • 2018-02-20
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2014-05-05
      • 2020-12-23
      相关资源
      最近更新 更多