- vừa được xem lúc

Upload multipart / form-data files to S3 with Python AWS Lambda

0 0 414

Người đăng: Dinh Tung

Theo Viblo Asia

Upload multipart / form-data files to S3 with Python AWS Lambda

Overview

Upload the multipart / form-data created via Lambda on AWS to S3.

I often see implementations that send files to S3 as they are with client, and send files as Blobs, but it is troublesome and many people use multipart / form-data for normal API (I think there are many), why to be Client when I had to change it in Api and Lambda.

Repo

Click here

Too long, didn't read

  1. Multipart / form-data Change to Form using cgi module
  2. Get the file with the field name of responseBody from the Formed data

0. app.py looks like this at first

import json def lambda_handler(event, context): return { "statusCode": 200, "body": json.dumps({ "message": "hello world", }), }

1. Multipart / form-data Change the created data to Form data using cgi module

import json
import cgi
import io import logging # これは個人的に入れているのでほっておいていい
import base64 def get_file_from_request_body(headers, body): fp = io.BytesIO(base64.b64decode(body)) # decode environ = {"REQUEST_METHOD": "POST"} headers = { "content-type": headers["Content-Type"], "content-length": headers["Content-Length"], } fs = cgi.FieldStorage(fp=fp, environ=environ, headers=headers) # FieldStorageを利用してForm Dataとして扱う def lambda_handler(event, context): return { "statusCode": 200, "body": json.dumps({ "message": "hello world", }), }

2. Get the file with the field name of responseBody from the Formed data

It is assumed that the form of request is as follows.

  • request.ts
{ "file": File
}
import json
import cgi
import io import logging
import base64 def get_file_from_request_body(headers, body): fp = io.BytesIO(base64.b64decode(body)) # decode environ = {"REQUEST_METHOD": "POST"} headers = { "content-type": headers["Content-Type"], "content-length": headers["Content-Length"], } fs = cgi.FieldStorage(fp=fp, environ=environ, headers=headers) return [fs["file"], None] def lambda_handler(event, context): file_item, file_item_error = get_file_from_request_body( headers=event["headers"], body=event["body"] ) return { "statusCode": 200, "body": json.dumps({ "message": "hello world", }), }

It responded appropriately like this, but I think there is probably a better way to write it ...

Bình luận

Bài viết tương tự

- vừa được xem lúc

Cài đặt WSL / WSL2 trên Windows 10 để code như trên Ubuntu

Sau vài ba năm mình chuyển qua code trên Ubuntu thì thật không thể phủ nhận rằng mình đã yêu em nó. Cá nhân mình sử dụng Ubuntu để code web thì thật là tuyệt vời.

0 0 374

- vừa được xem lúc

Phần 1: Giới thiệu về Kubernetes

Kubernetes là gì. Trang chủ: https://kubernetes.io/. Ai cần Kubernetes.

0 0 80

- vừa được xem lúc

Docker: Chưa biết gì đến biết dùng (Phần 1- Lịch sử)

1. Vì sao nên sử dụng. . .

0 0 89

- vừa được xem lúc

Docker - những kiến thức cơ bản phần 1

Giới thiệu. Nếu bạn đang làm ở một công ty công nghệ thông tin, chắc rằng bạn đã được nghe nói về Docker.

0 0 65

- vừa được xem lúc

Docker: Chưa biết gì đến biết dùng (Phần 2 - Dockerfile)

1. Mở đầu.

0 0 53

- vừa được xem lúc

Docker: Chưa biết gì đến biết dùng (Phần 3: Docker-compose)

1. Mở đầu. . .

0 0 106