S3 File Storage & Image Resizing
Relevant source files
The following files were used as context for generating this wiki page:
Polar utilizes AWS S3 (or S3-compatible storage like MinIO for local development) to manage, store, and serve files. This system is primarily designed to handle hosted downloads for benefits, organization assets, and user-generated content. The architecture supports multi-part uploads, presigned URLs for secure access, and integration with S3-compatible services.
The system is structured to separate the storage logic (handled via S3 services) from the business logic (managed by the File Service). This ensures that files are securely uploaded, tracked in the PostgreSQL database, and accessible only to authorized entities.
Sources: server/polar/file/README.md:1-4, DEVELOPMENT.md:10-25
System Architecture
The file storage system is integrated into the Polar backend, interacting with the REST API, the worker queue, and the S3 storage layer.
The diagram shows the flow of file metadata through the internal repository to the database, while the actual binary data is transmitted directly between the client/API and the S3 storage layer.
Sources: DEVELOPMENT.md:15-28, server/polar/file/service.py
S3 Storage Implementation
Polar defines specific S3 services to manage different file types. For local development, MinIO is used as an S3-compatible replacement.
Configuration & Security
The S3 implementation relies on standard AWS IAM policies and CORS configurations to ensure secure uploads and downloads. Secure defaults include blocking all public access and strictly defining allowed origins and methods.
| Configuration Item | Requirement / Value |
|---|---|
| Allowed Methods | PUT, HEAD |
| Allowed Origins | http://127.0.0.1:3000 (Development) |
| S3 Actions | PutObject, GetObject, DeleteObject, GetObjectAttributes |
| Bucket Policy | Restricted to specific IAM User per bucket |
Sources: server/polar/file/README.md:11-45
S3 Service Interaction
The backend uses boto3 for interacting with S3. For local development, the S3_ENDPOINT_URL is typically pointed to a local MinIO instance (e.g., http://127.0.0.1:9000).
# Example S3 Client configuration for testing
s3 = boto3.resource(
"s3",
endpoint_url=settings.S3_ENDPOINT_URL,
aws_access_key_id=settings.MINIO_USER,
aws_secret_access_key=settings.MINIO_PWD,
config=Config(signature_version=settings.AWS_SIGNATURE_VERSION),
)
Sources: server/tests/fixtures/file.py:210-218
File Upload Workflow
Polar supports a sophisticated multi-part upload workflow to handle large files reliably. This process involves generating presigned URLs that allow clients to upload data directly to S3 without proxying through the Polar API.
Upload Sequence Diagram
This sequence ensures that the Polar backend only handles file metadata and orchestration, while S3 handles the heavy lifting of data transfer.
Sources: server/tests/fixtures/file.py:82-160, server/polar/file/service.py
Multi-part Upload Components
- Presigned Upload Generation: The
generate_presigned_uploadfunction creates the initial file record and requests an upload ID from S3. - Part Management: For large files, the file is split into parts. Each part receives its own presigned URL and checksum (SHA256 Base64).
- Completion: Once all parts are uploaded to S3, the client sends the
FileUploadCompletedschema containing the ETags for all parts to thecomplete_uploadservice.
Sources: server/tests/fixtures/file.py:82-124, server/tests/fixtures/file.py:165-175
File Service Logic
The FileService acts as the coordinator between the database (via FileRepository) and the S3 storage.
Key Data Structures
The system uses several Pydantic schemas to validate and manage file data:
DownloadableFileCreate: Metadata for creating a new downloadable file.FileUpload: Contains the presigned URLs and upload ID returned to the client.FileUploadCompleted: Used to finalize the upload process.S3FileUploadCompletedPart: Stores individual part identifiers (number and ETag).
Sources: server/tests/fixtures/file.py:20-35
Metadata Management
When a file upload is completed, the service attaches custom metadata to the S3 object for internal tracking:
polar-id: The UUID of the file in Polar's database.polar-organization-id: The organization owning the file.polar-name: The original filename.polar-size: The file size in bytes.
Sources: server/tests/fixtures/file.py:180-188
Development and Testing
For developers, Polar provides a robust testing environment using local S3-compatible storage.
Local Infrastructure
Local development uses Docker Compose to spin up MinIO.
- MinIO API: http://localhost:9000
- MinIO Console: http://localhost:9001
Sources: DEVELOPMENT.md:145-155, DEVELOPMENT.md:255-265
Test Fixtures
The testing suite includes a TestFile utility class in server/tests/fixtures/file.py that automates:
- Generating SHA256 checksums for test assets.
- Simulating multi-part uploads via
httpx.AsyncClient. - Validating S3 object state after completion (Content-Type, Content-Length, and Custom Metadata).
Sources: server/tests/fixtures/file.py:40-100
Summary
The S3 File Storage system in Polar is a production-grade implementation that leverages S3's multi-part upload capabilities for efficiency and security. By using presigned URLs and direct-to-S3 uploads, the system minimizes backend load while maintaining strict control over file metadata and access through the PostgreSQL database. This architecture supports both cloud-native AWS deployments and local-first development with MinIO.