S3 Storage Adapter
The S3 storage adapter enables GUN to store data in Amazon S3 or any S3-compatible storage service (DigitalOcean Spaces, MinIO, Wasabi, etc.). This provides durable, scalable cloud storage for distributed applications.Installation
First, install the AWS SDK:Configuration Options
The S3 adapter accepts the following configuration options:Using Environment Variables
The adapter automatically reads from environment variables:Key Options
bucket (string, required)
Name of the S3 bucket where data will be stored.
region (string)
AWS region for your bucket.
Default: 'us-east-1'
accessKeyId / key (string)
AWS access key ID for authentication.
secretAccessKey / secret (string)
AWS secret access key for authentication.
fakes3 / endpoint (string)
Custom endpoint for S3-compatible services.
AWS Setup
Creating an S3 Bucket
- Go to AWS S3 Console
- Click “Create bucket”
- Enter bucket name (e.g., “my-gun-data”)
- Select a region
- Configure options:
- Block all public access: Enabled (recommended)
- Versioning: Optional
- Encryption: Optional
- Click “Create bucket”
Creating IAM Credentials
- Go to AWS IAM Console
- Navigate to “Users” > “Add users”
- Enter username (e.g., “gun-app”)
- Select “Programmatic access”
- Attach policy:
- Save the Access Key ID and Secret Access Key
Using IAM Roles (EC2, Lambda, ECS)
For applications running on AWS infrastructure, use IAM roles instead of credentials:How S3 Storage Works
Storage Structure
Data is stored as individual objects in S3:Caching Layer
The S3 adapter includes a built-in caching layer to minimize API calls:- Reduces S3 API calls (and costs)
- Improves read/write performance
- Batches multiple requests
Write Operations
Read Operations
S3-Compatible Services
DigitalOcean Spaces
DigitalOcean Spaces is an S3-compatible object storage service:nyc3- New York 3ams3- Amsterdam 3sgp1- Singapore 1sfo3- San Francisco 3fra1- Frankfurt 1
MinIO (Self-Hosted)
MinIO is a self-hosted S3-compatible storage server:Wasabi
Wasabi is a low-cost S3-compatible storage:Backblaze B2
Backblaze B2 offers S3-compatible API:Mixed Storage (S3 + Local)
Combine S3 with local storage for better performance:Cost Optimization
Minimizing S3 Requests
S3 charges per request. Optimize by:-
Using the cache: The adapter already caches, but batch your writes:
-
Configuring lifecycle rules: Archive old data to S3 Glacier:
- In S3 Console, go to Management > Lifecycle rules
- Transition objects to Glacier after 30+ days
- Using Intelligent-Tiering: Automatically moves data between access tiers
Estimated Costs (AWS S3)
For a typical GUN application:Performance Tuning
Parallel Operations
The S3 adapter batches requests automatically:Prefetching Data
For known access patterns, prefetch data:Transfer Acceleration
Enable S3 Transfer Acceleration for faster uploads:Security Best Practices
Bucket Permissions
Encryption at Rest
Enable default encryption for your bucket:- Go to S3 Console
- Select your bucket
- Properties > Default encryption
- Choose AES-256 (SSE-S3) or AWS KMS
Encryption in Transit
Always use SSL:Credential Rotation
Rotate AWS credentials regularly:Monitoring and Debugging
Enable S3 Request Logging
Log all S3 requests for debugging:CloudWatch Metrics
Monitor S3 metrics in AWS CloudWatch:- Request count
- Error rate
- Latency
- Bucket size
Error Handling
Troubleshooting
”Please npm install aws-sdk”
Install the AWS SDK:
“AccessDenied” Errors
Check your IAM policy includes:s3:GetObjects3:PutObjects3:ListBucket
”NoSuchBucket” Errors
Ensure the bucket exists and region is correct:Connection Timeouts
Increase timeout for slow connections:Best Practices
- Use environment variables for credentials
- Enable versioning for production buckets
- Set up lifecycle rules to manage costs
- Monitor usage with CloudWatch
- Encrypt at rest and in transit
- Use IAM roles when possible
- Enable Transfer Acceleration for global apps
- Combine with local storage for performance
Next Steps
RADisk Adapter
Local file system storage
Custom Adapters
Build your own storage adapter
Distributed Systems
Build distributed GUN applications
Security
Secure your GUN application