简体   繁体   中英

How to handle file from POST requests in python

I am sending file via requests.post method in python 3.7. 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. 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.

The best way is to use boto3 appropriate function to upload files directly to S3

If you really want to use requests.post , open the file as a string and send It via post, something like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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