Skip to content

Instantly share code, notes, and snippets.

@3B00D
Last active September 28, 2017 15:32
Show Gist options
  • Save 3B00D/d7a7dd6d0d73e0a30307ba06c653e72a to your computer and use it in GitHub Desktop.
Save 3B00D/d7a7dd6d0d73e0a30307ba06c653e72a to your computer and use it in GitHub Desktop.
Sync files to S3 automatically whenever they are modified/deleted.
var chokidar = require('chokidar')
var AWS = require('aws-sdk')
var fs = require('fs')
var s3 = new AWS.S3()
config = { destBucket: 'YOUR_BUCKET_NAME', directoryToWatch: 'path/to/your/directory/' }
function watchDirs(dirs) {
watcher = chokidar.watch(dirs, {
ignored: /[\/\\]\./,
persistent: true,
interval: 300
})
watcher
.on('add', function (path) {
syncFile(path, path, function (err, data) {
if (err) {
console.error('an error occured while uploading the file : ' + path, err)
} else {
console.log('the file "' + path + '" was uploaded successfuly!')
}
})
})
.on('change', function (path) {
syncFile(path, path, function (err, data) {
if (err) {
console.error('an error occured while uploading the file : ' + path, err)
} else {
console.log('the file "' + path + '" was uploaded successfuly!')
}
})
})
.on('unlink', function (path) {
unSyncFile(path, function (err, data) {
if (err) {
console.error('an error occured while deleting the file : ' + path, err)
} else {
console.log('the file "' + path + '" was deleted successfuly!')
}
})
})
.on('error', function (error) {
console.error(error)
})
.on('unlinkDir', path => console.log(`Directory ${path} has been removed`))
}
function syncFile(input, output, callback) {
try {
var params = { Bucket: config.destBucket, Key: output, Body: fs.createReadStream(input) };
var options = { partSize: 10 * 1024 * 1024, queueSize: 1 };
s3.upload(params, options, callback);
} catch (e) {
callback(e)
}
}
function unSyncFile(filepath, callback) {
var param = {
Bucket: config.destBucket,
Key: filepath
}
s3.deleteObject(param, callback)
}
watchDirs(config.directoryToWatch)
{
"name": "fileWatcherS3Sync",
"version": "1.0.0",
"description": "Sync files to S3 automatically whenever they are modified/deleted.",
"main": "fileWatcherS3Sync.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node fileWatcherS3Sync.js"
},
"author": "Abdulsalam Alshallah",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.124.0",
"chokidar": "^1.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment