Initial commit
This commit is contained in:
commit
ec49fedc45
1288 files changed
+226749
No files matched your search
+246
@@ -0,0 +1,246 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* 1. Demonstrates how to upload all files from a given directory in parallel
|
||||
*
|
||||
* 2. Demonstrates how to download all files from a given blob container to a given destination directory.
|
||||
*
|
||||
* 3. Demonstrate making requests using AccessConditions.
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var container = 'updownsample3';
|
||||
var blob = 'updownsample';
|
||||
var blobAccess = 'updownaccesssample';
|
||||
|
||||
var blobService = azure.createBlobService()
|
||||
.withFilter(new azure.ExponentialRetryPolicyFilter());
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);
|
||||
*/
|
||||
|
||||
function uploadSample() {
|
||||
var processArguments = process.argv;
|
||||
if (processArguments.length !== 4) {
|
||||
console.log('Incorrect number of arguments. Should be: srcPath destPath');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var srcPath = processArguments[2];
|
||||
var destPath = processArguments[3];
|
||||
|
||||
console.log('Starting blobuploaddownloadsample.');
|
||||
|
||||
// Create the container
|
||||
createContainer(container, function () {
|
||||
|
||||
// Demonstrates how to upload all files from a given directoy
|
||||
uploadBlobs(srcPath, container, function () {
|
||||
|
||||
// Demonstrates how to download all files from a given
|
||||
// blob container to a given destination directory.
|
||||
downloadBlobs(container, destPath, function () {
|
||||
|
||||
// Demonstrate making requests using AccessConditions.
|
||||
useAccessCondition(container, function () {
|
||||
|
||||
// Delete the container
|
||||
deleteContainer(container, function () {
|
||||
console.log('Ending blobuploaddownloadsample.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer (container, callback) {
|
||||
// Create the container.
|
||||
blobService.createContainerIfNotExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function uploadBlobs(sourceDirectoryPath, containerName, callback) {
|
||||
console.log('Entering uploadBlobs.');
|
||||
|
||||
// validate directory is valid.
|
||||
if (!fs.existsSync(sourceDirectoryPath)) {
|
||||
console.log(sourceDirectoryPath + ' is an invalid directory path.');
|
||||
} else {
|
||||
// Search the directory and generate a list of files to upload.
|
||||
walk(sourceDirectoryPath, function (error, files) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
var finished = 0;
|
||||
|
||||
// generate and schedule an upload for each file
|
||||
files.forEach(function (file) {
|
||||
var blobName = file.replace(/^.*[\\\/]/, '');
|
||||
|
||||
blobService.createBlockBlobFromLocalFile(containerName, blobName, file, function (error) {
|
||||
finished++;
|
||||
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log(' Blob ' + blobName + ' upload finished.');
|
||||
|
||||
if (finished === files.length) {
|
||||
// Wait until all workers complete and the blobs are uploaded to the server.
|
||||
console.log('All files uploaded');
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function downloadBlobs(containerName, destinationDirectoryPath, callback) {
|
||||
console.log('Entering downloadBlobs.');
|
||||
|
||||
// Validate directory
|
||||
if (!fs.existsSync(destinationDirectoryPath)) {
|
||||
console.log(destinationDirectoryPath + ' does not exist. Attempting to create this directory...');
|
||||
fs.mkdirSync(destinationDirectoryPath);
|
||||
console.log(destinationDirectoryPath + ' created.');
|
||||
}
|
||||
|
||||
// NOTE: does not handle pagination.
|
||||
blobService.listBlobsSegmented(containerName, null, function (error, result) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
var blobs = result.entries;
|
||||
var blobsDownloaded = 0;
|
||||
|
||||
blobs.forEach(function (blob) {
|
||||
blobService.getBlobToLocalFile(containerName, blob.name, destinationDirectoryPath + '/' + blob.name, function (error2) {
|
||||
blobsDownloaded++;
|
||||
|
||||
if (error2) {
|
||||
console.log(error2);
|
||||
} else {
|
||||
console.log(' Blob ' + blob.name + ' download finished.');
|
||||
|
||||
if (blobsDownloaded === blobs.length) {
|
||||
// Wait until all workers complete and the blobs are downloaded
|
||||
console.log('All files downloaded');
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function useAccessCondition(containerName, callback) {
|
||||
console.log('Entering useAccessCondition.');
|
||||
|
||||
// Create a blob.
|
||||
blobService.createBlockBlobFromText(containerName, blobAccess, 'hello', function (error, blobInformation) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log(' Created the blob ' + blobInformation.name);
|
||||
console.log(' Blob Etag is: ' + blobInformation.etag);
|
||||
|
||||
// Use the If-not-match ETag condition to access the blob. By
|
||||
// using the IfNoneMatch condition we are asserting that the blob needs
|
||||
// to have been modified in order to complete the request. In this
|
||||
// sample no other client is accessing the blob, so this will fail as
|
||||
// expected.
|
||||
var options = { accessConditions: { EtagNonMatch: blobInformation.etag} };
|
||||
blobService.createBlockBlobFromText(containerName, blobInformation.name, 'new hello', options, function (error2) {
|
||||
if (error2 && error2.statusCode === 412 && error2.code === 'ConditionNotMet') {
|
||||
console.log('Attempted to recreate the blob with the if-none-match access condition and got the expected exception.');
|
||||
callback();
|
||||
} else {
|
||||
console.log(' Blob was incorrectly updated');
|
||||
if (error2) {
|
||||
console.log(error2);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer (container, callback) {
|
||||
// Delete the container.
|
||||
blobService.deleteContainerIfExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Utility function
|
||||
|
||||
var walk = function (dir, done) {
|
||||
var results = [];
|
||||
fs.readdir(dir, function (err, list) {
|
||||
if (err) return done(err);
|
||||
var i = 0;
|
||||
(function next() {
|
||||
var file = list[i++];
|
||||
if (!file) return done(null, results);
|
||||
file = dir + '/' + file;
|
||||
fs.stat(file, function (err2, stat) {
|
||||
if (stat && stat.isDirectory()) {
|
||||
walk(file, function (err3, res) {
|
||||
results = results.concat(res);
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
results.push(file);
|
||||
next();
|
||||
}
|
||||
});
|
||||
})();
|
||||
});
|
||||
};
|
||||
|
||||
uploadSample();
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* This sample demonstrates how to handle continuation tokens and virtual "pages" of results when performing a listing
|
||||
* operation on the blob service.
|
||||
*
|
||||
* This sample peformsthe following steps:
|
||||
*
|
||||
* 0. Create container.
|
||||
*
|
||||
* 1. Create 50 blobs.
|
||||
*
|
||||
* 2. List the first 10(page size) blobs.
|
||||
*
|
||||
* 3. Check whether there are more results.
|
||||
*
|
||||
* 4. Repeat 2 and 3 until complete.
|
||||
*
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var container = 'paginationsample';
|
||||
var blob = 'contsample';
|
||||
var blobs = [];
|
||||
|
||||
var blobService = azure.createBlobService()
|
||||
.withFilter(new azure.ExponentialRetryPolicyFilter());
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);
|
||||
*/
|
||||
|
||||
var totalBlobsCount;
|
||||
var pageSize;
|
||||
|
||||
function continuationSample () {
|
||||
var processArguments = process.argv;
|
||||
if (processArguments.length !== 4) {
|
||||
console.log('Incorrect number of arguments. Should be: numBlobs pageSize [deleteContainer]\nTry: 51 10');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
totalBlobsCount = parseInt(processArguments[2], 10);
|
||||
pageSize = parseInt(processArguments[3], 10);
|
||||
|
||||
console.log('Starting continuationSample.');
|
||||
|
||||
// Create the container
|
||||
createContainer(container, function () {
|
||||
|
||||
console.log('Entering createBlobs.');
|
||||
|
||||
// Upload blobs from text.
|
||||
createBlobs(totalBlobsCount, function () {
|
||||
var options = {
|
||||
maxResults: pageSize,
|
||||
include: 'metadata',
|
||||
locationMode: azure.StorageUtilities.LocationMode.PRIMARY_THEN_SECONDARY
|
||||
};
|
||||
|
||||
console.log('Entering listBlobs.');
|
||||
|
||||
// List blobs using continuation tokens.
|
||||
listBlobs(options, null, function () {
|
||||
|
||||
// Delete the container
|
||||
deleteContainer(container, function () {
|
||||
console.log('Ending continuationSample.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer (container, callback) {
|
||||
// Create the container.
|
||||
blobService.createContainerIfNotExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createBlobs(currentBlobsCount, callback) {
|
||||
// Upload totalBlobsCount blobs to the container.
|
||||
var options = {};
|
||||
options.metadata = {'hello':'world'};
|
||||
|
||||
blobService.createBlockBlobFromText(container, blob + currentBlobsCount, 'blob' + currentBlobsCount, options, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else if (currentBlobsCount > 1) {
|
||||
createBlobs(--currentBlobsCount, callback);
|
||||
} else {
|
||||
console.log(' Created ' + totalBlobsCount + ' blobs.');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function listBlobs (options, token, callback) {
|
||||
blobService.listBlobsSegmented(container, token, options, function(error, result) {
|
||||
blobs.push.apply(blobs, result.entries);
|
||||
var token = result.continuationToken;
|
||||
if(token) {
|
||||
console.log(' Received a page of results. There are ' + result.entries.length + ' blobs on this page.');
|
||||
listBlobs(options, token, callback);
|
||||
}
|
||||
else {
|
||||
console.log(' Completed listing. There are ' + blobs.length + ' blobs');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer (container, callback) {
|
||||
// Delete the container.
|
||||
blobService.deleteContainerIfExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
continuationSample();
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* Demonstrates how to define sendingrequest and receivedresponse event handlers.
|
||||
*/
|
||||
var fs = require('fs');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var container = 'sendingrequestevent3';
|
||||
|
||||
// The service object which will define the event handlers
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);
|
||||
blobService2.setProxy(proxy);
|
||||
*/
|
||||
|
||||
// the sending request event handler
|
||||
var sendingRequestHandler = function (webresource) {
|
||||
webresource.withHeader('x-ms-custom-header', 'value');
|
||||
console.log(' sending request event handler called');
|
||||
};
|
||||
|
||||
// the response received event handler
|
||||
var responseReceivedHandler = function (response) {
|
||||
console.log(' received response event handler called');
|
||||
};
|
||||
|
||||
function eventHandlersSample () {
|
||||
console.log('Starting eventHandlersSample.');
|
||||
|
||||
// set the event handlers
|
||||
blobService.on('sendingRequestEvent', sendingRequestHandler);
|
||||
blobService.on('receivedResponseEvent', responseReceivedHandler);
|
||||
|
||||
// create and delete a container with these handlers
|
||||
createContainer(container, function () {
|
||||
// Delete the container
|
||||
deleteContainer(container, function () {
|
||||
console.log('Ending eventHandlersSample.');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer (container, callback) {
|
||||
// Create the container.
|
||||
blobService.createContainer(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer (container, callback) {
|
||||
// Delete the container.
|
||||
blobService.deleteContainer(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
eventHandlersSample();
|
||||
+230
@@ -0,0 +1,230 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* Demonstrates how to use pre-written retry policies and how to define a customized retry policy.
|
||||
*
|
||||
* In the sample for pre-written retry policies, we simply show how to use pre-written retry policies.
|
||||
*
|
||||
* In the sample for customized retry policy, we define a customized retry policy,
|
||||
* which retries on the "The specified container is being deleted" exception besides the server exceptions.
|
||||
*
|
||||
* Note that only in the cloud(not the storage emulator), "The specified container is being deleted" exceptions will be
|
||||
* sent if users immediately recreate a container after delete it.
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
if (!fs.existsSync) {
|
||||
fs.existsSync = require('path').existsSync;
|
||||
}
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var RetryPolicyFilter = azure.RetryPolicyFilter;
|
||||
var LocationMode = azure.StorageUtilities.LocationMode;
|
||||
|
||||
var container = 'customretrypolicysample';
|
||||
|
||||
var blobService;
|
||||
|
||||
/**
|
||||
* Demonstrate how to use pre-written retry policies.
|
||||
* By default, no retry will be performed with service instances newly created.
|
||||
* Several pre-written retry policies are available with modifiable settings,
|
||||
* and can be used through associating filter.
|
||||
*/
|
||||
function setRetries() {
|
||||
console.log('Starting Sample 1 - setRetries.');
|
||||
|
||||
// By default, no retry will be performed with all kinds of services created
|
||||
// by Azure storage client library for Node.js.
|
||||
var blobServiceWithoutRetry = azure.createBlobService();
|
||||
console.log('BlobService instance created, no retry will be performed by default.');
|
||||
|
||||
// There are two pre-written retry policies: ExponentialRetryPolicyFilter
|
||||
// and LinearRetryPolicyFilter can be used with modifiable settings.
|
||||
// Use an exponential retry with customized settings.
|
||||
var fileServiceWithExponentialRetry = azure.createFileService().withFilter(
|
||||
new azure.ExponentialRetryPolicyFilter(
|
||||
3, // retryCount is set to 3 times.
|
||||
4000, // retryInterval is set to 4 seconds.
|
||||
3000, // minRetryInterval is set to 3 seconds.
|
||||
120000 // maxRetryInterval is set to 120 seconds.
|
||||
));
|
||||
console.log('FileService instance created and associated with ExponentialRetryPolicyFilter.');
|
||||
console.log(' Retries will be performed with exponential back-off.');
|
||||
|
||||
// Use a default linear retry policy.
|
||||
var tableServiceWithLinearRetry = azure.createTableService().withFilter(
|
||||
new azure.LinearRetryPolicyFilter()); // By default, retryCount is set to 3 times and retryInterval is set to 30 seconds.
|
||||
console.log('TableService instance created and associated with LinearRetryPolicyFilter,');
|
||||
console.log(' Retries will be performed with linear back-off.');
|
||||
|
||||
console.log('Ending Sample 1 - setRetries.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrate how to use custom retry policy.
|
||||
* Any custom retry logic may be used by simply creating and setting RetryPolicyFilter instance.
|
||||
*/
|
||||
function setCustomRetryPolicy() {
|
||||
console.log('Starting Sample 2 - setCustomRetryPolicy.');
|
||||
|
||||
// Step 1 : Set the retry policy to customized retry policy which will
|
||||
// not retry on any failing status code other than the excepted one.
|
||||
var retryOnContainerBeingDeleted = new RetryPolicyFilter();
|
||||
retryOnContainerBeingDeleted.retryCount = 5;
|
||||
retryOnContainerBeingDeleted.retryInterval = 5000;
|
||||
|
||||
retryOnContainerBeingDeleted.shouldRetry = function (statusCode, retryData) {
|
||||
console.log(' Made the request at ' + new Date().toUTCString() + ', received StatusCode: ' + statusCode);
|
||||
|
||||
var retryInfo = {};
|
||||
|
||||
// retries on any bad status code other than 409
|
||||
if (statusCode >= 300 && statusCode !== 409 && statusCode !== 500) {
|
||||
retryInfo.retryable = false;
|
||||
} else {
|
||||
var currentCount = (retryData && retryData.retryCount) ? retryData.retryCount : 0;
|
||||
|
||||
retryInfo = {
|
||||
retryInterval: this.retryInterval + 2000 * currentCount,
|
||||
retryable: currentCount < this.retryCount
|
||||
};
|
||||
}
|
||||
|
||||
return retryInfo;
|
||||
};
|
||||
|
||||
blobService = azure.createBlobService().withFilter(retryOnContainerBeingDeleted);
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);*/
|
||||
|
||||
|
||||
// Step 2: Create the container
|
||||
createContainer(function () {
|
||||
|
||||
// Step 3: Fetch attributes from the container using LocationMode.SECONDARY_THEN_PRIMARY
|
||||
fetchAttributesContainer(function () {
|
||||
|
||||
// Step 4: Lease the container
|
||||
leaseContainer(function () {
|
||||
|
||||
// Step 5: Lease the container again, retrying until it succeeds
|
||||
leaseContainer(function () {
|
||||
|
||||
// Step 6: Delete the container
|
||||
deleteContainer(function () {
|
||||
console.log('Ending Sample 2 - setCustomRetryPolicy.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer(callback) {
|
||||
console.log('Entering createContainer.');
|
||||
|
||||
// Create the container.
|
||||
blobService.createContainerIfNotExists(container, function (error, containerResult) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log(' Container info ');
|
||||
console.log(containerResult);
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function fetchAttributesContainer(callback) {
|
||||
console.log('Entering fetchAttributesContainer.');
|
||||
|
||||
var options = {
|
||||
locationMode: LocationMode.SECONDARY_THEN_PRIMARY
|
||||
};
|
||||
|
||||
// Get the properties of the container.
|
||||
blobService.getContainerProperties(container, options, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Downloaded container properties from ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function leaseContainer(callback) {
|
||||
console.log('Entering leaseContainer.');
|
||||
|
||||
// Try to acquire the lease.
|
||||
blobService.acquireLease(container, null, {leaseDuration: 15}, function (error, lease) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
}
|
||||
else {
|
||||
console.log('Acquired lease from ' + container + ' with leaseid' + lease.id);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer(callback) {
|
||||
console.log('Entering deleteContainer.');
|
||||
|
||||
// Break the lease.
|
||||
blobService.breakLease(container, null, {leaseBreakPeriod: 0}, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log(' Broke the lease on the container ' + container);
|
||||
}
|
||||
|
||||
// Delete the container.
|
||||
blobService.deleteContainer(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function runAllSamples() {
|
||||
console.log("Starting retrypolicySample.");
|
||||
setRetries();
|
||||
setCustomRetryPolicy();
|
||||
console.log("Ending retrypolicySample.");
|
||||
}
|
||||
|
||||
runAllSamples();
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* In this sample, we demonstrate how to generate and use the blob level shared access signature and the container level
|
||||
* shared access signature.
|
||||
*
|
||||
* In the blob level shared access signature sample, there are the following steps:
|
||||
*
|
||||
* 1. Create a container and a blob.
|
||||
*
|
||||
* 2. Generate a shared access signature for the blob and download the blob using it.
|
||||
*
|
||||
* 3. Upload a "ReadWrite" policy and a "Read" permission to the container.
|
||||
*
|
||||
* 4. Generate a shared access signature for the blob using the policy id and download the blob using it.
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
var assert = require('assert');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('../../lib/azure-storage.js')) {
|
||||
azure = require('../../lib/azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
var BlobUtilities = azure.BlobUtilities;
|
||||
|
||||
var container = 'container-sassample';
|
||||
var blob = 'blob-sassample';
|
||||
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);
|
||||
blobService2.setProxy(proxy);
|
||||
*/
|
||||
|
||||
function sasSample () {
|
||||
console.log('Starting sasSample.');
|
||||
|
||||
// Create the container.
|
||||
createContainer(function () {
|
||||
|
||||
// Create a blob.
|
||||
createBlob( function () {
|
||||
|
||||
// Create a shared access signature and use it to download the blob just created.
|
||||
downloadBlobUsingSharedAccessSignature(function () {
|
||||
|
||||
// Add Shared Access policies to the container
|
||||
createPolicies(function () {
|
||||
|
||||
// Use the read policy just created
|
||||
usePermissions(function () {
|
||||
|
||||
// Delete the container
|
||||
deleteContainer(function () {
|
||||
console.log('Ending sasSample.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer (callback) {
|
||||
// Create the container.
|
||||
blobService.createContainerIfNotExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createBlob (callback) {
|
||||
// Create the blob
|
||||
blobService.createBlockBlobFromText(container, blob, 'test blob', function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
}
|
||||
else {
|
||||
console.log('Created the blob ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function downloadBlobUsingSharedAccessSignature (callback) {
|
||||
var startDate = new Date();
|
||||
var expiryDate = new Date(startDate);
|
||||
expiryDate.setMinutes(startDate.getMinutes() + 5);
|
||||
|
||||
var sharedAccessPolicy = {
|
||||
AccessPolicy: {
|
||||
Permissions: BlobUtilities.SharedAccessPermissions.READ,
|
||||
Start: startDate,
|
||||
Expiry: expiryDate
|
||||
}
|
||||
};
|
||||
|
||||
var sharedAccessSignatureToken = blobService.generateSharedAccessSignature(container, blob, sharedAccessPolicy);
|
||||
|
||||
var sharedBlobService = azure.createBlobServiceWithSas(blobService.host, sharedAccessSignatureToken);
|
||||
|
||||
// Download the blob by using the shared access signature URL.
|
||||
sharedBlobService.getBlobProperties(container, blob, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Downloaded the blob ' + blob + ' by using the shared access signature URL: \n ' + sharedBlobService.getUrl(container, blob, sharedAccessSignatureToken));
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function createPolicies (callback) {
|
||||
// Create a "ReadWrite" policy and a "Read" policy.
|
||||
var readWriteStartDate = new Date();
|
||||
var readWriteExpiryDate = new Date(readWriteStartDate);
|
||||
readWriteExpiryDate.setMinutes(readWriteStartDate.getMinutes() + 10);
|
||||
|
||||
var signedIdentifiers = {
|
||||
readwrite: {
|
||||
Start: readWriteStartDate,
|
||||
Expiry: readWriteExpiryDate,
|
||||
Permissions: 'rw'
|
||||
},
|
||||
read: {
|
||||
Expiry: readWriteStartDate,
|
||||
Permissions: 'r'
|
||||
}
|
||||
};
|
||||
|
||||
// Wait 30 seconds for the container acl to be processed
|
||||
var func = function () {
|
||||
var options = { publicAccessLevel: BlobUtilities.BlobContainerPublicAccessType.CONTAINER };
|
||||
blobService.setContainerAcl(container, signedIdentifiers, options, function(error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Uploaded the permissions for the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
setTimeout(func, 30000);
|
||||
}
|
||||
|
||||
function usePermissions (callback) {
|
||||
// Read, write the blob using the shared access signature from "ReadWrite" policy.
|
||||
var readWriteAccessPolicy = {
|
||||
Id: 'readwrite'
|
||||
};
|
||||
|
||||
var headers = {
|
||||
cacheControl: 'no-transform',
|
||||
contentDisposition: 'attachment',
|
||||
contentEncoding: 'gzip',
|
||||
contentLanguage: 'tr,en',
|
||||
contentType: 'text/html'
|
||||
};
|
||||
|
||||
var sharedAccessSignatureToken = blobService.generateSharedAccessSignature(container, null, {Id: 'readwrite'}, headers);
|
||||
|
||||
var sharedBlobService = azure.createBlobServiceWithSas(blobService.host, sharedAccessSignatureToken);
|
||||
|
||||
sharedBlobService.getBlobProperties(container, blob, function (error, result) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Downloaded the blob ' + blob + ' by using the shared access signature URL: \n ' + sharedBlobService.getUrl(container, blob, sharedAccessSignatureToken));
|
||||
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer (callback) {
|
||||
// Delete the container.
|
||||
blobService.deleteContainerIfExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sasSample();
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
//
|
||||
// Copyright (c) Microsoft and contributors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/**
|
||||
* This sample is used to provide an overview of blob snapshots and how to work with them.
|
||||
*
|
||||
* 1. Upload blocks and commit them.
|
||||
*
|
||||
* 2. Take a snapshot for that blob.
|
||||
*
|
||||
* 3. Re-upload one of the three blocks and commit them.
|
||||
*
|
||||
* 4. Get the snapshot.
|
||||
*
|
||||
* 5. List blobs including snapshots.
|
||||
*
|
||||
* 6. Delete the snapshot.
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var BlobUtilities = azure.BlobUtilities;
|
||||
|
||||
var container = 'snapshotsample';
|
||||
var blob = 'snapshotsample';
|
||||
|
||||
var blockList = ['b1', 'b2', 'b3'];
|
||||
var blockContent = ['content1', 'content2', 'content3'];
|
||||
var blockContentAlternative2 = 'alternative2';
|
||||
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
blobService.setProxy(proxy);*/
|
||||
|
||||
function snapshotSample () {
|
||||
var processArguments = process.argv;
|
||||
if (processArguments.length !== 2) {
|
||||
console.log('Incorrect number of arguments. No arguments should be given.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('Starting snapshotSample.');
|
||||
|
||||
// Create the container
|
||||
createContainer(function () {
|
||||
|
||||
// Upload a blob
|
||||
uploadBlockBlob(function () {
|
||||
|
||||
// Create a snapshot of the blob
|
||||
createSnapshot(function (snapshot) {
|
||||
|
||||
// Update the blob
|
||||
blockContent[1] = blockContentAlternative2;
|
||||
uploadBlockBlob(function () {
|
||||
|
||||
// Create a snapshot of the modified blob
|
||||
getSnapshotToText(snapshot, function () {
|
||||
|
||||
// List the blob and its snapshots
|
||||
listSnapshots(function () {
|
||||
|
||||
// Delete the snapshots
|
||||
deleteSnapshots(function () {
|
||||
|
||||
// Delete the container
|
||||
deleteContainer(function () {
|
||||
console.log('Ending snapshotSample.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer (callback) {
|
||||
// Create the container.
|
||||
blobService.createContainerIfNotExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function uploadBlockBlob(callback) {
|
||||
// Upload 3 blocks and commit them.
|
||||
var blocks = 0;
|
||||
var blobCallbackCounter = function (block) {
|
||||
++blocks;
|
||||
if (blocks === blockList.length) {
|
||||
console.log(' Created ' + blocks + ' blocks.');
|
||||
|
||||
blobService.commitBlocks(container, blob, {LatestBlocks: blockList}, function (error4) {
|
||||
if (error4) {
|
||||
console.log(error4);
|
||||
}
|
||||
else {
|
||||
console.log('Committed the blob ' + blob);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for(var i = 0; i < blockList.length; i++) {
|
||||
console.log(' Uploading a block. ID: ' + blockList[i] + ' Content: ' + blockContent[i]);
|
||||
blobService.createBlockFromText(blockList[i], container, blob, blockContent[i], blockContent[i].length, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
blobCallbackCounter();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function createSnapshot(callback) {
|
||||
// Creates a snapshot.
|
||||
blobService.createBlobSnapshot(container, blob, function (error, snapshot) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Created a snapshot for the blob ' + blob);
|
||||
callback(snapshot);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getSnapshotToText(snapshot, callback) {
|
||||
// Gets a snapshot.
|
||||
blobService.getBlobToText(container, blob, {snapshotId: snapshot}, function (error, text) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Snapshot ' + blob + '?' + snapshot + ' text: ' + text);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function listSnapshots (callback) {
|
||||
// List the blobs, including snapshots
|
||||
blobService.listBlobsSegmented(container, null, { include: BlobUtilities.BlobListingDetails.SNAPSHOTS }, function (error, results) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Listing the blobs under the container ' + container);
|
||||
|
||||
results.entries.forEach(function (blob) {
|
||||
var snapshot = '';
|
||||
if (blob.snapshot) {
|
||||
snapshot = '; BlobSnapshot: ' + blob.snapshot;
|
||||
}
|
||||
console.log(' BlobName: ' + blob.name + snapshot);
|
||||
});
|
||||
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function deleteSnapshots (callback) {
|
||||
// Delete the snapshot.
|
||||
blobService.deleteBlob(container, blob, { deleteSnapshots: BlobUtilities.SnapshotDeleteOptions.SNAPSHOTS_ONLY }, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the snapshots.');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function deleteContainer (callback) {
|
||||
// Delete the container.
|
||||
blobService.deleteContainerIfExists(container, function (error) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
console.log('Deleted the container ' + container);
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
snapshotSample();
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
var fs = require('fs');
|
||||
var assert = require('assert');
|
||||
var util = require('util');
|
||||
var http = require('http');
|
||||
|
||||
var azure;
|
||||
if (fs.existsSync('absolute path to azure-storage.js')) {
|
||||
azure = require('absolute path to azure-storage');
|
||||
} else {
|
||||
azure = require('azure-storage');
|
||||
}
|
||||
|
||||
var TableQuery = azure.TableQuery;
|
||||
var TableUtilities = azure.TableUtilities;
|
||||
var eg = TableUtilities.entityGenerator;
|
||||
|
||||
var tableName = 'tablequerysample';
|
||||
var tableService = azure.createTableService();
|
||||
|
||||
// optionally set a proxy
|
||||
/*var proxy = {
|
||||
protocol: 'http:',
|
||||
host: '127.0.0.1',
|
||||
port: 8888
|
||||
};
|
||||
|
||||
tableService.setProxy(proxy);*/
|
||||
|
||||
var entity1 = {
|
||||
PartitionKey: eg.String('partition1'),
|
||||
RowKey: eg.String('row1'),
|
||||
integerfield: eg.Int32(1),
|
||||
stringfield: eg.String('stringfield value'),
|
||||
longfield: eg.Int64('92233720368547758')
|
||||
};
|
||||
|
||||
var entity2 = {
|
||||
PartitionKey: eg.String('partition1'),
|
||||
RowKey: eg.String('row2'),
|
||||
stringfield: eg.String('stringfield value'),
|
||||
longfield: eg.Int64('8547758')
|
||||
};
|
||||
|
||||
function performTableQuery() {
|
||||
// Create the table
|
||||
tableService.createTable(tableName, function (error1) {
|
||||
assert.equal(error1, null);
|
||||
|
||||
// Insert the entities
|
||||
insertEntities(function() {
|
||||
|
||||
// Return all entities
|
||||
queryAllEntities(function () {
|
||||
|
||||
// Return entities Where certain conditions are met
|
||||
queryEntitiesWhere(function () {
|
||||
|
||||
// Return the Top n entities
|
||||
queryEntitiesTop(function () {
|
||||
|
||||
// Return Select fields from entities
|
||||
queryEntitiesSelect(function () {
|
||||
|
||||
// Delete the table
|
||||
tableService.deleteTable(tableName, function (error2) {
|
||||
assert.equal(error2, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function insertEntities(callback) {
|
||||
// insert the entities
|
||||
tableService.insertEntity(tableName, entity1, function (error1) {
|
||||
assert.equal(error1, null);
|
||||
tableService.insertEntity(tableName, entity2, function (error2) {
|
||||
assert.equal(error2, null);
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function queryAllEntities(callback) {
|
||||
// Select all fields
|
||||
tableService.queryEntities(tableName, null, null, function (error, result) {
|
||||
assert.equal(error, null);
|
||||
assert.notEqual(result, null);
|
||||
assert.notEqual(result.entries, null);
|
||||
|
||||
var entities = result.entries;
|
||||
assert.equal(entities.length, 2);
|
||||
var entityResult = entities[0];
|
||||
assert.equal(entityResult.stringfield._, entity1.stringfield._);
|
||||
assert.equal(entityResult.longfield._, entity1.longfield._);
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function queryEntitiesWhere(callback) {
|
||||
// Select only the entries where the longfield is great than 10,000,000
|
||||
|
||||
// equivalently: var tableQuery = new TableQuery().where('longfield == ?int64?', '10000000');
|
||||
var tableQuery = new TableQuery().where(TableQuery.int64Filter('longfield', TableUtilities.QueryComparisons.GREATER_THAN, '10000000'));
|
||||
|
||||
tableService.queryEntities(tableName, tableQuery, null, function (error, result) {
|
||||
assert.equal(error, null);
|
||||
assert.notEqual(result, null);
|
||||
assert.notEqual(result.entries, null);
|
||||
|
||||
var entities = result.entries;
|
||||
assert.equal(entities.length, 1);
|
||||
|
||||
var entityResult = entities[0];
|
||||
assert.equal(entityResult.longfield._, entity1.longfield._);
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function queryEntitiesTop(callback) {
|
||||
// Select only the top entry
|
||||
var tableQuery = new TableQuery().top(1);
|
||||
|
||||
tableService.queryEntities(tableName, tableQuery, null, function (error, result) {
|
||||
assert.equal(error, null);
|
||||
assert.notEqual(result, null);
|
||||
assert.notEqual(result.entries, null);
|
||||
|
||||
var entities = result.entries;
|
||||
assert.equal(entities.length, 1);
|
||||
|
||||
var entityResult = entities[0];
|
||||
assert.equal(entityResult.integerfield._, entity1.integerfield._);
|
||||
assert.equal(entityResult.longfield._, entity1.longfield._);
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function queryEntitiesSelect(callback) {
|
||||
// Select specific field
|
||||
var tableQuery = new TableQuery().select('integerfield');
|
||||
|
||||
tableService.queryEntities(tableName, tableQuery, null, function (error, result) {
|
||||
assert.equal(error, null);
|
||||
assert.notEqual(result, null);
|
||||
assert.notEqual(result.entries, null);
|
||||
|
||||
var entities = result.entries;
|
||||
assert.equal(entities.length, 2);
|
||||
|
||||
var entityResult = entities[0];
|
||||
assert.equal(entityResult.integerfield._, entity1.integerfield._);
|
||||
assert.equal(entityResult.longfield, undefined);
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
performTableQuery();
|
||||
Reference in new issue
Block a user