【问题标题】:How to handle file from POST requests in python如何在python中处理来自POST请求的文件
【发布时间】:2019-12-01 22:32:43
【问题描述】:

我在 python 3.7 中通过 requests.post 方法发送文件。代码很简单,如下所示,

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

请求被发送到在 AWS Lambda 上创建的处理程序,然后文件应存储在其他服务中。事件的主体似乎是文件对象的编码字符串,我找不到解码它的方法。

谢谢大家!

【问题讨论】:

  • 好吧,你用'rb'以字节模式打开它;为什么?
  • 我打算把它放在一个 s3 桶中,它以字节或文件格式接收数据;我发现的大多数示例也是以二进制模式阅读的。

标签: python python-3.x post aws-lambda python-requests


【解决方案1】:

你想要做的不是一个好主意。 Lambda 的调用负载 limit 为 6MB,因此您无法发送这样的大文件。

最好的方法是使用boto3appropriate function to upload files directly to S3

如果您真的想使用requests.post,请将文件作为字符串打开并通过邮寄方式发送,类似这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2013-06-12
    相关资源
    最近更新 更多