Skip to main content

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:
Then require the S3 adapter:

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

  1. Go to AWS S3 Console
  2. Click “Create bucket”
  3. Enter bucket name (e.g., “my-gun-data”)
  4. Select a region
  5. Configure options:
    • Block all public access: Enabled (recommended)
    • Versioning: Optional
    • Encryption: Optional
  6. Click “Create bucket”

Creating IAM Credentials

  1. Go to AWS IAM Console
  2. Navigate to “Users” > “Add users”
  3. Enter username (e.g., “gun-app”)
  4. Select “Programmatic access”
  5. Attach policy:
  6. 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:
Attach this policy to your EC2 instance role, Lambda function role, or ECS task role.

How S3 Storage Works

Storage Structure

Data is stored as individual objects in S3:
Each object contains JSON-encoded data:

Caching Layer

The S3 adapter includes a built-in caching layer to minimize API calls:
Benefits:
  • 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:
Available regions:
  • nyc3 - New York 3
  • ams3 - Amsterdam 3
  • sgp1 - Singapore 1
  • sfo3 - San Francisco 3
  • fra1 - 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:
  1. Using the cache: The adapter already caches, but batch your writes:
  2. Configuring lifecycle rules: Archive old data to S3 Glacier:
    • In S3 Console, go to Management > Lifecycle rules
    • Transition objects to Glacier after 30+ days
  3. 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:
  1. Go to S3 Console
  2. Select your bucket
  3. Properties > Default encryption
  4. 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:GetObject
  • s3:PutObject
  • s3:ListBucket

”NoSuchBucket” Errors

Ensure the bucket exists and region is correct:

Connection Timeouts

Increase timeout for slow connections:

Best Practices

  1. Use environment variables for credentials
  2. Enable versioning for production buckets
  3. Set up lifecycle rules to manage costs
  4. Monitor usage with CloudWatch
  5. Encrypt at rest and in transit
  6. Use IAM roles when possible
  7. Enable Transfer Acceleration for global apps
  8. 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