You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/event_handler/api_gateway.md
+75Lines changed: 75 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -523,6 +523,81 @@ In the following example, we use a new `Header` OpenAPI type to add [one out of
523
523
524
524
1. `cloudfront_viewer_country` is a list that must contain values from the `CountriesAllowed` enumeration.
525
525
526
+
#### Handling file uploads and form data
527
+
528
+
!!! info "You must set `enable_validation=True` to handle file uploads and form data via type annotation."
529
+
530
+
We use the `Annotated` type to tell the Event Handler that a parameter expects file upload or form data. This automatically sets the correct OpenAPI schema for `multipart/form-data` requests.
531
+
532
+
In the following example, we use `File` and `Form` OpenAPI types to handle file uploads and form fields:
533
+
534
+
*`File` parameters expect binary file data and generate OpenAPI schema with `format: binary`
535
+
*`Form` parameters expect form field values from multipart form data
536
+
* The OpenAPI spec will automatically set `requestBody` content type to `multipart/form-data`
537
+
538
+
=== "handling_file_uploads.py"
539
+
540
+
```python hl_lines="5 9-10 18-19"
541
+
from typing import Annotated
542
+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
543
+
from aws_lambda_powertools.event_handler.openapi.params import File, Form
file: Annotated[bytes, File(description="File to upload")],
550
+
filename: Annotated[str, Form(description="Name of the file")]
551
+
):
552
+
# file contains the binary data of the uploaded file
553
+
# filename contains the form field value
554
+
return {
555
+
"message": f"Uploaded {filename}",
556
+
"size": len(file)
557
+
}
558
+
559
+
def lambda_handler(event, context):
560
+
return app.resolve(event, context)
561
+
```
562
+
563
+
1. If you're not using Python 3.9 or higher, you can install and use [`typing_extensions`](https://pypi.org/project/typing-extensions/){target="_blank" rel="nofollow"} to the same effect
564
+
2. `File` is a special OpenAPI type for binary file uploads that sets `format: binary` in the schema
565
+
3. `Form` is a special OpenAPI type for form field values in multipart requests
566
+
567
+
=== "Multiple files"
568
+
569
+
You can handle multiple file uploads by declaring parameters as lists:
570
+
571
+
```python hl_lines="9-10"
572
+
from typing import Annotated, List
573
+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
574
+
from aws_lambda_powertools.event_handler.openapi.params import File, Form
0 commit comments