Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

# delete blobs + samples polishing

  • Loading branch information...
commit 3e688eb3e9964c0f5bc7d8956cb5e6e8c3d3b461 1 parent 10d6104
Juan Pablo Garcia Dalolla authored
View
6 History.md
@@ -1,3 +1,9 @@
+0.1.1 / TDB
+==================
+* Adding remaining Blob functionality:
+ - Retrieve and update blob metadata + properties
+ - Delete blobs
+
0.1.0 / 2011-07-31
==================
* Initial Release: Basic support for Blobs
View
29 Readme.md
@@ -21,11 +21,11 @@ To install via npm
## Blobs
// Creating a new container
- waz.blobs.container.create('myContainer', function(err, result){
+ waz.blobs.container.create('myContainer', function(err, container){
});
// Listing existing containers
- waz.blobs.container.list(function(err, result){
+ waz.blobs.container.list(function(err, containers){
});
// Finding a container
@@ -36,49 +36,54 @@ To install via npm
});
// Getting container's properties
- container.properties(function(err, metadata){
+ container.properties(function(err, properties){
});
// Adding metadata to a container
- container.putMetadata({'x-ms-custom' : 'MyValue'}, function(err, result){
+ container.putMetadata({'x-ms-custom' : 'MyValue'}, function(err){
});
// Getting container's ACL
- container.getAcl(function(err, result){
+ container.getAcl(function(err, acl){
});
// Setting container's ACL (null, 'blob', 'container')
- container.setAcl('container', function(err, result){
+ container.setAcl('container', function(err, container){
});
// Listing blobs in a container
- container.blobs(function(err, result){
+ container.blobs(function(err, blobs){
});
// Getting blob's information
result.getBlob('myfolder/my file.txt', function(err, blob){
// Adding metadata to a blob
- blob.putMetadata({'x-ms-custom' : 'MyValue'}, function(err, result){
+ blob.putMetadata({'x-ms-custom' : 'MyValue'}, function(err){
});
// Getting blob's metadata
blob.metadata(function(err, metadata){
- console.log(metadata);
+ });
+
+ // Adding properties to a blob
+ blob.putProperties({'x-ms-blob-property1': 'MyValue'}, function(err, properties){
});
// Getting blob's properties
blob.properties(function(err, properties){
- console.log(properties);
});
// Getting blob's contents
blob.getContents(function(err, data){
- console.log(data);
+
+ // Removing a blob
+ blob.destroy(function(err){
+ });
});
});
- // Uploading a new Blob
+ // Storing a new Blob
result.store('folder/my file.xml', '<xml>content</xml>', 'text/xml', {'x-ms-MyProperty': 'value'}, function(err, result){
});
}
View
79 examples/blobs.js
@@ -2,68 +2,89 @@ var waz = require('waz-storage-js');
waz.establishConnection( { accountName: 'your_account_name', accountKey: 'your_account_key', useSsl: false } );
-waz.blobs.container.create('test1', function(err, result){
+waz.blobs.container.create('container1', function(err, container1){
console.log('\n_________| creating a container |_________\n');
- console.log(err || result);
+ console.log(err || container1);
- waz.blobs.container.create('test2', function(err, result){
+ waz.blobs.container.create('container2', function(err, container2){
console.log('\n_________| creating a container |_________\n');
- console.log(err || result);
+ console.log(err || container2);
waz.blobs.container.list(function(err, containers){
console.log('\n_________| listing existing containers |_________\n');
console.log(containers || err.message);
- containers[0].putMetadata({'x-ms-meta-Custom' : 'MyValue'}, function(err, result){
- console.log(err || result);
+ containers[0].putMetadata({'x-ms-meta-Custom' : 'MyValue'}, function(err){
+ console.log(err || '`x-ms-meta-Custom` metadata added!');
- containers[0].metadata(function(err, result){
+ containers[0].metadata(function(err, metadata){
console.log('\n_________| showing container metadata |_________\n');
- console.log(err || result);
+ console.log(err || metadata);
});
});
- waz.blobs.container.find('test1', function(err, container){
+ waz.blobs.container.find('container1', function(err, container1){
console.log('\n_________| finding a container |_________\n');
- console.log(err || container);
-
- container.blobs(function(err, blobs){
+ console.log(err || container1);
+
+ container1.blobs(function(err, blobs){
+ console.log('\n_________| listing container blobs |_________\n');
console.log(err || blobs)
});
- container.setAcl('container', function(err, result){
- result.getAcl(function(err, result){
- console.log(err || result);
+ container1.setAcl('container', function(err){
+ console.log('\n_________| setting container acl to `container` |_________\n');
+ console.log(err || 'container acl set to `container`');
+
+ container1.getAcl(function(err, acl){
+ console.log('\n_________| displaying container acl |_________\n');
+ console.log(err || acl);
});
});
- container.store('Folder/hello world.xml', '<xml/>', 'text/xml', {'x-ms-test': 'myvalue'}, function(err, helloBlob){
- console.log(err || helloBlob);
+ container1.store('Folder/hello world.xml', '<xml/>', 'text/xml', {'x-ms-test': 'myvalue'}, function(err, helloWorldBlob){
+ console.log('\n_________| storing a new blob |_________\n');
+ console.log(err || helloWorldBlob);
- container.getBlob('Folder/hello world.xml', function(err, blob){
- console.log(err || result);
+ container1.getBlob('Folder/hello world.xml', function(err, blob){
+ console.log('\n_________| getting a blob |_________\n');
+ console.log(err || blob);
blob.getContents(function(err,data){
+ console.log('\n_________| displaying blob contents |_________\n');
console.log(err || data);
});
- blob.putMetadata({'x-ms-meta-custom': 'value'}, function(err, result) {
+ blob.putMetadata({'x-ms-meta-custom': 'value'}, function(err) {
+ console.log('\n_________| adding metadata to the blob |_________\n');
+ console.log(err || '`x-ms-meta-custom` metadata added');
+
blob.metadata(function(err, metadata){
+ console.log('\n_________| displaying blob metadata |_________\n');
console.log(err || metadata);
- blob.putProperties({'x-ms-blob-custom': 'value'}, function(err, result) {
+ blob.putProperties({'x-ms-blob-custom': 'value'}, function(err) {
+ console.log('\n_________| adding a property to the blob |_________\n');
+ console.log(err || '`x-ms-blob-custom` property added!');
+
blob.properties(function(err, properties){
+ console.log('\n_________| displaying blob properties |_________\n');
console.log(err || properties);
- waz.blobs.container.delete('test1', function(err){
- console.log('\n_________| removing a container |_________\n');
- console.log(err || 'test1 container removed!');
- });
+ blob.destroy(function(err){
+ console.log('\n_________| destroying a blob |_________\n');
+ console.log(err || 'blob ' + blob.path + ' removed!');
+
+ waz.blobs.container.delete('container1', function(err){
+ console.log('\n_________| removing a container |_________\n');
+ console.log(err || 'container1 container removed!');
+ });
- waz.blobs.container.delete('test2', function(err){
- console.log('\n_________| removing a container |_________\n');
- console.log(err || 'test2 container removed!');
- });
+ waz.blobs.container.delete('container2', function(err){
+ console.log('\n_________| removing a container |_________\n');
+ console.log(err || 'container2 container removed!');
+ });
+ });
});
});
});
View
16 lib/waz-blobs/blob.js
@@ -24,14 +24,14 @@ Blob.prototype.metadata = function(callback) {
};
Blob.prototype.putProperties = function(properties, callback) {
- serviceInstance.setBlobProperties(this.path, properties, function(err, data) {
- callback(err, data);
+ serviceInstance.setBlobProperties(this.path, properties, function(err) {
+ callback(err);
});
};
Blob.prototype.putMetadata = function(metadata, callback) {
- serviceInstance.setBlobMetadata(this.path, metadata, function(err, data) {
- callback(err, data);
+ serviceInstance.setBlobMetadata(this.path, metadata, function(err) {
+ callback(err);
});
};
@@ -39,4 +39,10 @@ Blob.prototype.getContents = function(callback){
serviceInstance.getBlob(this.path, function(err, data) {
callback(err, data);
});
-}
+};
+
+Blob.prototype.destroy = function(callback){
+ serviceInstance.deleteBlob(this.path, function(err) {
+ callback(err);
+ });
+};
View
6 lib/waz-blobs/container.js
@@ -18,8 +18,8 @@ Container.prototype.metadata = function(callback) {
};
Container.prototype.putMetadata = function(metadata, callback) {
- serviceInstance.setContainerMetadata(this.name, metadata, function(err, data) {
- callback(err, data);
+ serviceInstance.setContainerMetadata(this.name, metadata, function(err) {
+ callback(err);
});
};
@@ -32,7 +32,7 @@ Container.prototype.getAcl = function(callback) {
Container.prototype.setAcl = function(level, callback) {
var container = this;
- serviceInstance.setContainerAcl(this.name, level, function(err, data) {
+ serviceInstance.setContainerAcl(this.name, level, function(err) {
callback(err, container);
});
};
View
43 lib/waz-blobs/service.js
@@ -53,7 +53,7 @@ Service.prototype.listBlobs = function(containerName, callback){
Service.prototype.createContainer = function(name, callback){
this.coreService.execute('put', name, {restype: 'container'}, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var error = null, data = null
+ var error = null, data = null;
if (err != null && err.statusCode == 409)
error = { message: 'container `' + name + '` already exists' };
@@ -66,12 +66,12 @@ Service.prototype.createContainer = function(name, callback){
Service.prototype.deleteContainer = function(name, callback){
this.coreService.execute('delete', name, {restype: 'container'}, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var error = null, data = null
+ var error = null;
if (err != null && err.statusCode == 404)
error = { message: 'container `' + name + '` not found' };
- callback(error, data);
+ callback(error);
});
};
@@ -90,18 +90,18 @@ Service.prototype.getContainerProperties = function(name, callback){
Service.prototype.setContainerMetadata = function(name, metadata, callback){
this.coreService.execute('put', name, {restype: 'container', comp: 'metadata'}, {'x-ms-version': '2009-09-19'}.merge(metadata || {}), null, function(err, response) {
- var error = null, data = null
+ var error = null;
if (err != null && err.statusCode == 400)
error = { message: 'container `' + name + '` not found' };
- callback(error, data);
+ callback(error);
});
};
Service.prototype.getContainerAcl = function(name, callback){
this.coreService.execute('get', name, {restype: 'container', comp: 'acl'}, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var error = null, data = null
+ var error = null, data = null;
if (err != null)
error = { message: err.statusCode };
@@ -119,18 +119,18 @@ Service.prototype.setContainerAcl = function(name, level, callback){
var payload = '<?xml version="1.0" encoding="utf-8"?><SignedIdentifiers />'
this.coreService.execute('put', name, {restype: 'container', comp: 'acl'}, headers, payload, function(err, response) {
- var error = null, data = null
+ var error = null;
if (err != null)
error = { message: err.statusCode };
- callback(error, data);
+ callback(error);
});
};
Service.prototype.getBlobProperties = function(path, callback){
this.coreService.execute('head', path, null, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var error = null, data = null
+ var error = null, data = null;
if (err != null && err.statusCode == 400)
error = { message: 'blob `' + path + '` not found' };
@@ -143,7 +143,7 @@ Service.prototype.getBlobProperties = function(path, callback){
Service.prototype.getBlobMetadata = function(name, callback){
this.coreService.execute('head', name, {comp: 'metadata'}, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var error = null, data = null
+ var error = null, data = null;
if (err != null && err.statusCode == 400)
error = { message: 'blob `' + name + '` not found' };
@@ -156,23 +156,23 @@ Service.prototype.getBlobMetadata = function(name, callback){
Service.prototype.setBlobProperties = function(path, properties, callback){
this.coreService.execute('put', path, {comp: 'properties'}, {'x-ms-version': '2009-09-19'}.merge(properties || {}), null, function(err, response) {
- var error = null, data = null
+ var error = null;
if (err != null && err.statusCode == 400)
error = { message: 'blob `' + path + '` not found' };
- callback(error, data);
+ callback(error);
});
};
Service.prototype.setBlobMetadata = function(path, properties, callback){
this.coreService.execute('put', path, {comp: 'metadata'}, {'x-ms-version': '2009-09-19'}.merge(properties || {}), null, function(err, response) {
- var error = null, data = null
+ var error = null;
if (err != null && err.statusCode == 400)
error = { message: 'blob `' + path + '` not found' };
- callback(error, data);
+ callback(error);
});
};
@@ -190,7 +190,7 @@ Service.prototype.putBlob = function(path, payload, contentType, metadata, callb
callback(error, data);
});
-}
+};
Service.prototype.getBlob = function(path, callback){
this.coreService.execute('get', path, null, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
@@ -203,8 +203,19 @@ Service.prototype.getBlob = function(path, callback){
callback(error, data);
});
+};
+
+Service.prototype.deleteBlob = function(path, callback){
+ this.coreService.execute('delete', path, null, {'x-ms-version': '2009-09-19'}, null, function(err) {
+ var error = null;
+
+ if (err != null && err.statusCode == 404)
+ error = { message: 'blob `' + path + '` not found' };
+
+ callback(error);
+ });
}
Service.prototype.generateRequestUri = function(path, options){
return this.coreService.generateRequestUri(path, options);
-}
+};
View
18 test/waz-blobs/blob.test.js
@@ -40,6 +40,24 @@ module.exports = {
mock.verify();
},
+ 'should delete a blob': function(){
+ waz.establishConnection({ accountName : 'name', accountKey : 'key' });
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService);
+
+ var blob = new Blob({name: 'blobName', url: 'http://localhost/containerName/my%20Blob', contentType: 'text/xml', serviceInstance: blobService});
+
+ mock.expects("deleteBlob").withArgs("containerName/my%20Blob")
+ .yields(null)
+ .once();
+
+ blob.destroy(function(err){
+ assert.isNull(err);
+ });
+
+ mock.verify();
+ },
+
'should be able to return properties from a given blob': function(){
waz.establishConnection({ accountName : 'name', accountKey : 'key' });
var blobService = new Service({});
View
9 test/waz-blobs/container.test.js
@@ -87,11 +87,10 @@ module.exports = {
mock.expects("getContainerProperties").withArgs("containerName").yields(null, mockData).once();
var metadata = { 'x-meta-CustomProperty' : "customPropertyValue" };
- mock.expects("setContainerMetadata").withArgs("containerName", metadata).yields(null, null).once();
+ mock.expects("setContainerMetadata").withArgs("containerName", metadata).yields(null).once();
waz.blobs.container.find('containerName', function (err, container) {
- container.putMetadata(metadata, function(err, metadata) {
- assert.isNull(metadata);
+ container.putMetadata(metadata, function(err) {
assert.isNull(err);
});
});
@@ -162,10 +161,10 @@ module.exports = {
var mockData = { 'x-meta-Name' : "containerName" };
mock.expects("getContainerProperties").withArgs("containerName").yields(null, mockData).once();
- mock.expects("setContainerAcl").withArgs("containerName", "blob").yields(null, null).once();
+ mock.expects("setContainerAcl").withArgs("containerName", "blob").yields(null).once();
waz.blobs.container.find('containerName', function (err, container) {
- container.setAcl('blob', function(err, data){
+ container.setAcl('blob', function(err, data){
assert.equal(data, container);
assert.isNull(err);
});
View
60 test/waz-blobs/service.test.js
@@ -31,9 +31,8 @@ module.exports = {
.yields(null, mockData)
.once();
- blobService.deleteContainer('existing', function(err, data){
- assert.isNull(err);
- assert.isNull(data);
+ blobService.deleteContainer('existing', function(err){
+ assert.isNull(err);
});
mock.verify();
@@ -47,9 +46,8 @@ module.exports = {
.yields({ statusCode: 404 }, null)
.once();
- blobService.deleteContainer('unexisting', function(err, data){
+ blobService.deleteContainer('unexisting', function(err){
assert.equal(err.message, 'container `unexisting` not found');
- assert.isNull(data);
});
mock.verify();
@@ -129,10 +127,11 @@ module.exports = {
.yields(null, mockData)
.once();
- var properties = blobService.setBlobProperties('blobPath', {'x-ms-CustomProp': 'value'}, function(err, properties){
- mock.verify();
+ var properties = blobService.setBlobProperties('blobPath', {'x-ms-CustomProp': 'value'}, function(err){
assert.isNull(err);
});
+
+ mock.verify();
},
'should set blob metadata': function(){
@@ -144,10 +143,11 @@ module.exports = {
.yields(null, mockData)
.once();
- var properties = blobService.setBlobMetadata('blobPath', {'x-ms-meta-CustomProp': 'value'}, function(err, properties){
- mock.verify();
+ var properties = blobService.setBlobMetadata('blobPath', {'x-ms-meta-CustomProp': 'value'}, function(err){
assert.isNull(err);
});
+
+ mock.verify();
},
'should set container properties': function(){
@@ -159,10 +159,11 @@ module.exports = {
.yields(null, mockData)
.once();
- var properties = blobService.setContainerMetadata('mock-container', {'x-ms-CustomProp': 'value'}, function(err, properties){
- mock.verify();
+ var properties = blobService.setContainerMetadata('mock-container', {'x-ms-CustomProp': 'value'}, function(err){
assert.isNull(err);
});
+
+ mock.verify();
},
'should get null when container ACL is not set': function(){
@@ -225,7 +226,7 @@ module.exports = {
.yields(null, mockData)
.once();
- var properties = blobService.setContainerAcl('mock-container', 'blob', function(err, properties){
+ var properties = blobService.setContainerAcl('mock-container', 'blob', function(err){
assert.isNull(err);
});
@@ -242,7 +243,7 @@ module.exports = {
.yields(null, mockData)
.once();
- var properties = blobService.setContainerAcl('mock-container', null, function(err, properties){
+ var properties = blobService.setContainerAcl('mock-container', null, function(err){
assert.isNull(err);
});
@@ -464,4 +465,37 @@ module.exports = {
mock.verify();
},
+
+ 'should delete a blob': function(){
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService.coreService);
+ var mockData = {body: '', headers: null};
+
+ mock.expects("execute").withArgs('delete', 'mock-container/blob', null, {'x-ms-version': '2009-09-19'}, null)
+ .yields(null)
+ .once();
+
+ var properties = blobService.deleteBlob('mock-container/blob', function(err){
+ assert.isNull(err);
+ });
+
+ mock.verify();
+ },
+
+ 'should throw when trying to delete an unexisting blob': function(){
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService.coreService);
+ var mockData = {body: '', headers: null};
+
+ mock.expects("execute").withArgs('delete', 'mock-container/unexisting', null, {'x-ms-version': '2009-09-19'}, null)
+ .yields({statusCode:404})
+ .once();
+
+ var properties = blobService.deleteBlob('mock-container/unexisting', function(err){
+ assert.isNotNull(err)
+ assert.equal(err.message, 'blob `mock-container/unexisting` not found');
+ });
+
+ mock.verify();
+ },
}
Please sign in to comment.
Something went wrong with that request. Please try again.