Skip to content

Instantly share code, notes, and snippets.

@adrai
Last active August 29, 2015 14:06
Show Gist options
  • Save adrai/d3ed2146d4817c79c7db to your computer and use it in GitHub Desktop.
Save adrai/d3ed2146d4817c79c7db to your computer and use it in GitHub Desktop.
atmos (s3) + local (fs)
'use strict';
var aws = require('aws-sdk'),
s3blobs = require('s3-blob-store'),
fsblobs = require('fs-blob-store'),
fs = require('fs'),
path = require('path');
var workLocally = false;
var directory = 'blobs/images';
var store;
if (workLocally) {
store = fsblobs(path.join(__dirname, directory));
} else {
var client = new aws.S3({
endpoint: 'url',
accessKeyId: 'id',
secretAccessKey: 'secret'
});
store = s3blobs({
client: client,
bucket: directory
});
}
store.exists({ key: 'file1.txt' }, function (err, exists) {
if (err) {
return console.log(err);
}
console.log('exists: ' + exists);
if (!exists) {
var writeStream = store.createWriteStream({ key: 'file1.txt' }, function (err) {
if (err) {
return console.log(err);
}
store.exists({ key: 'file1.txt' }, function (err, exists) {
if (err) {
return console.log(err);
}
console.log('exists: ' + exists);
});
});
fs.createReadStream(__filename)
.pipe(writeStream);
} else {
var readStream = store.createReadStream({ key: 'file1.txt' });
readStream.pipe(fs.createWriteStream('downloaded.txt'));
readStream.on('end', function() {
store.remove({ key: 'file1.txt' }, function (err) {
if (err) {
return console.log(err);
}
console.log('removed!');
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment