This directory contains Node.js examples demonstrating how to integrate Garage S3 into your applications.
cd examples/nodejs
npm install
Set environment variables before running examples:
export GARAGE_ENDPOINT="http://192.168.1.100:3900"
export GARAGE_REGION="garage"
export GARAGE_ACCESS_KEY="your-access-key"
export GARAGE_SECRET_KEY="your-secret-key"
simple-upload.js)Basic S3 operations using AWS SDK v3.
Features:
Run:
node simple-upload.js
express-middleware.js)File upload API using Express and Multer.
Features:
Run:
node express-middleware.js
Test:
# Upload single file
curl -F "file=@test.txt" http://localhost:3000/upload
# Upload multiple files
curl -F "files=@file1.txt" -F "files=@file2.txt" http://localhost:3000/upload-multiple
# List files
curl http://localhost:3000/files
# Delete file
curl -X DELETE http://localhost:3000/files/1234567890-test.txt
# Health check
curl http://localhost:3000/health
multipart-upload.js)Efficient large file uploads with progress tracking.
Features:
Run:
node multipart-upload.js /path/to/large/file.zip
When to use:
All examples use AWS SDK v3 with the following configuration:
import { S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({
endpoint: process.env.GARAGE_ENDPOINT,
region: process.env.GARAGE_REGION,
credentials: {
accessKeyId: process.env.GARAGE_ACCESS_KEY,
secretAccessKey: process.env.GARAGE_SECRET_KEY
},
forcePathStyle: true // Required for Garage
});
Important: Always set forcePathStyle: true when connecting to Garage.
import { PutObjectCommand } from '@aws-sdk/client-s3';
const command = new PutObjectCommand({
Bucket: 'my-bucket',
Key: 'file.txt',
Body: fileContent
});
await s3Client.send(command);
import { GetObjectCommand } from '@aws-sdk/client-s3';
const command = new GetObjectCommand({
Bucket: 'my-bucket',
Key: 'file.txt'
});
const response = await s3Client.send(command);
const content = await response.Body.transformToString();
import { ListObjectsV2Command } from '@aws-sdk/client-s3';
const command = new ListObjectsV2Command({
Bucket: 'my-bucket'
});
const response = await s3Client.send(command);
console.log(response.Contents);
try {
await s3Client.send(command);
} catch (error) {
if (error.name === 'NoSuchBucket') {
console.error('Bucket does not exist');
} else if (error.name === 'NoSuchKey') {
console.error('Object not found');
} else {
console.error('S3 error:', error.message);
}
}
Check that your credentials are correct and that the endpoint URL is accessible.
Verify that:
Run npm install to install dependencies.