@upload decorator creates an API that handles file uploads. It automatically parses multipart form-data requests and provides file objects. Can be used independently without @api decorator, automatically setting POST method and multipart clients (axios-multipart, tanstack-mutation-multipart).
Upload Modes
Sonamu provides two file upload modes:Buffer Mode (Default)
Buffer mode loads files into memory before processing. Use this when you need to work directly with file contents like MD5 hash calculation or image resizing.BufferedFile Object
Stream Mode
Stream mode streams files directly to storage without loading them into memory. Suitable for large file uploads.UploadedFile Object
In Stream mode, files are already uploaded to storage, so only metadata is accessible.Options
guards
Specifies guards to apply to the upload API. Use this for upload APIs that require authentication.description
Specifies the API description. Displayed in auto-generated API documentation.limits
Specifies file upload limits. Accepts Fastify multipartβslimits options directly.
- Single File (Buffer)
- Multiple Files (Buffer)
- Stream Mode
Getting File Information
File Processing (Buffer Mode)
Accessing Buffer
Saving Files
MD5 Hash Calculation
Using Storage Drivers
Sonamu provides multiple storage drivers.Storage drivers are configured in
sonamu.config.ts. Pass the disk name as the first parameter.Using with Other Decorators
With @transactional
Client Usage (Web)
Sonamu automatically generates file upload client code.Axios (Single File)
Axios (Multiple Files)
React Example
TanStack Query Example
File Validation
MIME Type Validation
File Size Validation
File Extension Validation
Image Processing
Using Sharp
Constraints
1. Independent Usage without @api
@upload is used independently without @api decorator:
2. httpMethod is POST
When using@upload, httpMethod: "POST" is automatically set.
3. Automatic clients Setting
When using@upload, the clients option is automatically set to ["axios-multipart", "tanstack-mutation-multipart"]:
Examples
- Profile Image
- Multiple File Upload
- CSV File Processing
- MD5 Hash-based Storage
- Stream Mode Large Files
Notes
Relationship with @api Decorator
The@upload decorator automatically creates an API endpoint internally. Therefore, you donβt need to use the @api decorator separately.
Automatically configured values:
httpMethod:"POST"(fixed)clients:["axios-multipart", "tanstack-mutation-multipart"](multipart-specific clients)guards:guardsvalue from@uploadoptions is passed to the APIdescription:descriptionvalue from@uploadoptions is passed to the API
@upload automatically applies optimized settings for file uploads, so thereβs no need to add the
@api decorator.Next Steps
@api
Create API endpoints
@transactional
Save safely with transactions
Storage Drivers
Use S3, Local and other storage
File Handling
Image/document processing guide