Skip to content

Instantly share code, notes, and snippets.

@minwe
Forked from nuxlli/azure_upload_async.js
Created August 16, 2014 11:50
Show Gist options
  • Save minwe/7d3bb296dc3e117a8967 to your computer and use it in GitHub Desktop.
Save minwe/7d3bb296dc3e117a8967 to your computer and use it in GitHub Desktop.
var blob = require('azure').createBlobService()
, fs = require('fs')
, Q = require('q')
var options = { publicAccessLevel: 'blob' }
// With Q
var fsLstat = Q.nbind(fs.lstat, fs);
var getContainer = Q.nbind(blob.createContainerIfNotExists, blob);
var streamFile = Q.nbind(blob.createBlockBlobFromStream, blob);
function upload(container, name, path, callback) {
var promise = getContainer(container, options)
.then(function() {
return fsLstat(path);
})
.then(function(stats) {
return streamFile(
scontainer, name, fs.createReadStream(path), stats.size
);
})
.then(function() {
return blob.getBlobUrl(container, name).url();
});
if (typeof(callback) != "undefined") {
promise.then(callback)
} else {
return promise;
}
}
var blob = require('azure').createBlobService()
, fs = require('fs')
var options = { publicAccessLevel: 'blob' }
function upload(container, name, path, callback) {
blob.createContainerIfNotExists(container, options, function(err) {
if (!err) {
fs.lstat(path, function(stats) {
if (!err) {
var stream = fs.createReadStream(path);
blob.createBlockBlobFromStream(container, name, stream, stats.size, function(err) {
if (!err) {
var url = blob.getBlobUrl(container, name).url();
callback(null, error);
} else {
callback(err);
}
});
} else {
callback(err);
}
});
} else {
callback(err);
}
});
}
var blob = require('azure').createBlobService()
, fs = require('fs')
, Q = require('q')
var options = { publicAccessLevel: 'blob' }
// With Q
var fsLstat = Q.nbind(fs.lstat, fs);
var getContainer = Q.nbind(blob.createContainerIfNotExists, blob);
var streamFile = Q.nbind(blob.createBlockBlobFromStream, blob);
function upload(container, name, path) {
return getContainer(container, options)
.then(function() {
return fsLstat(path);
})
.then(function(stats) {
return streamFile(
scontainer, name, fs.createReadStream(path), stats.size
);
})
.then(function() {
return blob.getBlobUrl(container, name).url();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment