https://github.com/developmentseed/asgi-s3-response-middleware
ASGI Middleware to spill large responses over to S3.
https://github.com/developmentseed/asgi-s3-response-middleware
asgi fastapi fastapi-middleware s3 starlette starlette-middleware
Last synced: 11 months ago
JSON representation
ASGI Middleware to spill large responses over to S3.
- Host: GitHub
- URL: https://github.com/developmentseed/asgi-s3-response-middleware
- Owner: developmentseed
- License: mit
- Created: 2024-09-04T02:33:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-05T16:48:52.000Z (over 1 year ago)
- Last Synced: 2025-05-20T22:35:05.417Z (about 1 year ago)
- Topics: asgi, fastapi, fastapi-middleware, s3, starlette, starlette-middleware
- Language: Python
- Homepage:
- Size: 54.7 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ASGI S3 Response Middleware
Middleware to spill large responses over to S3.
---
**Source Code**: https://github.com/developmentseed/asgi-s3-response-middleware
---
## Usage
An ASGI middleware class to automatically push repsonses S3 and instead return a 303 redirect to the object on S3.
This can be useful to avoid hitting limits on the size of API response bodies, such as when working around [AWS Lambda's 6MB response limit](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html).
### Example
```py
import uuid
import boto3
from fastapi import FastAPI
from asgi_s3_response_middleware import S3ResponseMiddleware
s3_client = boto3.client('s3')
app = FastAPI()
app.add_middleware(
S3ResponseMiddleware,
s3_bucket_name='my-example-bucket',
s3_client=s3_client,
key_generator=lambda: f"responses/{uuid.uuid4()}",
size_threshold=2 * 1024**2, # 2MB
url_expiry=30, # 30 seconds
)
```
## Development
### Releases
Releases are managed via CICD workflow, as described in the [Python Packaging User Guide](https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/). To create a new release:
1. Update the version in `src/asgi_s3_response_middleware/__init__.py` following appropriate [Semantic Versioning convention](https://semver.org/).
1. Push a tagged commit to `main`, with the tag matching the package's new version number.
> [!NOTE]
> This package makes use of Github's [automatically generated release notes](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes). These can be later augmented if one sees fit.