diff --git a/HelloPage.dat b/HelloPage.dat deleted file mode 100644 index 38ac68a..0000000 Binary files a/HelloPage.dat and /dev/null differ diff --git a/HelloWorld.png b/HelloWorld.png deleted file mode 100644 index e098a8a..0000000 Binary files a/HelloWorld.png and /dev/null differ diff --git a/blobSample.js b/blobSample.js index 4208c5f..64fedd4 100644 --- a/blobSample.js +++ b/blobSample.js @@ -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. @@ -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) { @@ -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'); @@ -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 { @@ -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 { @@ -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); }); } @@ -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; @@ -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 @@ -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 @@ -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); }); } @@ -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 @@ -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 diff --git a/package.json b/package.json index de51bf3..f4a8195 100644 --- a/package.json +++ b/package.json @@ -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"