简体   繁体   English

如何在python中处理来自POST请求的文件

[英]How to handle file from POST requests in python

I am sending file via requests.post method in python 3.7. 我正在通过python 3.7中的request.post方法发送文件。 The code is something as simple as the following, 代码很简单,如下所示:

with open('filename','rb') as data:
    r = requests.post(url, data)

The request is sent to a handler created on AWS Lambda, where the file should then be stored in other services. 该请求将发送到在AWS Lambda上创建的处理程序,然后将该文件存储在其他服务中。 The body of the event seems to be an encoded string of the file object and I can't find a way to decode it. 事件的主体似乎是文件对象的编码字符串,我找不到解码它的方法。

Thanks guys! 多谢你们!

What you're trying to do is not a great ideia. 您尝试做的并不是一个伟大的想法。 Lambda has Invocation payload limit of 6MB, so you can't send large files like this. Lambda的调用有效负载限制为6MB,因此您无法发送此类大文件。

The best way is to use boto3 appropriate function to upload files directly to S3 最好的方法是使用适当的 boto3 功能将文件直接上传到S3

If you really want to use requests.post , open the file as a string and send It via post, something like that: 如果您确实要使用requests.postrequests.post以字符串形式打开文件,然后通过post发送该文件,如下所示:

with open('file.txt', 'r') as file:
    STRING_FILE = file.read().replace('\n', '')
    r = requests.post(<URL>, data = {'key':STRING_FILE})

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

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