Introduction to S3 Sync Client
The s3-sync-client library is designed to help developers seamlessly manage file synchronization between local directories and Amazon S3 buckets. Whether you are dealing with large datasets or simple file structures, this client makes the synchronization process efficient and straightforward.
Useful API Examples
1. Installation
npm install s3-sync-client
2. Basic Usage
Initialize the client with your AWS credentials and start syncing files.
const { S3SyncClient, TransferProgress } = require('s3-sync-client');
const client = new S3SyncClient({
region: 'us-east-1',
credentials: {
accessKeyId: '',
secretAccessKey: ''
}
});
client.sync('./local-dir', 's3://my-bucket').then(() => {
console.log('Sync complete');
});
3. Sync with Exclusions
Exclude specific files or directories from the sync process using glob patterns.
client.sync('./local-dir', 's3://my-bucket', {
exclude: ['*.tmp', 'backup/']
}).then(() => {
console.log('Sync with exclusions complete');
});
4. Bi-Directional Sync
Sync files from the S3 bucket back to your local directory.
client.sync('s3://my-bucket', './local-dir').then(() => {
console.log('Bi-directional sync complete');
});
5. Sync with Progress Indicator
Monitor the progress with each file transferred.
const progress = new TransferProgress();
progress.on('progress', (p) => {
console.log(`Transferred: ${p.percent}%`);
});
client.sync('./local-dir', 's3://my-bucket', { progress }).then(() => {
console.log('Sync with progress complete');
});
Application Example
Here’s an example of a simple Node.js application that uses various APIs from the s3-sync-client library:
const express = require('express');
const { S3SyncClient, TransferProgress } = require('s3-sync-client');
const app = express();
const port = 3000;
const client = new S3SyncClient({
region: 'us-east-1',
credentials: {
accessKeyId: '',
secretAccessKey: ''
}
});
app.get('/sync', (req, res) => {
const progress = new TransferProgress();
progress.on('progress', (p) => {
console.log(`Transferred: ${p.percent}%`);
});
client.sync('./local-dir', 's3://my-bucket', { progress }).then(() => {
res.send('Sync complete');
}).catch((error) => {
res.status(500).send(`Failed to sync: ${error.message}`);
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
This complete example demonstrates initializing the sync client, setting up routes, and handling sync progress within an Express server.
Hash: f0a356adb984351efc51a7394750f1e138bf9df64a9ba870d5d566fbf5cc58ee