Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed HelloPage.dat
Binary file not shown.
Binary file removed HelloWorld.png
Binary file not shown.
152 changes: 133 additions & 19 deletions blobSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ var guid = require('node-uuid');
var crypto = require('crypto');
var storage = require('azure-storage');

runBlobSamples();

function runBlobSamples() {
var config = readConfig();
runBlobSamples(config);

function runBlobSamples(config) {
/**
* Instructions: This sample can be run using either the Azure Storage Emulator that installs as part of this SDK - or by
* updating the app.config file with your connection string.
Expand All @@ -61,6 +63,10 @@ function runBlobSamples() {
{
scenario: basicStoragePageBlobOperations,
message: 'Page Blob Sample Completed\n'
},
{
scenario: basicStorageAppendBlobOperations,
message: 'Append Blob Sample Completed\n'
}];

var callback = function (error) {
Expand All @@ -71,28 +77,30 @@ function runBlobSamples() {

current++;
if (current < scenarios.length) {
scenarios[current].scenario(callback);
scenarios[current].scenario(config, callback);
}
}
};

scenarios[current].scenario(callback);
scenarios[current].scenario(config, callback);
}

/**
* Page blob basics.
* @ignore
*
* @param {config} config The configuration which contains the connectionString.
* @param {errorOrResult} callback The callback function.
*/
function basicStorageBlockBlobOperations(callback) {
function basicStorageBlockBlobOperations(config, callback) {
// Create a blob client for interacting with the blob service from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
var blobService = storage.createBlobService(readConfig().connectionString);
var blobService = storage.createBlobService(config.connectionString);

var imageToUpload = "HelloWorld.png";
var fileToUpload = "HelloWorld.dat";
writeRandomFile(fileToUpload, 1024);
var blockBlobContainerName = "demoblockblobcontainer-" + guid.v1();
var blockBlobName = "demoblockblob-" + imageToUpload;
var blockBlobName = "demoblockblob-" + fileToUpload;

console.log('Block Blob Sample');

Expand All @@ -109,7 +117,7 @@ function basicStorageBlockBlobOperations(callback) {

// Upload a BlockBlob to the newly created container
console.log('2. Uploading BlockBlob');
blobService.createBlockBlobFromLocalFile(blockBlobContainerName, blockBlobName, imageToUpload, function (error) {
blobService.createBlockBlobFromLocalFile(blockBlobContainerName, blockBlobName, fileToUpload, function (error) {
if (error) {
callback(error);
} else {
Expand All @@ -120,13 +128,13 @@ function basicStorageBlockBlobOperations(callback) {
callback(error);
} else {
for (var i = 0; i < results.length; i++) {
console.log(util.format(' - %s (type: %s)'), results[i].name, results[i].properties.blobtype);
console.log(util.format(' - %s (type: %s)'), results[i].name, results[i].blobType);
}

// Download a blob to your file system
console.log('4. Download Blob');
var downloadedImageName = util.format('CopyOf%s', imageToUpload);
blobService.getBlobToLocalFile(blockBlobContainerName, blockBlobName, downloadedImageName, function (error) {
var downloadedFileName = util.format('CopyOf%s', fileToUpload);
blobService.getBlobToLocalFile(blockBlobContainerName, blockBlobName, downloadedFileName, function (error) {
if (error) {
callback(error);
} else {
Expand Down Expand Up @@ -160,13 +168,14 @@ function basicStorageBlockBlobOperations(callback) {
console.log('7. Delete block Blob and all of its snapshots');
var deleteOption = { deleteSnapshots: storage.BlobUtilities.SnapshotDeleteOptions.BLOB_AND_SNAPSHOTS };
blobService.deleteBlob(blockBlobContainerName, blockBlobName, deleteOption, function (error) {
try { fs.unlinkSync(downloadedImageName); } catch (e) { }
try { fs.unlinkSync(downloadedFileName); } catch (e) { }
if (error) {
callback(error);
} else {
// Delete the container
console.log('8. Delete Container');
blobService.deleteContainerIfExists(blockBlobContainerName, function (error) {
try { fs.unlinkSync(fileToUpload); } catch (e) { }
callback(error);
});
}
Expand Down Expand Up @@ -194,14 +203,17 @@ function basicStorageBlockBlobOperations(callback) {
* Page blob basics.
* @ignore
*
* @param {config} config The configuration which contains the connectionString.
* @param {errorOrResult} callback The callback function.
*/
function basicStoragePageBlobOperations(callback) {
function basicStoragePageBlobOperations(config, callback) {
// Create a blob client for interacting with the blob service from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
var blobService = storage.createBlobService(readConfig().connectionString);
var blobService = storage.createBlobService(config.connectionString);

var fileToUpload = "HelloPage.dat";
writeRandomFile(fileToUpload, 1024);

var pageBlobContainerName = "demopageblobcontainer-" + guid.v1();
var pageBlobName = "demopageblob-" + fileToUpload;

Expand All @@ -215,7 +227,7 @@ function basicStoragePageBlobOperations(callback) {
} else {
// To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
// to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
// to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the file
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/demopageblobcontainer-[guid]/demopageblob-HelloPage.dat

// Upload a PageBlob to the newly created container
Expand All @@ -231,7 +243,7 @@ function basicStoragePageBlobOperations(callback) {
callback(error);
} else {
for (var i = 0; i < results.length; i++) {
console.log(util.format(' - %s (type: %s)'), results[i].name, results[i].properties.blobtype);
console.log(util.format(' - %s (type: %s)'), results[i].name, results[i].blobType);
}

// Read a range from a page blob
Expand All @@ -246,14 +258,15 @@ function basicStoragePageBlobOperations(callback) {
console.log(' Downloaded File Size: %s', stats.size);
try { fs.unlinkSync(downloadedFileName); } catch (e) { }
// Clean up after the demo
console.log('7. Delete Page Blob');
console.log('5. Delete Page Blob');
blobService.deleteBlob(pageBlobContainerName, pageBlobName, function (error) {
if (error) {
callback(error);
} else {
// Delete the container
console.log('8. Delete Container');
console.log('6. Delete Container');
blobService.deleteContainerIfExists(pageBlobContainerName, function (error) {
try { fs.unlinkSync(fileToUpload); } catch (e) { }
callback(error);
});
}
Expand All @@ -269,6 +282,95 @@ function basicStoragePageBlobOperations(callback) {
});
}


/**
* Append blob basics.
* @ignore
*
* @param {config} config The configuration which contains the connectionString.
* @param {errorOrResult} callback The callback function.
*/
function basicStorageAppendBlobOperations(config, callback) {
// Create a blob client for interacting with the blob service from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
var blobService = storage.createBlobService(config.connectionString);

var fileToUpload = "HelloAppend.dat";
writeRandomFile(fileToUpload, 1024);
var appendBlobContainerName = "demoappendblobcontainer-" + guid.v1();
var appendBlobName = "demoappendblob-" + fileToUpload;

console.log('Append Blob Sample');

// Create a container for organizing blobs within the storage account.
console.log('1. Creating Container');
blobService.createContainerIfNotExists(appendBlobContainerName, function (error) {
if (error) {
callback(error);
} else {
// To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
// to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the file
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/demoappendblobcontainer-[guid]/demopageblob-HelloAppend.dat

// Upload a PageBlob to the newly created container
console.log('2. Uploading AppendBlob');
blobService.createAppendBlobFromLocalFile(appendBlobContainerName, appendBlobName, fileToUpload, function (error) {
if (error) {
callback(error);
} else {
// List all the blobs in the container
console.log('3. List Blobs in Container');
listBlobs(blobService, appendBlobContainerName, null, null, function (error, results) {
if (error) {
callback(error);
} else {
for (var i = 0; i < results.length; i++) {
console.log(util.format(' - %s (type: %s)'), results[i].name, results[i].blobType);
}

// Download a blob to your file system
console.log('4. Download Blob');
var downloadedFileName = util.format('CopyOf%s', fileToUpload);
blobService.getBlobToLocalFile(appendBlobContainerName, appendBlobName, downloadedFileName, function (error) {
if (error) {
callback(error);
} else {
fs.stat(downloadedFileName, function(error, stats) {
console.log('5. Append block to append blob');
blobService.appendBlockFromText(appendBlobContainerName, appendBlobName, 'text to be appended', { appendPosition: stats.size } /** Verify that the blob has NOT been written by another process */, function(error){
if (error) {
callback(error);
} else {
console.log(' Downloaded File Size: %s', stats.size);
try { fs.unlinkSync(downloadedFileName); } catch (e) { }
// Clean up after the demo
console.log('6. Delete Append Blob');
blobService.deleteBlob(appendBlobContainerName, appendBlobName, function (error) {
if (error) {
callback(error);
} else {
// Delete the container
console.log('7. Delete Container');
blobService.deleteContainerIfExists(appendBlobContainerName, function (error) {
try { fs.unlinkSync(fileToUpload); } catch (e) { }
callback(error);
});
}
});
}
});
});
}
});
}
});
}
});
}
});
}

/**
* Lists blobs in the container.
* @ignore
Expand Down Expand Up @@ -321,6 +423,18 @@ function getRandomBuffer(size) {
return crypto.randomBytes(size);
}

/**
* Write some random bytes to the specified file.
* @ignore
*
* @param {string} file The file name.
* @param {int} size The size of the file in bytes.
*/
function writeRandomFile(file, size) {
var buffer = getRandomBuffer(size);
fs.writeFileSync(file, buffer);
}

/**
* Generates a random ID for the blob block.
* @ignore
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"node-uuid": "~1.4.0",
"request": "~2.56.0",
"azure-storage": "0.6.0"
"azure-storage": "0.10.0"
},
"devDependencies": {
"jshint": ">= 2.1.4"
Expand Down