Installation
Install the required dependencies:- @aws-sdk/client-s3: AWS SDK v3 for S3 operations (modular and tree-shakeable).
Environment variables
Add the following environment variables to your application from the Object storage service details:Setting up the client
Basic usage examples
Upload a file
Download a file
Delete a file
List files in bucket
Generate pre-signed URL (Temporary access)
Advanced S3 operations
Multi-part upload (Large files)
Copy objects
Get object metadata
Set object ACL (Access control)
Best Practices
- Use streaming for large files - Don’t load entire files into memory
- Implement retry logic - S3 operations can fail, implement exponential backoff
- Use pre-signed URLs - For direct client uploads/downloads, reduce server load
- Set appropriate CORS - Configure bucket CORS if accessing from browser
- Use multipart upload - For files > 100MB
- Implement file validation - Check file types and sizes before upload
- Use proper naming conventions - Organize files with prefixes (folders)
- Enable versioning - For important files, enable S3 versioning
- Monitor storage costs - Use lifecycle policies to move/delete old files
- Implement error handling - Always wrap S3 operations in try-catch blocks
- Use content types - Always set the correct ContentType when uploading
- Secure credentials - Never commit S3 credentials, use environment variables
Common Issues
Access Denied Errors
If you get “Access Denied” errors:- Verify access key and secret key are correct
- Verify the bucket policy allows the operations
- Ensure credentials match the service details
Connection Timeout
If uploads/downloads timeout:- Verify S3_ENDPOINT is correct
-
Increase timeout in S3 client config:
Large File Upload Issues
For large files:- Use multipart upload for files > 100MB
- Implement progress tracking
- Handle network interruptions with retry logic
CORS Errors (Browser Uploads)
Fine-tune your bucket CORS configuration on the Sevalla dashboard.Performance Tips
-
Parallelize uploads:
-
Cache frequently accessed files:
-
Implement pagination for listing:
Security Best Practices
-
Enable encryption at rest - Use server-side encryption:
- Scan files for viruses before storing
- Implement rate limiting for upload endpoints
- Validate file types based on content, not just extension
- Use signed URLs for sensitive content