2012-08-31 27 views
0

我有一个问题(使用csrf)使用POST执行直接上传到S3。如何通过POST绕过CRFSMIDDLEWARE错误到Amazon S3?

下面是代码:

<form action="https://mymediaurl/" method="post" enctype="multipart/form-data"> 
    <input type="hidden" name="key" value="uploads/${filename}"> 
    <input type="hidden" name="AWSAccessKeyId" value="{{ access_key }}"> 
    <input type="hidden" name="acl" value="private"> 
    <input type="hidden" name="success_action_redirect" value="http://localhost/"> 
    <input type="hidden" name="policy" value="{{ policy }}"> 
    <input type="hidden" name="signature" value="{{ signature }}"> 
    <input type="hidden" name="Content-Type" value="image/jpeg"> 
    <!-- Include any additional input fields here --> 

    File to upload to S3: 
    <input name="file" type="file"> 
    <br> 
    <input type="submit" value="Upload File to S3"> 
</form> 

以下是错误:

<form action="https://mymediaurl/" method="post" enctype="multipart/form-data"> 
    <div style='display:none;'><input type='hidden' id='csrfmiddlewaretoken' name='csrfmiddlewaretoken' value='123412341234' /></div> 
    <div style='display:none;'><input type='hidden' id='csrfmiddlewaretoken' name='csrfmiddlewaretoken' value='123412341234' /></div> 
    <input type="hidden" name="key" value="uploads/${filename}"> 
    <input type="hidden" name="AWSAccessKeyId" value="ASFDFDSF"> 
    <input type="hidden" name="acl" value="private"> 
    <input type="hidden" name="success_action_redirect" value="http://localhost/"> 
    <input type="hidden" name="policy" value="asdhfkajewhlfawe"> 
    <input type="hidden" name="signature" value="asdfasdfasdf"> 
    <input type="hidden" name="Content-Type" value="image/jpeg"> 
    <!-- Include any additional input fields here --> 

    File to upload to S3: 
    <input name="file" type="file"> 
    <br> 
    <input type="submit" value="Upload File to S3"> 
</form> 

我曾尝试加入csrfmiddlewaretoken我的政策:

包括捷克斯洛伐克
<Code>AccessDenied</Code> 
<Message> 
    Invalid according to Policy: Extra input fields: csrfmiddlewaretoken 
</Message> 

源代码文件,但这不起作用。有没有人遇到这个问题,并找到解决方案?我搜索了高和低,但似乎无法找到这个具体问题的答案。

运行Django 1.3.1这个项目,如果该事项..

+0

为什么不使用s3boto存储或django-storage与AWS有很好的集成?这样,您可以将AWS密钥等设置为设置文件,而不必以任何形式对其进行处理 – karthikr

+0

@karthikr感谢您的答复。我现在实际上使用s3boto,但我想尝试跳过本地存储并将这些文件直接发送到s3。该文件都在200 + MB附近,我不想打我的服务器。 – Lonoshea

+1

确保您的政策正确格式化了“csrfmiddlewaretoken”?具体来说,我想到这里提到的'$':https://forums.aws.amazon.com/message.jspa?messageID=188006 – Christopher

回答

0

回答我自己与克里斯托弗的评论帮助的问题。

这里是我的政策文件:

{"expiration": "2014-01-01T00:00:00Z", 
"conditions": [ 
    {"bucket": "media.somehost.com"}, 
    ["starts-with", "$key", "uploads/"], 
    ["starts-with", "$csrfmiddlewaretoken", ""], 
    {"acl": "private"}, 
    {"success_action_redirect": "http://localhost/"}, 
    ["starts-with", "$Content-Type", ""], 
    ["content-length-range", 0, 1048576] 
] 

我只需要在csrfmiddlewaretoken用正确的格式添加到我的政策。