简体   繁体   English

AWS Elastic BeansTalk Django cronjob发布请求返回403错误

[英]AWS Elastic BeansTalk Django cronjob post request returning 403 error

I'm working on a software function in which I have to delete files periodically using Django + cron + AWS . 我正在开发一个软件功能,我必须使用Django + cron + AWS定期删除文件。 The problem is I can't make it work. 问题是我无法使其发挥作用。 What's the best way to make it work? 什么是让它发挥作用的最佳方法? Am I missing some AWS configuration? 我错过了一些AWS配置吗? I've configured one web server and one worker environment, deployed the same application version on them. 我已经配置了一个Web服务器和一个工作环境,在它们上部署了相同的应用程序版本。 The task is a view mapped into a url (accessing the url the function is executed). 任务是映射到url视图 (访问url函数执行)。 There's a confirmation message on the worker environment: 工作者环境有一条确认消息:

Successfully loaded 1 scheduled tasks from cron.yaml. 从cron.yaml成功加载了1个计划任务。

But also an 403 error on the worker access_log : 但是对于worker access_log也有403错误:

"POST /networks_app/delete_expired_files HTTP/1.1" 403 2629 "-" "aws-sqsd/2.0" “POST / networks_app / delete_expired_files HTTP / 1.1”403 2629“ - ”“aws-sqsd / 2.0”

cron.yaml : cron.yaml

version: 1
cron:
 - name: "delete_expired_files"
   url: "/networks_app/delete_expired_files"
   schedule: "10 * * * *"

url mapping at urls.py : urls.py上的 url映射:

urlpatterns = [
    url(r'^delete_expired_files', views.delete_expired_files, name='delete_expired_files'),
]

function to delete files at views.py : 用于删除views.py文件的函数:

def delete_expired_files(request):
    users = DemoUser.objects.all()
    for user in users:
        documents = Document.objects.filter(owner=user.id)
        if documents:
            for doc in documents:
                now = timezone.now()
                if now >= doc.date_published + timedelta(days=doc.owner.group.valid_time):
                    doc.delete()

My IAM roles are: 我的IAM角色是:

AmazonSQSFullAccess AmazonSQSFullAccess

AmazonS3FullAccess AmazonS3FullAccess

AWSElasticBeanstalkFullAccess AWSElasticBeanstalkFullAccess

AmazonDynamoDBFullAccess AmazonDynamoDBFullAccess

If I access the url via browser, the task is executed (the expired files are deleted). 如果我通过浏览器访问URL,则执行任务(删除过期的文件)。 However, the worker environment was supposed to access the url and execute the task automatically and not only when I access the url via browser. 但是,工作者环境应该访问URL并自动执行任务,而不仅仅是当我通过浏览器访问URL时。 How can I make it work? 我怎样才能使它工作?

I had a similar issue. 我有一个类似的问题。 In my case, I needed to modify 2 things to get it to work: 在我的情况下,我需要修改两件事来让它工作:

  1. Ensure the view is set up to accept a POST action from AWS. 确保将视图设置为接受来自AWS的POST操作。 Previously I had mine set up as GET only, and it doesn't seem that AWS supports GET cron requests. 以前我只将我的设置为GET,并且AWS似乎不支持GET cron请求。

  2. Once it supports POST, make it CSRF-exempt, so that Django isn't afraid that there's a CSRF threat taking place when AWS makes POST requests lacking a CSRF token . 一旦它支持POST,使其免于CSRF,这样当AWS发出缺少CSRF令牌的POST请求时,Django并不担心会发生CSRF威胁 You can use the @csrf_exempt decorator described at this SO answer ; 您可以使用本SO答案中描述的@csrf_exempt装饰器; in my case, it was slightly more complicated still by my using a class-based view, and I found this other SO answer on how to include the @csrf_exempt decorator on a class-based view. 在我的情况下,我使用基于类的视图仍然稍微复杂一点,我发现了另一个关于如何在基于类的视图中包含@csrf_exempt装饰器的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM