Initial commit
This commit is contained in:
commit
ec49fedc45
1288 files changed
+226749
No files matched your search
+3
@@ -0,0 +1,3 @@
|
||||
node_modules/*
|
||||
packages
|
||||
.git
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"bitwise": true,
|
||||
"camelcase": true,
|
||||
"curly": false,
|
||||
"eqeqeq": false,
|
||||
"forin": true,
|
||||
"immed": true,
|
||||
"indent": 2,
|
||||
"latedef": true,
|
||||
"maxparams": false,
|
||||
"maxdepth": false,
|
||||
"maxstatements": false,
|
||||
"maxcomplexity": false,
|
||||
"newcap": true,
|
||||
"noarg": true,
|
||||
"node": true,
|
||||
"noempty": true,
|
||||
"nonew": true,
|
||||
"plusplus": false,
|
||||
"quotmark": "single",
|
||||
"regexp": true,
|
||||
"sub": true,
|
||||
"strict": false,
|
||||
"trailing": true,
|
||||
"undef": true,
|
||||
"unused": true,
|
||||
"shadow": true,
|
||||
"globals": {
|
||||
"FileReader": true,
|
||||
"window": true
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
# git #
|
||||
.git*
|
||||
|
||||
# Node #
|
||||
node_modules/
|
||||
|
||||
# Test #
|
||||
test/
|
||||
|
||||
# Examples #
|
||||
examples/blobuploader/
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4"
|
||||
- "5"
|
||||
- "6"
|
||||
- "7"
|
||||
|
||||
after_script:
|
||||
- npm run coveralls
|
||||
|
||||
install:
|
||||
- npm --version
|
||||
- npm install
|
||||
|
||||
allow_failures:
|
||||
- node_js: "5"
|
||||
- node_js: "7"
|
||||
|
||||
sudo: false
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
Tracking Breaking Changes in 2.0.0
|
||||
|
||||
ALL
|
||||
* Fixed the issue that retry filter will continuously retry for client error like `ETIMEDOUT`.
|
||||
|
||||
BLOB
|
||||
* When specifiying access condition `If-None-Match: *` for read, it will always fail.
|
||||
|
||||
QUEUE
|
||||
* `createMessage` callback has been changed from `errorOrResponse` to `errorOrResult<QueueMessageResult>` which contains `messageId`, `popReceipt`, `timeNextVisible`, `insertionTime` and `expirationTime`. It can be passed to `updateMessage` and `deleteMessage` APIs.
|
||||
|
||||
Tracking Breaking Changes in 1.4.0
|
||||
|
||||
BLOB
|
||||
* Changed `/S` of SpeedSummary to `/s`.
|
||||
|
||||
FILE
|
||||
* Changed `/S` of SpeedSummary to `/s`.
|
||||
|
||||
Tracking Breaking Changes in 1.3.0
|
||||
|
||||
QUEUE
|
||||
* Updated the `QueueMessageResult.dequeueCount` from `string` to `number`.
|
||||
|
||||
Tracking Breaking Changes in 1.2.0
|
||||
|
||||
TABLE
|
||||
* Beginning with version 2015-12-11, the Atom feed is no longer supported as a payload format for Table service operations. Version 2015-12-11 and later versions support only JSON for the payload format.
|
||||
|
||||
Tracking Breaking Changes in 1.0.0
|
||||
|
||||
BLOB
|
||||
* The `blob` property of BlobResult has been renamed to `name` to keep consistent with other services API and the `listBlob` API.
|
||||
* Decoded the block name of LockListResult from base64 string to utf-8 string.
|
||||
|
||||
QUEUE
|
||||
* The `encodeMessage` flag of QueueService has been replaced by `messageEncoder` which support `TextBase64QueueMessageEncoder`, `BinaryBase64QueueMessageEncoder`, `TextXmlQueueMessageEncoder` and custom implementation of QueueMessageEncoder.
|
||||
|
||||
Tracking Breaking Changes in 0.10.0
|
||||
|
||||
ALL
|
||||
* The `signedIdentifiers` parameter and result properties have been changed from array to hash map to avoid non unique signed identifier id.
|
||||
|
||||
BLOB
|
||||
* The `contentType`, `contentEncoding`, `contentLanguage`, `contentDisposition`, 'contentMD5' and `cacheControl` parameters and return values about the blob's content settings are grouped into a `contentSettings` sub property.
|
||||
* The `contentMD5` parameter to verify the integrity of the data during the transport is changed to `transactionalContentMD5`
|
||||
* The `copy*` return values are grouped into a `copy` sub property.
|
||||
* The `lease*` return values are grouped into a `lease` sub property.
|
||||
* The options.accessConditions parameter is changed to AccessConditions type.
|
||||
|
||||
QUEUE
|
||||
* Renamed QueueResult.approximatemessagecount to camelCase and change its type to 'number'.
|
||||
* Renamed the property names of QueueMessageResult to camelCase
|
||||
* Renamed the parameter options.messagettl of the createMessage API to options.messageTimeToLive.
|
||||
* Updated the callback of the createMessage API from errorOrResult to errorOrResponse.
|
||||
* Removed peekOnly option from the options parameter of the getMessages API. To peek messages, use peekMessages instead.
|
||||
|
||||
FILE
|
||||
* The `contentType`, `contentEncoding`, `contentLanguage`, `contentDisposition`, 'contentMD5' and `cacheControl` parameters and return values about the blob's content settings are grouped into a `contentSettings` sub property.
|
||||
* The `contentMD5` parameter to verify the integrity of the data during the transport is changed to `transactionalContentMD5`
|
||||
* The `copy*` return values are grouped into a `copy` sub property.
|
||||
* The options.accessConditions parameter is changed to AccessConditions type.
|
||||
|
||||
TABLE
|
||||
* Renamed the function updateEntity to replaceEntity.
|
||||
* Renamed TableUtilities.entityGenerator.Entity to EntityProperty.
|
||||
|
||||
Tracking Breaking Changes in 0.7.0
|
||||
|
||||
ALL
|
||||
* The generateDevelopmentStorageCredendentials function in the azure-storage.js is renamed to generateDevelopmentStorageCredentials.
|
||||
|
||||
BLOB
|
||||
* The AppendFromLocalFile function in the blobservice.js is renamed to appendFromLocalFile.
|
||||
* The AppendFromStream function in the blobservice.js is renamed to appendFromStream.
|
||||
* The AppendFromText function in the blobservice.js is renamed to appendFromText.
|
||||
* The properties in the properties object of BlobResult and ContainerResult when listing blobs or containers are moved to the result object.
|
||||
* The property names returned from listing blobs or containers are changed to camelCase.
|
||||
* The blob result is added to the result of BlobService.commitBlocks and the blob list information is embedded in it.
|
||||
|
||||
FILE
|
||||
* The properties in the properties object of FileResult and ShareResult when listing files or shares are moved to the result object.
|
||||
* The property names returned from listing files or shares are changed to camelCase.
|
||||
* The property names returned from getting share stats are changed to camelCase.
|
||||
|
||||
Tracking Breaking Changes in 0.5.0
|
||||
|
||||
ALL
|
||||
* The suffix "_HEADER" is removed from all the http header constants.
|
||||
* The generateSharedAccessSignatureWithVersion function in each service is deprecated.
|
||||
* The shouldRetry function in the retry policy filters takes a "requestOption" object instead of a "retryData" object.
|
||||
|
||||
BLOB
|
||||
* The "publicAccessLevel" parameter in the BlobService.setContainerACL function is moved into the "options" parameter.
|
||||
* The properties in the BlobService.setBlobProperties function are moved from the "options" to the "properties" parameter.
|
||||
* The "AccessPolicy.Permission" is renamed to "AccessPolicy.Permissions" on the result object of the BlobService.getContainerAcl function.
|
||||
|
||||
TABLE
|
||||
* The "signedIdentifiers" parameter in the TableService.SetTableACL function is moved out from the "options" parameter.
|
||||
* The "AccessPolicy.Permission" is renamed to "AccessPolicy.Permissions" on the result object of the TableService.getTableAcl function.
|
||||
|
||||
QUEUE
|
||||
* The option "options.messagetext" is renamed to "options.messageText" in the QueueService.UpdateMessage function.
|
||||
* The "AccessPolicy.Permission" is renamed to "AccessPolicy.Permissions" on the result object of the QueueService.getQueueAcl function.
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
# Contribute Code or Provide Feedback
|
||||
|
||||
If you would like to become an active contributor to this project please follow the instructions provided in [Microsoft Azure Projects Contribution Guidelines](https://azure.github.io/guidelines/).
|
||||
|
||||
## Project Steup
|
||||
The Azure Storage development team uses Visual Studio Code so instructions will be tailored to that preference. However, any preferred IDE or other toolset should be usable.
|
||||
|
||||
### Install
|
||||
* Node v0.10, v0.12 or v4
|
||||
* [Visual Studio Code](https://code.visualstudio.com/)
|
||||
|
||||
### Development Environment Setup
|
||||
To get the source code of the SDK via **git** just type:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/Azure/azure-storage-node.git
|
||||
cd ./azure-storage-node
|
||||
```
|
||||
|
||||
Then, run NPM to install all the NPM dependencies:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
### Running
|
||||
Unit tests don't require real credentials and don't require any environment variables to be set. By default the unit tests are run with Nock recording data.
|
||||
|
||||
If you would like to run the unit test against a live storage account, you will need to setup environment variables which will be used. These test will use these credentials to run live tests against Azure with the provided credentials. Note that you will be charged for storage usage. You need verify the clean up script did its job at the end of a test run.
|
||||
|
||||
Unit tests can then be run from root directory using:
|
||||
|
||||
```bash
|
||||
npm test
|
||||
```
|
||||
|
||||
To run unit tests against live storage accounts, please set environment variable to turn off Nock by:
|
||||
|
||||
```bash
|
||||
export NOCK_OFF=true
|
||||
```
|
||||
|
||||
and set up the following environment variables for storage account credentials by:
|
||||
|
||||
```bash
|
||||
export AZURE_STORAGE_CONNECTION_STRING="valid storage connection string"
|
||||
export AZURE_STORAGE_CONNECTION_STRING_PREMIUM_ACCOUNT="optional valid storage connection string for premium storage account"
|
||||
export AZURE_STORAGE_CONNECTION_STRING_SSE_ENABLED_ACCOUNT="optional valid storage connection string for storage account with storage service encryption enabled"
|
||||
```
|
||||
|
||||
Note: `AZURE_STORAGE_CONNECTION_STRING_PREMIUM_ACCOUNT` and `AZURE_STORAGE_CONNECTION_STRING_SSE_ENABLED_ACCOUNT` are optional settings to enable testing suites related to premium storage account and storage service encryption, and only needed to be set when you are developing related features.
|
||||
|
||||
### Testing Features
|
||||
As you develop a feature, you'll need to write tests to ensure quality. Your changes should be covered by both unit tests. You should also run existing tests related to your change to address any unexpected breaks.
|
||||
|
||||
## Pull Requests
|
||||
|
||||
### Guidelines
|
||||
The following are the minimum requirements for any pull request that must be met before contributions can be accepted.
|
||||
* Make sure you've signed the [CLA](https://cla.azure.com/) before you start working on any change.
|
||||
* Discuss any proposed contribution with the team via a GitHub issue **before** starting development.
|
||||
* Code must be professional quality
|
||||
* No style issues
|
||||
* You should strive to mimic the style with which we have written the library
|
||||
* Clean, well-commented, well-designed code
|
||||
* Try to limit the number of commits for a feature to 1-2. If you end up having too many we may ask you to squash your changes into fewer commits.
|
||||
* [ChangeLog.md](ChangeLog.md) needs to be updated describing the new change
|
||||
* Thoroughly test your feature
|
||||
|
||||
### Branching Policy
|
||||
Changes should be based on the **dev** branch, not master as master is considered publicly released code. Each breaking change should be recorded in [BreakingChanges.md](BreakingChanges.md).
|
||||
|
||||
### Adding Features for All Platforms
|
||||
We strive to release each new feature for each of our environments at the same time. Therefore, we ask that all contributions be written for Node v0.10 and later.
|
||||
|
||||
### Review Process
|
||||
We expect all guidelines to be met before accepting a pull request. As such, we will work with you to address issues we find by leaving comments in your code. Please understand that it may take a few iterations before the code is accepted as we maintain high standards on code quality. Once we feel comfortable with a contribution, we will validate the change and accept the pull request.
|
||||
|
||||
Thank you for any contributions! Please let the team know if you have any questions or concerns about our contribution policy.
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Contributors should submit an update to this file with a commit in order to receive recognition. Thank you for your contributions.
|
||||
|
||||
List of Contributors
|
||||
====================
|
||||
|
||||
Microsoft Corporation
|
||||
Microsoft Open Technologies, Inc
|
||||
+761
@@ -0,0 +1,761 @@
|
||||
Note: This is an Azure Storage only package. The all up Azure node sdk still has the old storage bits in there. In a future release, those storage bits will be removed and an npm dependency to this storage node sdk will
|
||||
be taken. This is a GA release and the changes described below indicate the changes from the Azure node SDK 0.9.8 available here - https://github.com/Azure/azure-sdk-for-node.
|
||||
|
||||
2018.02 Version 2.8.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2017-07-29.
|
||||
|
||||
BLOB
|
||||
* Added support for Soft Delete feature.
|
||||
* Fixed several blobuploader example issues.
|
||||
* Fixed a compatibility issue for `getBlobToLocalFile` and `createReadStream` with Node.js v9.
|
||||
* Fixed a bug that blob name will be wrongly trimmed to empty string when listing blobs.
|
||||
* Fixed a bug when blob size > 32M, GetBlobStream returns root blob data instead of snapshot data.
|
||||
|
||||
FILE
|
||||
* Fixed a compatibility issue for `getFileToLocalFile` and `createReadStream` with Node.js v9.
|
||||
|
||||
2017.12 Version 2.7.0
|
||||
|
||||
ALL
|
||||
* Default HTTP connection will enable keep-alive to improve performance.
|
||||
|
||||
BLOB
|
||||
* Added support for `getBlobProperties`, `listBlobsSegmented` and `listBlobsSegmentedWithPrefix` to return `AccessTierChangeTime` and `AccessTierInferred` properties.
|
||||
* Fixed a blob lease support issue for `appendFromText` and `resizePageBlob`.
|
||||
|
||||
TABLE
|
||||
* Fixed an apostrophe missing issue in `TableQuery.where()` example.
|
||||
|
||||
2017.10 Version 2.6.0
|
||||
|
||||
FILE
|
||||
|
||||
* Added support for file share snapshot.
|
||||
|
||||
2017.09 Version 2.5.0
|
||||
|
||||
ALL
|
||||
* Optimized samples and documentation for retry policies.
|
||||
* Added additional samples for blob and file.
|
||||
|
||||
BLOB
|
||||
* Optimized `commitBlocks` API implementation and documentation.
|
||||
|
||||
FILE
|
||||
* Added support for File metrics.
|
||||
|
||||
2017.08 Version 2.4.0
|
||||
|
||||
ALL
|
||||
* Fixed a TypeScript issue that `browserFile` should be `Object` type in the TypeScript definition file.
|
||||
|
||||
BLOB
|
||||
* Added support for getting and setting a tier for a block blob under a LRS Blob Storage Account from tiers hot, cool and archive.
|
||||
|
||||
2017.08 Version 2.3.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2017-04-17. For more information, please see - https://docs.microsoft.com/en-us/rest/api/storageservices/versioning-for-the-azure-storage-services
|
||||
* Updated the dependency of the 'request' module to avoid security vulnerability: (https://snyk.io/test/npm/azure-storage).
|
||||
* Added `requestServerEncrypted` property to `ServiceResponse` which indicates if the contents of the request have been successfully encrypted.
|
||||
* Improved API documentation.
|
||||
|
||||
BLOB
|
||||
* PageBlobs: For Premium Accounts only, added support for getting and setting the tier on a page blob. The tier can also be set when creating or copying from an existing page blob.
|
||||
|
||||
FILE
|
||||
* Added `serverEncryped` property to `FileResult` and `DirectoryResult` which indicates if the file data and application metadata are completely encrypted using the specified algorithm on the server.
|
||||
* Fixed a TypeScript issue that SharedAccessPermissions for FileUtilities is missing in type definition file.
|
||||
|
||||
TABLE
|
||||
* Fixed a typo in table query example for combineFilters function.
|
||||
|
||||
2017.08 Version 2.2.2
|
||||
|
||||
ALL
|
||||
|
||||
* Fixed a retry timeout issue during uploading.
|
||||
|
||||
2017.07 Version 2.2.1
|
||||
|
||||
BLOB
|
||||
|
||||
* Optimized memory usage especially for uploading blobs with large block size.
|
||||
|
||||
2017.06 Version 2.2.0
|
||||
|
||||
ALL
|
||||
* Fixed a TypeScript issue that callback of `createWriteStreamToBlockBlob` should be optional in the TypeScript definition file.
|
||||
* Fixed a bug in TypeScript definition file about `AccessConditions` mismatches with `AccessCondition` which is exported in JavaScript.
|
||||
* Fixed an undefined property issue in `BlobResult` object of the sample code.
|
||||
* Updated node-uuid to uuid.
|
||||
* Updated underscore version to 1.8.3.
|
||||
* Updated validator version to 3.35.0.
|
||||
|
||||
BLOB
|
||||
* Added a `defaultEnableReuseSocket` option for `BlobService` to control reuseSocket settings.
|
||||
* Fixed a hanging or silent failing issue for blob uploading under some situations.
|
||||
* Fixed a bug that `doesBlobExist` does not support `snapshotId` parameter.
|
||||
* Fixed a bug in `getBlobToLocalFile` that `fs` will throw exceptions instead of returning errors in callback.
|
||||
|
||||
FILE
|
||||
* Added a `defaultEnableReuseSocket` option for `FileService` to control reuseSocket settings.
|
||||
* Fixed a hanging or silent failing issue for file uploading under some situations.
|
||||
* Fixed a bug in `getFileToLocalFile` that `fs` will throw exceptions instead of returning errors in callback.
|
||||
|
||||
2017.03 Version 2.1.0
|
||||
|
||||
ALL
|
||||
* Fixed the type script issue that AccessConditions is missing in the type definition file.
|
||||
|
||||
BLOB
|
||||
* Added support for page blob incremental copy. Refer to https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/incremental-copy-blob
|
||||
|
||||
QUEUE
|
||||
* Fixed the issue that `responseObject` may not have response body in `createMessage` function.
|
||||
|
||||
BROWSER
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.1.0
|
||||
|
||||
2017.01 Version 2.0.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2016-05-31. Fore more information, please see - https://msdn.microsoft.com/en-us/library/azure/dd894041.aspx
|
||||
* Fixed the issue that `BatchOperation` doesn't support socket reuse for some node versions.
|
||||
* Fixed the issue that `BatchOperation` request pool size is too big when the socket reuse is supported.
|
||||
* Added empty headers to string-to-sign.
|
||||
* For response has body and no `content-type` header, try to parse the body using xml format.
|
||||
* Fixed the issue that retry filter will continuously retry for client error like `ETIMEDOUT`.
|
||||
* Added support for client side timeout. In order to set the timeout per API, please use `options.clientRequestTimeoutInMs`. To set the default value for all requests made via a particular service, please use `{blob|queue|table|file}Service.defaultClientRequestTimeoutInMs`.
|
||||
|
||||
BLOB
|
||||
* Added support for large block blob.
|
||||
* Added `publicAccessLevel` to `ContainerResult` for the APIs `listContainersSegmented` and `listContainersSegmentedWithPrefix`.
|
||||
* When specifying access condition `If-None-Match: *` for reading, it will always fail.
|
||||
* Returned content MD5 for range gets Blobs.
|
||||
* Fixed the issue that `useTransactionalMD5` didn't take effect for downloading a big blob.
|
||||
|
||||
QUEUE
|
||||
* `createMessage` callback has been changed from `errorOrResponse` to `errorOrResult<QueueMessageResult>` which contains `messageId`, `popReceipt`, `timeNextVisible`, `insertionTime` and `expirationTime`. It can be passed to updateMessage and deleteMessage APIs.
|
||||
|
||||
FILE
|
||||
* Returned content MD5 for range gets Files.
|
||||
* Fixed the issue that `useTransactionalMD5` didn't take effect for downloading a big file.
|
||||
* Added support for listing files and directories with prefix, refer to `FileService.listFilesAndDirectoriesSegmentedWithPrefix`.
|
||||
|
||||
TABLE
|
||||
* Fixed the issue that response in incorrect for table batch operation when the error response item is not the first item in the responses.
|
||||
|
||||
2016.11 Version 1.4.0
|
||||
|
||||
ALL
|
||||
* Added `ENOTFOUND` for secondary endpoint and `ECONNREFUSED` to `RetryPolicyFilter`.
|
||||
* Added support for `text/html` error response body.
|
||||
|
||||
BLOB
|
||||
* Fixed the issue that the ChunkAllocator maxCount is aligned with parallelOperationThreadCount.
|
||||
* Changed `/S` of SpeedSummary to `/s`.
|
||||
* Fixed the issue that `BlobService.createBlockBlobFromText` will hang when passed `null` or `undefined` `text` argument.
|
||||
* Fixed the issue that `BlobService.createBlockBlobFromText` will always set `content-type` to `text/plain`.
|
||||
|
||||
QUEUE
|
||||
* Allowed `QueueService.peekMessages` against secondary endpoint.
|
||||
|
||||
FILE
|
||||
* Fixed the issue that the ChunkAllocator maxCount is aligned with parallelOperationThreadCount.
|
||||
* Changed `/S` of SpeedSummary to `/s`.
|
||||
|
||||
2016.10 Version 1.3.2
|
||||
|
||||
BLOB
|
||||
* Prevent a blockId from being generated with a decimal point.
|
||||
|
||||
2016.09 Version 1.3.1
|
||||
|
||||
ALL
|
||||
* Improved the type script support.
|
||||
|
||||
2016.08 Version 1.3.0
|
||||
|
||||
ALL
|
||||
|
||||
* Fixed the issue that retry filter will fail against storage emulator.
|
||||
* Fixed a hang issue of `StorageServiceClient` with retry policy filter set when retrying sending the request, the stream is not readable anymore.
|
||||
* Updated the default value of `CorsRule.ExposedHeaders`, `CorsRule.AllowedHeaders` to empty and `CorsRule.MaxAgeInSeconds` to `0` for `setServiceProperties` APIs of all services.
|
||||
* Fixed the issue that service SAS doesn't work if specifying the `AccessPolicy.Protocols`.
|
||||
|
||||
BLOB
|
||||
* Added the API `BlobService.getPageRangesDiff` for getting the page ranges difference. Refer to https://msdn.microsoft.com/en-us/library/azure/mt736912.aspx for more detailed information.
|
||||
|
||||
QUEUE
|
||||
* Updated the `QueueMessageResult.dequeueCount` from `string` to `number`.
|
||||
* Added the API `QueueService.getUrl` for getting the queue url.
|
||||
|
||||
TABLE
|
||||
* Added the API `TableService.getUrl` for getting the table url.
|
||||
|
||||
2016.07 Version 1.2.0
|
||||
|
||||
ALL
|
||||
* Fixed the issue that metadata name will be converted to lower-case after retrieving back from the server. **Note** that this fix is only applicable for Node 0.12 or higher version.
|
||||
* Added support for EndpointSuffix for all service constructors.
|
||||
* Updated storage service version to 2015-12-11. Fore more information, please see - https://msdn.microsoft.com/en-us/library/azure/dd894041.aspx
|
||||
* Updated the `request` package to version 2.74.0 to address the security vulnerability - https://nodesecurity.io/advisories/130
|
||||
|
||||
BLOB
|
||||
* Fixed the issue that the service error message will be written to the destination stream if getting error when downloading the blob to a stream/file.
|
||||
* Added `serverEncryped` property to `BlobResult` class which indicates if the blob data and application metadata are completely encrypted using the specified algorithm on the server.
|
||||
|
||||
FILE
|
||||
* Fixed the issue that the service error message will be written to the destination stream if getting error when downloading the file to a stream/file.
|
||||
|
||||
TABLE
|
||||
* The atom feed payload format is not supported anymore for table service APIs.
|
||||
|
||||
2016.06 Version 1.1.0
|
||||
|
||||
ALL
|
||||
* Fixed the issue that using SAS doesn't work against storage emulator.
|
||||
* Fixed the issue that the service SAS signature is incorrect when protocol parameter is specified.
|
||||
* Fixed the issue that the timeout query string should be in seconds instead of milliseconds.
|
||||
|
||||
BLOB
|
||||
* Added parameter snapshotId to BlobService.getUrl function to support getting url of a specified snapshot.
|
||||
* Fixed the issue that the getUrl doesn't work against storage emulator.
|
||||
* Fixed the race issue that the _rangeList may be deleted before using it in the BlockRangeStream._getTypeList function.
|
||||
* Fixed the issue that downloading block blob with size bigger than 32MB will fail when using anonymous credential.
|
||||
* Added `CREATE` to `BlobUtilities.SharedAccessPermissions`.
|
||||
|
||||
TABLE
|
||||
* Supported string type value for entity PartionKey and RowKey.
|
||||
* Supported implicit Edm type value for entity properties. The supported implicit Edm types including Int32, Double, Bool, DateTime and String.
|
||||
|
||||
FILE
|
||||
* Fixed the issue that the getUrl doesn't work against storage emulator.
|
||||
* Added `CREATE` to `FileUtilities.SharedAccessPermissions`.
|
||||
|
||||
2016.05 Version 1.0.1
|
||||
|
||||
ALL
|
||||
* Fixed the issue that StorageServiceClient._normalizeError will throw exception on Node below v4 because string.startsWith is not available on Node below v4.
|
||||
|
||||
2016.05 Version 1.0.0
|
||||
|
||||
BLOB
|
||||
* The `blob` property of BlobResult has been renamed to `name` to keep consistent with other services API and the `listBlob` API.
|
||||
* Decoded the block name of LockListResult from base64 string to utf-8 string.
|
||||
|
||||
QUEUE
|
||||
* The `encodeMessage` flag of QueueService has been replaced by `messageEncoder` which support `TextBase64QueueMessageEncoder`, `BinaryBase64QueueMessageEncoder`, `TextXmlQueueMessageEncoder` and custom implementation of QueueMessageEncoder.
|
||||
|
||||
TABLE
|
||||
* Fixed the issue that loses the data type for Edm.Double value like: 1.0.
|
||||
* Fixed the issue that loses the data precision for Edm.Int64 value when it is outisde of the range (2^53 - 1) to -(2^53 - 1).
|
||||
|
||||
2016.03 Version 0.10.0
|
||||
|
||||
ALL
|
||||
* The `signedIdentifiers` parameter and result properties have been changed from array to hash map to avoid non unique signed identifier id.
|
||||
|
||||
BLOB
|
||||
* Added 'COPY' to the BlobUtilities.BlobListingDetails to include copy information in the results.
|
||||
* Added 'bytesCopied' and 'totalBytes' to the blob result.
|
||||
* Added the blob result to the callback of BlobService.commitBlocks.
|
||||
* Moved the properties in the properties object of BlobResult and ContainerResult when listing blobs or containers to the result object.
|
||||
* Renamed the property names returned from listing blobs or containers to camelCase.
|
||||
* The `contentType`, `contentEncoding`, `contentLanguage`, `contentDisposition`, 'contentMD5' and `cacheControl` parameters and return values about the blob's content settings are grouped into a `contentSettings` sub property.
|
||||
* The `contentMD5` parameter to verify the integrity of the data during the transport is changed to `transactionalContentMD5`
|
||||
* The `copy*` return values are grouped into a `copy` sub property.
|
||||
* The `lease*` return values are grouped into a `lease` sub property.
|
||||
* The options.accessConditions parameter is changed to AccessConditions type.
|
||||
|
||||
QUEUE
|
||||
* Renamed QueueResult.approximatemessagecount to camelCase and change its type to 'number.
|
||||
* Renamed the property names of QueueMessageResult to camelCase.
|
||||
* Renamed the parameter options.messagettl of the createMessage API to options.messageTimeToLive.
|
||||
* Updated the callback of the createMessage API from errorOrResult to errorOrResponse.
|
||||
* Removed peekOnly option from the options parameter of the getMessages API. To peek messages, use peekMessages instead.
|
||||
* Added getMessage and peekMessage API.
|
||||
|
||||
FILE
|
||||
* Moved the properties in the properties object of FileResult and ShareResult when listing files or shares to the result object.
|
||||
* Renamed the property names returned from listing files or shares to camelCase.
|
||||
* Renamed the property names returned from getting share stats to camelCase.
|
||||
* The `contentType`, `contentEncoding`, `contentLanguage`, `contentDisposition`, 'contentMD5' and `cacheControl` parameters and return values about the blob's content settings are grouped into a `contentSettings` sub property.
|
||||
* The `contentMD5` parameter to verify the integrity of the data during the transport is changed to `transactionalContentMD5`
|
||||
* The `copy*` return values are grouped into a `copy` sub property.
|
||||
* Fixed the issue that SAS tokens created from table names with upper-case letters do not work.
|
||||
* The options.accessConditions parameter is changed to AccessConditions type.
|
||||
|
||||
TABLE
|
||||
* Fixed the issue that getTableAcl returns empty array with signedIdentifier property.
|
||||
* Renamed the function updateEntity to replaceEntity.
|
||||
* Renamed TableUtilities.entityGenerator.Entity to EntityProperty.
|
||||
|
||||
2016.03 Version 0.9.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2015-04-05. Fore more information, please see - https://msdn.microsoft.com/en-us/library/azure/dd894041.aspx
|
||||
* Added support for Account SAS.
|
||||
* Added support for IPACL and Protocols options for service SAS.
|
||||
* Fixed the issue where the authentication may fail when a metadata key is in upper case.
|
||||
* Added 'nsp check' task for security vulnerability check.
|
||||
* Updated the dependency of the 'request' module to avoid the security vulnerability reported by the 'nsp' tool.
|
||||
|
||||
BLOB
|
||||
* Added new permission 'ADD' for service SAS.
|
||||
|
||||
FILE
|
||||
* Added support for metrics setting for service properties.
|
||||
|
||||
2016.01 Version 0.8.0
|
||||
|
||||
ALL
|
||||
* Preview release of the TypeScript definition file at "lib/azure-storage.d.ts".
|
||||
|
||||
BLOB
|
||||
* Added the blob result to the callback of BlobService.commitBlocks.
|
||||
* Added the speed summary to the downloading APIs.
|
||||
|
||||
2015.12 Version 0.7.0
|
||||
|
||||
ALL
|
||||
* Fixed the typo in the function generateDevelopmentStorageCredentials.
|
||||
* Fixed the issue that the HTTP global agent setting is changed during parallel uploading and downloading and impacts on other Node.js applications.
|
||||
* Fixed the issue that the chunked stream writing methods do not accept string.
|
||||
* Fixed the issue that the request fails when the content-length is set to string '0' in the 'sendingRequestEvent' event handler.
|
||||
* Supported retry on XML parsing errors when the XML in the response body is corrupted.
|
||||
* Replaced the dependency "mime" to "browserify-mime" to work with Browserify.
|
||||
|
||||
BLOB
|
||||
* Added an option to skip the blob or file size checking prior to the actual downloading.
|
||||
* Fixed the issue that it doesn't callback when loses the internet connection during uploading/uploading.
|
||||
* Fixed the issue that the local file cannot be removed in the callback when uploading a blob from a local file.
|
||||
* Fixed the issue that the stream length doesn't work when it is larger than 32MB in the createBlockBlobFromStream, createPageBlobFromStream, createAppendBlobFromStream and appendFromStream functions.
|
||||
* Fixed the issue that it doesn't return error in the page range validation when the size exceeds the limit.
|
||||
* Renamed the function AppendFromLocalFile to appendFromLocalFile.
|
||||
* Renamed the function AppendFromStream to appendFromStream.
|
||||
* Renamed the function AppendFromText to appendFromText.
|
||||
|
||||
TABLE
|
||||
* Fixed the issue that listTablesSegmentedWithPrefix with maxResult option throws exception.
|
||||
|
||||
2015.09 Version 0.6.0
|
||||
|
||||
ALL
|
||||
* Fixed the MD5 mismatch issue in uploading a blob when running with Node.js v4.0 or io.js.
|
||||
|
||||
BLOB
|
||||
* Fixed the issue that it doesn't prompt appropriate error message when the source URI is missing in BlobService.startCopyBlob().
|
||||
|
||||
2015.08 Version 0.5.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2015-02-21. For more information, please see - https://msdn.microsoft.com/en-us/library/azure/dd894041.aspx
|
||||
* Unified the function parameters for setting ACL of container, table, queue and share.
|
||||
* Renamed 'AccessPolicy.Permission' to 'AccessPolicy.Permissions' on the result object from get*Acl to match the property name on the signedIdentifier property passed to set*Acl calls.
|
||||
* Unified the name pattern of the http header constants.
|
||||
* Split the property parameters from the options parameter for setting properties of blob, share and file.
|
||||
* Updated the error message when an argument is in a wrong type while it requires a string.
|
||||
* Exported AccessCondition to generate an object that represents a condition.
|
||||
* Fixed an issue that the SAS in the SharedAccessSignature part of the connection string cannot start with '?'.
|
||||
* Deprecated the generateSharedAccessSignatureWithVersion() method in each service.
|
||||
|
||||
BLOB
|
||||
* Supported operating against append blobs.
|
||||
* Fixed an issue that the file descriptor in the FileReadStream is not closed.
|
||||
|
||||
QUEUE
|
||||
* Fixed an issue that options.messageText doesn't work in QueueService.updateMessage.
|
||||
|
||||
2015.06 Version 0.4.5
|
||||
|
||||
* Updated the dependency of the 'request' module to avoid the security vulnerability reported by the 'nsp' tool: (https://nodesecurity.io/advisories/qs_dos_extended_event_loop_blocking) and (https://nodesecurity.io/advisories/qs_dos_memory_exhaustion).
|
||||
* Included package validation in grunt tasks.
|
||||
|
||||
2015.05 Version 0.4.4
|
||||
|
||||
ALL
|
||||
* Updated the dependency of the 'validator' module to avoid the security vulnerability reported by the 'nsp' tool. (https://nodesecurity.io/advisories/validator-isurl-denial-of-service)
|
||||
* Updated the error message when an argument is in a wrong type while it requires a string.
|
||||
* Updated the grunt file to run test with mocha and generate jsDoc.
|
||||
|
||||
BLOB
|
||||
* Fixed an issue that the metadata is duplicated when creating a page blob.
|
||||
* Fixed an issue that the metadata is duplicated when setting blob's metadata with metadata in the options.
|
||||
* Fixed an issue that cannot create an empty block blob with useTransactionalMD5 option.
|
||||
|
||||
FILE
|
||||
* Fixed an issue that the result of file downloading contains wrong values for share, directory or file names.
|
||||
|
||||
TABLE
|
||||
* Fixed an issue that it prompts "Cannot set property 'isSuccessful' of null" when TableService.createTableIfNotExists is called.
|
||||
|
||||
2015.03 Version 0.4.3
|
||||
|
||||
ALL
|
||||
* Fixed an issue that setting metadata keys are converted into lowercase. The metadata keys retrieved from the service will however still be converted into lowercase by the http component of Node.js.(https://github.com/joyent/node/issues/1954)
|
||||
* Included all storage error code strings in the error constants definition.
|
||||
* Documented the client request ID option in all APIs.
|
||||
|
||||
BLOB
|
||||
* Supported listing blob virtual directories.
|
||||
* Fixed an issue that exception is thrown when downloading a blob larger than 32MB to a stream.
|
||||
* Fixed an issue that the process exits when the free memory is low.
|
||||
|
||||
2014.12 Version 0.4.2
|
||||
|
||||
ALL
|
||||
* Fixed an issue that batch operation could probably wait without callback.
|
||||
* Added the readable-stream module to adapt stream operations in both node 0.8 and node 0.10.
|
||||
* Supported nock in tests.
|
||||
|
||||
BLOB
|
||||
* Supported trimming the default port of http or https when getting URL for the blob service.
|
||||
* Fixed an issue that the metadata is not populated when getting the blob to text.
|
||||
|
||||
FILE
|
||||
* Supported trimming the default port of http or https when getting URL for the file service.
|
||||
|
||||
2014.11.28 Version 0.4.1
|
||||
|
||||
ALL
|
||||
* Fixed an issue where the request does not invoke the callback when the input stream ends with an 'end' event instead of a 'finish' event.
|
||||
* Fixed an issue where the request does not invoke the callback when the input stream ends with a 'close' event on Node 0.8.X.
|
||||
* Fixed an issue that the temporary files generated by unit tests are not cleaned up.
|
||||
* Fixed an issue that the unit tests may fail when the file generated by previous tests is not accessible temporarily.
|
||||
|
||||
FILE
|
||||
* Added support to download a single file in parallel similar to upload. You can set ‘parallelOperationThreadCount’ option for api’s that download a file to indicate number of parallel operations to use for download.
|
||||
|
||||
TABLE
|
||||
* Fixed an issue which caused invalid input errors when the partition key or the row key contains an apostrophe.
|
||||
|
||||
2014.10.28 Version 0.4.0
|
||||
|
||||
ALL
|
||||
* Provide an option to enable/disable nagling. Nagling is disabled by default. It can be enabled by setting options.useNagleAlgorithm to true.
|
||||
* Added batch operation callback in sequence mode.
|
||||
|
||||
BLOB
|
||||
* Added support to download a single blob in parallel similar to upload. You can set ‘parallelOperationThreadCount’ option for api’s that download a blob to indicate number of parallel operations to use for download.
|
||||
* Added speed summary in blob downloading.
|
||||
|
||||
FILE
|
||||
* Fixed an issue which caused an invalid resource name error when the directory name starts or ends with a '/'
|
||||
|
||||
2014.08.20 Version 0.3.3
|
||||
|
||||
BLOB
|
||||
* Fixed an issue where SAS tokens were being incorrectly generated for the root container and when the blob name required encoding.
|
||||
* Documented the 'parallelOperationThreadCount' option as input to various uploadBlob APIs.
|
||||
|
||||
FILE
|
||||
* Fixed an issue where signing was incorrect when the URI contained '.' or '..'.
|
||||
* Fixed an issue where "getURI" was requiring a file parameter, although the parameter should be optional.
|
||||
|
||||
2014.07.25 Version 0.3.2
|
||||
|
||||
ALL
|
||||
* Fixed an issue which prevented transient server errors like ECONNRESET, ESOCKETTIMEDOUT and ETIMEDOUT from being retried.
|
||||
|
||||
BLOB
|
||||
* Fixed an issue which caused a reference error in blobs due to 'err' not being defined.
|
||||
|
||||
2014.07.22 Version 0.3.1
|
||||
|
||||
ALL
|
||||
* Fixed an issue which failed to validate special names for containers and tables.
|
||||
* Exposed the Validation utility methods so users can use it to validate resource names.
|
||||
|
||||
BLOB
|
||||
* Fixed an issue which caused failures when an error was encountered while uploading big blobs.
|
||||
|
||||
2014.07.07 Version 0.3.0
|
||||
|
||||
BLOB
|
||||
* Fixed an issue which failed to return single item blocklists while doing listBlocks.
|
||||
|
||||
FILE
|
||||
* Added File Service support. The File Service and the associated APIs are in preview.
|
||||
|
||||
2014.07.01 Version 0.2.1
|
||||
|
||||
ALL
|
||||
* Fixed an issue with casing which caused the module to fail on linux machines.
|
||||
|
||||
BLOB
|
||||
* Fixed an issue which failed to upload an empty blob for empty block blob files uploaded using createBlockBlobFromFile when retry policies were used.
|
||||
|
||||
2014.06.16 Version 0.2.0
|
||||
|
||||
ALL
|
||||
* Updated storage service version to 2014-02-14. The SAS tokens generated will therefore contain a signed version of 2014-02-14 and all the requests using SAS credentials have the api-version query parameter appended to the URI.
|
||||
|
||||
2014.06.12 Version 0.1.0
|
||||
|
||||
ALL
|
||||
* The package has been renamed to azure-storage.
|
||||
* There is no separate azure-common sub-package. Everything is condensed into a single package called azure-storage.
|
||||
* azure.create*Service supports the following configurations:
|
||||
* create*Service(connection string)
|
||||
* create*Service(account name, account key)
|
||||
* create*Service(storage host, sasToken)
|
||||
* Added the ability to choose which SAS Version to use when generating Shared Access Signatures (either the 2012-02-12 or 2013-08-15 versions).
|
||||
* Host can be given as either an object with primaryHost and secondaryHost or as a string representing the primary.
|
||||
* The order credentials are assessed is consistent: If parameters are passed in, they and they alone are honored. There is no mixing (ex, if users pass in host, they won't get the access key from your env var). If no parameters are passed in, the env vars are used. Within both those strategies, the order of evaluation is emulator (in the case of EMULATED env var only), connection string, account/key or SAS, anonymous. If account/key and SAS are passed in, an error is thrown indicating the credentials are invalid.
|
||||
* Removed the ability to create a storage service from a config file.
|
||||
* azure.create*Service supports secondaryHost only, but either primaryHost or secondaryHost must be provided.
|
||||
* Storage service client constructors take the sasToken rather than a credentials object, meaning that the SharedAccessSignature class no longer needs to be used by consumers.
|
||||
* Ensure request is always built/signed for every retry attempt.
|
||||
* Custom retry policies should implement the shouldRetry method and return an object with the retryInterval and retryable information in it. Optionally, they can also set the locationMode and targetLocation to which the request should be sent.
|
||||
* Implemented RA-GRS support. For more information on this, please see - http://blogs.msdn.com/b/windowsazurestorage/archive/2013/12/04/introducing-read-access-geo-replicated-storage-ra-grs-for-windows-azure-storage.aspx
|
||||
* Service properties updated: replaced metrics with hour metrics and minute metrics, added cors support, added defaults if properties are unspecified to reduce invalid xml exceptions.
|
||||
* Added maximum execution time, settable for all requests via {blob|queue|table}service.defaultMaximumExecutionTime or for an individual request via options.maximumExecutionTime. This applies to all requests except blob downloads.
|
||||
* The blob, queue and table services no longer set/modify any values within the options object optionally passed in by the user for every API.
|
||||
* Fixed an issue where null, empty and white-space only metadata header values were allowed.
|
||||
* "timeout" query parameter will not be sent to the server if not set by the user. In order to set the timeout per API, please use options.timeoutIntervalInMs. To set it for all requests made via a particular service, please use {blob|queue|table}Service.defaultTimeoutIntervalInMs.
|
||||
* ECONNRESET is handled in the retry policy filter.
|
||||
* DevStore with secondary access is supported.
|
||||
* Set*Acl takes signed identifiers as a parameter - before it was in options.
|
||||
* GenerateSharedAccessSignature() produces a query string rather than an object.
|
||||
* The SharedKey class has the methods to create shared access signatures. These should not be in the SharedAccessSignature class because one can only create a SAS if they have the account/key.
|
||||
* The service client no longer evaluates the port and protocol set on it. Before, if storageserviceclient.host was called and port and protocol had been changed after host was set, host would be incorrect.
|
||||
* Removed SharedKeyLite and SharedKeyLiteTable in favor of SharedKey and SharedKeyTable which were already the defaults.
|
||||
* Added separate utility files for Blob, Queue, Table, and Storage. These contain useful enums that were previously found in a single Constants file as well as new utility functions. The old Constants file will mostly include internal-use constants.
|
||||
|
||||
BLOB
|
||||
* createBlobServiceWithSas has been provided to create a blobService to use with a Shared Access Signature. Users can create a blobService using azure.createBlobServiceWithSas(host, sasToken).
|
||||
* Anonymous access for the blob service is supported.
|
||||
* Added support for 2013-08-15 Blob SAS changes.
|
||||
* Renamed listBlobs to listBlobsSegmented and added listBlobsSegmentedWithPrefix. listBlobsSegmented takes in currentToken and listBlobsSegmentedWithPrefix takes in prefix and currentToken along with other parameters. Please look at the documentation of these APIs for a list of parameters that users can set on the options object. These APIs return only error, result and response. The result contains entries which is a list of blobs and a continuation token for successive listing operations.
|
||||
* Renamed copyBlob to startCopyBlob.
|
||||
* Renamed putBlockBlobFromStream to _putBlockBlobFromStream and putBlockBlobFromFile to _putBlockBlobFromFile. These are internal methods and should not be called by users directly. Please use createBlockBlobFromStream and createBlockBlobFromFile.
|
||||
* Renamed page methods: listBlobRegions to listPageRanges, createBlobPagesFromText to createPagesFromText, clearBlobPages to clearPageRanges.
|
||||
* Renamed block methods: createBlobBlockFromStream to createBlockFromStream, createBlobBlockFromText to createBlockFromText, commitBlobBlocks to commitBlocks, listBlobBlocks to listBlocks.
|
||||
* BlobService lease methods also work with container leases, simply specify null for the blob parameter.
|
||||
* Added changeLease method to modify the lease ID of an active lease.
|
||||
* Fixed lease issues, break lease allows a leaseBreakPeriod of 0, lease time header is returned as an int rather than a string.
|
||||
* Added snapshot delete options: when deleting a blob with snapshots, users can decide to delete blob and snapshots or just snapshots. The options are provided in BlobUtilities.SnapshotDeleteOptions.*.
|
||||
* deleteBlob callback does not return isSuccessful.
|
||||
* Added does{Container|Blob}Exist methods in blobservice that can be used to determine if a particular container or blob exists.
|
||||
* Added delete{Container|Blob}IfExists methods in blobservice that can be used to delete a container or a blob only when they exist.
|
||||
* Added ContentMD5 validation for blob downloads. This is turned on by default for all block blob downloads and all page blob range downloads as long as the range is less than 4MB. In order to turn this off, set options.disableContentMD5Validation to true explicitly.
|
||||
* x-ms-range-get-content-md5 is set internally by the library and users do not have to specify it in the options.
|
||||
* Removed the option of doing range downloads while downloading a blob to text.
|
||||
* Added sequence number support for page blobs. Users can set the sequence number of a page blob using blobService.setPageBlobSequenceNumber and manage concurrency issues using x-ms-if-sequence-number-le, x-ms-if-sequence-number-lt and x-ms-if-sequence-number-eq. Please see http://msdn.microsoft.com/en-us/library/azure/ee691975.aspx for more details about using sequence numbers.
|
||||
* Removed createBlob API. Please use createPageBlob and createBlockBlob* APIs to create the respective blobs.
|
||||
* Renamed createPagesFromText to _createPagesFromText. This is an internal method and should not be called by users directly.
|
||||
* Added resizePageBlob that can be used to resize a page blob.
|
||||
* createPageBlob takes in the blob's sequence number using options.sequenceNumber.
|
||||
* All the BlobService APIs that took cacheControl/cacheControlHeader, contentType/contentTypeHeader, contentEncoding/contentEncodingHeader, contentLanguage/contentLanguageHeader, contentMD5/contentMD5Header in options only take cacheControl, contentType, contentEncoding, contentLanguage and contentMD5. These are the values that will be set on the blob at the server.
|
||||
* createPagesFromStream takes in the following 2 options - useTransactionalMD5 to calculate and send/validate content MD5 for each transaction and contentMD5 which is an optional hash value. When contentMD5 is provided, the client library uses that instead of trying to calculate it based on the data being uploaded.
|
||||
* createBlockBlobFromText, createBlockFromStream and createBlockFromText allows users to set useTransactionalMD5 on options to calculate and send/validate content MD5 for each transaction.
|
||||
* Added createWriteStreamToBlockBlob, createWriteStreamToNewPageBlob and createWriteStreamToExistingPageBlob that provide a stream for writing to the blob.
|
||||
* Added createReadStream that provides a stream for reading from the blob.
|
||||
* blobService can have a default parallelOperationThreadCount which specifies the number of parallel upload operations that may be performed when uploading a blob that is greater than the value specified by singleBlobPutUploadThresholdInBytes. This value can be set using blobService.parallelOperationThreadCount. The default value set on blobService is 1.
|
||||
* createBlockBlobFromText supports text Buffers in addition to strings.
|
||||
* createBlockBlobFromText will throw an error if the uploaded content exceeds 64MB.
|
||||
* BlobService getBlobUrl has been renamed to getUrl as it can also produce container urls. It can no longer create a SAS token (it used to take sharedAccessPolicy) but can take a sasToken produced by the generateSharedAccessSignature method and produce a url with that.
|
||||
|
||||
QUEUE
|
||||
* Added Shared Access Signatures for queues. createQueueServiceWithSas has been provided to create a queueService to use with a Shared Access Signature. Users can create a queueService using azure.createQueueServiceWithSas(host, sasToken).
|
||||
* Renamed listQueues to listQueuesSegmented and added listQueuesSegmentedWithPrefix. listQueuesSegmented takes in currentToken and listQueuesSegmentedWithPrefix takes in prefix and currentToken along with other parameters. Please look at the documentation of these APIs for a list of parameters that users can set on the options object. These APIs return only error, result and response. The result contains entries which is a list of queues and a continuation token for successive listing operations.
|
||||
* deleteQueue and deleteMessage callbacks do not return isSuccessful.
|
||||
* Added an option in the queue service to turn base 64 encoding off.
|
||||
|
||||
TABLE
|
||||
* Added Shared Access Signatures for tables. createTableServiceWithSas has been provided to create a tableService to use with a Shared Access Signature. Users can create a tableService using azure.createTableServiceWithSas(host, sasToken).
|
||||
* Added JSON support for tables and removed AtomPub. For the different flavours of JSON supported, please see - http://blogs.msdn.com/b/windowsazurestorage/archive/2013/12/05/windows-azure-tables-introducing-json.aspx.
|
||||
* Renamed queryTables to listTablesSegmented and added listTablesSegmentedWithPrefix. listTablesSegmented takes in currentToken and listTablesSegmentedWithPrefix takes in prefix and currentToken along with options and callback. Please look at the documentation of these APIs for a list of parameters that users can set on the options object. These APIs return only error, result and response. The result contains entries which is a list of tables and a continuation token for successive listing operations.
|
||||
* deleteTable and deleteEntity callbacks do not return isSuccessful.
|
||||
* queryEntities in TableService returns 3 parameters instead of 4: the entities and queryResultContinuation parameters are returned within a queryResult object.
|
||||
* Added support for disabling echo content in inserts. By default, inserts do not echo content.
|
||||
* Changed table error parsing so that the code string and message string are directly accessible from the error object.
|
||||
* Removed the getTable method; doesTableExist works similarly.
|
||||
* createTable does not request content back from the service, but still returns an object containing the TableName.
|
||||
* Renamed queryEntity in TableService to retrieveEntity.
|
||||
* Modified how batches work. Instead of turning the service batch mode on/off, a batch is a separate entity like query. Batches are constructed and then executed.
|
||||
* queryEntities in TableService requires a continuation token.
|
||||
* TableService updateEntity, mergeEntity and deleteEntity do not take a checkEtag option. The entity's etag is sent if it exists, otherwise * is sent.
|
||||
* retrieveEntity and queryEntities accept empty partition and row keys.
|
||||
* Added entity-property-creation helper methods. Users can use the entityGenerator provided in TableUtilities and create entity properties as follows:
|
||||
var entGen = TableUtilities.entityGenerator;
|
||||
var entity = { PartitionKey: entGen.String('part2'),
|
||||
RowKey: entGen.String('row1'),
|
||||
boolValueTrue: entGen.Boolean(true),
|
||||
boolValueFalse: entGen.Boolean(false),
|
||||
intValue: entGen.Int32(42),
|
||||
dateValue: entGen.DateTime(new Date(Date.UTC(2011, 10, 25))),
|
||||
complexDateValue: entGen.DateTime(new Date(Date.UTC(2013, 02, 16, 01, 46, 20)))
|
||||
};
|
||||
* Removed TableQuery whereKeys method. Instead of tableQuery.whereKeys, tableService.retrieveEntity should be used.
|
||||
* Removed TableQuery whereNextKeys method. Instead of tableQuery.whereNextKeys, tableService.queryEntities takes currentToken which can be retrieved from a previous queryEntities call in results.
|
||||
* Removed TableQuery from method. Instead of tableQuery.from, tableService.queryEntities takes table and tableQuery may be null to represent retrieving all entities in the table.
|
||||
* TableQuery select is an instance method for consistency with top and where. Instead of TableQuery.select('foo'), use new TableQuery().select('foo').
|
||||
* TableQuery provides helper methods to create filter strings to use with the where clause for a query. Query strings may also include type specifiers where necessary.
|
||||
For example, to query on a long value, users could do the following:
|
||||
var tableQuery = new TableQuery().where(TableQuery.int64Filter('Int64Field', TableUtilities.QueryComparisons.EQUAL, '4294967296'));
|
||||
OR
|
||||
var tableQuery = new TableQuery().where('Int64Field == ?int64?', '4294967296');
|
||||
|
||||
2013.01.15 Version 0.8.0
|
||||
* Added the Preview Service Management libraries as separate modules
|
||||
* Added ability to consume PEM files directly from the Service Management libraries
|
||||
* Added support for createOrUpdate and createRegistrationId in the Notification Hubs libraries
|
||||
|
||||
2013.01.10 Version 0.7.19
|
||||
* Lock validator version
|
||||
|
||||
2013.11.27 Version 0.7.18
|
||||
* Lock xmlbuilder version
|
||||
|
||||
2013.11.5 Version 0.7.17
|
||||
* Added getBlob and createBlob operations that support stream piping
|
||||
* Added compute, management, network, serviceBus, sql, storage management, store and subscription preview wrappers
|
||||
* Multiple bugfixes
|
||||
|
||||
2013.10.16 Version 0.7.16
|
||||
* Improved API documentation
|
||||
* Updated Virtual Machines API to 2013-06-01
|
||||
* Added website management preview wrappers
|
||||
* Multiple bugfixes
|
||||
|
||||
2013.08.19 Version 0.7.15
|
||||
* Multiple storage fixes
|
||||
* Fix issue with Notification Hubs template message sending
|
||||
|
||||
2013.08.12 Version 0.7.14
|
||||
* Multiple storage fixes
|
||||
* Documentation improvements
|
||||
* Added support for large blobs upload / download
|
||||
|
||||
2013.08.08 Version 0.7.13
|
||||
* Lock request version
|
||||
|
||||
2013.07.29 Version 0.7.12
|
||||
* Added MPNS support
|
||||
* Added Service management vnet operations support
|
||||
|
||||
2013.07.10 Version 0.7.11
|
||||
* Hooked up new configuration system to storage APIs
|
||||
* Support for AZURE_STORAGE_CONNECTION_STRING environment variable
|
||||
* Included API for websites management
|
||||
* Fixed UTF-8 support in table batch submit
|
||||
|
||||
2013.06.26 Version 0.7.10
|
||||
* Various fixes in storage APIs
|
||||
|
||||
2013.06.19 Version 0.7.9
|
||||
* First part of new SDK configuration system
|
||||
* Support for AZURE_SERVICEBUS_CONNECTION_STRING environment variable
|
||||
* Updated SAS generation logic to include version number
|
||||
* Infrastructure support for creating passwordless VMs
|
||||
|
||||
2013.06.13 Version 0.7.8
|
||||
* Updates to HDInsight operations
|
||||
|
||||
2013.06.06 Version 0.7.7
|
||||
* Added support for Android notification through Service Bus Notification Hubs
|
||||
* Support prefixes when listing tables
|
||||
* Support '$logs' as a valid blob container name to support reading diagnostic information
|
||||
* Fix for network configuration serialization for subnets
|
||||
|
||||
2013.05.30 Version 0.7.6
|
||||
* Added list, delete and create cluster operations for HD Insight.
|
||||
|
||||
2013.05.15 Version 0.7.5
|
||||
* Fixed registration hubs issue with requiring access key when shared key was provided.
|
||||
* Fixed registration hubs issue with listByTag, Channel and device token
|
||||
|
||||
2013.05.09 Version 0.7.4
|
||||
* Fixed encoding issue with partition and row keys in table storage query
|
||||
|
||||
2013.05.01 Version 0.7.3
|
||||
* Fixed issue #680: BlobService.getBlobUrl puts permissions in SAS url even if not given
|
||||
* Changes to test suite & sdk to run in other environments
|
||||
* Notification hubs registrations
|
||||
* Support in ServiceManagementClient for role reboot and reimage
|
||||
|
||||
2013.04.05 Version 0.7.2
|
||||
* Removing workaround for SSL issue and forcing node version to be outside the > 0.8 && < 0.10.3 range where the issue occurs
|
||||
|
||||
2013.04.03 Version 0.7.1
|
||||
* Adding (limited) support for node 0.10
|
||||
* Fixing issue regarding registering providers when using websites or mobiles services
|
||||
|
||||
2013.03.25 Version 0.7.0
|
||||
* Breaking change: Primitive types will be stored for table storage.
|
||||
* Adding support for creating and deleting affinity groups
|
||||
* Replacing http-mock by nock and making all tests use it by default
|
||||
* Adding notification hubs messages for WNS and APNS
|
||||
* Add Strict SSL validation for server certificates
|
||||
* Add support for creating subscriptions that expire
|
||||
|
||||
2013.03.12 Version 0.6.11
|
||||
* Added constraint to package.json to restrict to node versions < 0.9.
|
||||
|
||||
2013.02.11 Version 0.6.10
|
||||
* Added helper date.* functions for generating SAS expirations (secondsFromNow, minutesFromNow, hoursFromNow, daysFromNow)
|
||||
* Added SQL classes for managing SQL Servers, Databases and Firewall rules
|
||||
* Updating to use latest xml2js
|
||||
|
||||
2012.12.12 Version 0.6.9
|
||||
* Exporting WebResource, Client classes from package to support CLI.
|
||||
* Install message updated to remind users the CLI is now a separate package.
|
||||
|
||||
2012.11.20 Version 0.6.8
|
||||
* CLI functionality has been pulled out into a new "azure-cli" module. See https://github.com/WindowsAzure/azure-sdk-tools-xplat for details.
|
||||
* Add support for sb: in ServiceBus connection strings.
|
||||
* Add functions to ServiceManagement for managing storage accounts.
|
||||
* Merged #314 from @smarx for allowing empty partition keys on the client.
|
||||
* Merged #447 from @anodejs for array enumeration and exception on batch response.
|
||||
* Various other fixes
|
||||
|
||||
2012.10.15 Version 0.6.7
|
||||
* Adding connection strings support for storage and service bus
|
||||
* Fixing issue with EMULATED and explicit variables making the later more relevant
|
||||
* Adding Github support
|
||||
* Adding website application settings support
|
||||
|
||||
2012.10.12 Version 0.6.6
|
||||
* Using fixed version of commander.js to avoid bug in commander.js 1.0.5
|
||||
|
||||
2012.10.01 Version 0.6.5
|
||||
* Bugfixing
|
||||
|
||||
2012.09.18 Version 0.6.4
|
||||
* Multiple Bugfixes around blob streaming
|
||||
|
||||
2012.09.09 Version 0.6.3
|
||||
* Fixing issue with xml2js
|
||||
|
||||
2012.08.15 Version 0.6.2
|
||||
* Multiple Bugfixes
|
||||
|
||||
2012.07.02 Version 0.6.1
|
||||
* Multiple Bugfixes
|
||||
* Adding subscription setting and listing functionality.
|
||||
|
||||
2012.06.06 Version 0.6.0
|
||||
* Adding CLI tool
|
||||
* Multiple Bugfixes
|
||||
|
||||
2012.04.19 Version 0.5.3
|
||||
* Service Runtime Wrappers
|
||||
* Multiple Bugfixes
|
||||
* Unit tests converted to mocha and code coverage made easy through JSCoverage
|
||||
|
||||
2012.02.10 Version 0.5.2
|
||||
* Service Bus Wrappers
|
||||
* Storage Services UT run against a mock server.
|
||||
* Node.exe version requirement lowered to raise compatibility.
|
||||
* Multiple Bugfixes
|
||||
|
||||
2011.12.14 Version 0.5.1
|
||||
* Multiple bug fixes
|
||||
|
||||
2011.12.09 Version 0.5.0
|
||||
* Initial Release
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
+535
@@ -0,0 +1,535 @@
|
||||
# Microsoft Azure Storage SDK for Node.js
|
||||
|
||||
[](http://badge.fury.io/js/azure-storage) []( https://azurestorageslack.azurewebsites.net)
|
||||
|
||||
* Master [](https://travis-ci.org/Azure/azure-storage-node/branches) [](https://coveralls.io/github/Azure/azure-storage-node?branch=master)
|
||||
* Dev [](https://travis-ci.org/Azure/azure-storage-node/branches) [](https://coveralls.io/github/Azure/azure-storage-node?branch=dev)
|
||||
|
||||
This project provides a Node.js package and a browser compatible [JavaScript Client Library](#azure-storage-javascript-client-library-for-browsers) that makes it easy to consume and manage Microsoft Azure Storage Services.
|
||||
|
||||
> If you are looking for the Node.js SDK for other Azure services, visit [https://github.com/Azure/azure-sdk-for-node](https://github.com/Azure/azure-sdk-for-node).
|
||||
|
||||
# Features
|
||||
|
||||
- Tables
|
||||
- Create/Delete Tables
|
||||
- Query/Create/Read/Update/Delete Entities
|
||||
- Blobs
|
||||
- Create/Delete Containers
|
||||
- Create/Read/Update/Delete Blobs
|
||||
- Files
|
||||
- Create/Delete Shares
|
||||
- Create/Delete Directories
|
||||
- Create/Read/Update/Delete Files
|
||||
- Queues
|
||||
- Create/Delete Queues
|
||||
- Insert/Peek Queue Messages
|
||||
- Advanced Queue Operations
|
||||
- Service Properties
|
||||
- Get Service Properties
|
||||
- Set Service Properties
|
||||
|
||||
Please check details on [API reference documents](http://azure.github.io/azure-storage-node).
|
||||
|
||||
# Getting Started
|
||||
|
||||
## Install
|
||||
|
||||
```shell
|
||||
npm install azure-storage
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
```
|
||||
|
||||
When using the Storage SDK, you must provide connection information for the storage account to use. This can be provided using:
|
||||
|
||||
* Environment variables - **AZURE_STORAGE_ACCOUNT** and **AZURE_STORAGE_ACCESS_KEY**, or **AZURE_STORAGE_CONNECTION_STRING**.
|
||||
|
||||
* Constructors - For example, `var tableSvc = azure.createTableService(accountName, accountKey);`
|
||||
|
||||
### Table Storage
|
||||
|
||||
To ensure a table exists, call **createTableIfNotExists**:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var tableService = azure.createTableService();
|
||||
tableService.createTableIfNotExists('mytable', function(error, result, response) {
|
||||
if (!error) {
|
||||
// result contains true if created; false if already exists
|
||||
}
|
||||
});
|
||||
```
|
||||
A new entity can be added by calling **insertEntity**:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var tableService = azure.createTableService();
|
||||
var entGen = azure.TableUtilities.entityGenerator;
|
||||
var entity = {
|
||||
PartitionKey: entGen.String('part2'),
|
||||
RowKey: entGen.String('row1'),
|
||||
boolValueTrue: entGen.Boolean(true),
|
||||
boolValueFalse: entGen.Boolean(false),
|
||||
intValue: entGen.Int32(42),
|
||||
dateValue: entGen.DateTime(new Date(Date.UTC(2011, 10, 25))),
|
||||
complexDateValue: entGen.DateTime(new Date(Date.UTC(2013, 02, 16, 01, 46, 20)))
|
||||
};
|
||||
tableService.insertEntity('mytable', entity, function(error, result, response) {
|
||||
if (!error) {
|
||||
// result contains the ETag for the new entity
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Instead of creating entities manually, you can use **entityGenerator**:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var entGen = azure.TableUtilities.entityGenerator;
|
||||
var task = {
|
||||
PartitionKey: entGen.String('hometasks'),
|
||||
RowKey: entGen.String('1'),
|
||||
description: entGen.String('take out the trash'),
|
||||
dueDate: entGen.DateTime(new Date(Date.UTC(2015, 6, 20)))
|
||||
};
|
||||
```
|
||||
|
||||
The method **retrieveEntity** can then be used to fetch the entity that was just inserted:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var tableService = azure.createTableService();
|
||||
tableService.retrieveEntity('mytable', 'part2', 'row1', function(error, result, response) {
|
||||
if (!error) {
|
||||
// result contains the entity
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Use **TableQuery** to build complex queries:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var tableService = azure.createTableService();
|
||||
var query = new azure.TableQuery()
|
||||
.top(5)
|
||||
.where('PartitionKey eq ?', 'part2');
|
||||
|
||||
tableService.queryEntities('mytable', query, null, function(error, result, response) {
|
||||
if (!error) {
|
||||
// result.entries contains entities matching the query
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Blob Storage
|
||||
|
||||
The **createContainerIfNotExists** method can be used to create a
|
||||
container in which to store a blob:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
blobService.createContainerIfNotExists('taskcontainer', {
|
||||
publicAccessLevel: 'blob'
|
||||
}, function(error, result, response) {
|
||||
if (!error) {
|
||||
// if result = true, container was created.
|
||||
// if result = false, container already existed.
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method **createBlockBlobFromLocalFile** can be used.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
blobService.createBlockBlobFromLocalFile('mycontainer', 'taskblob', 'task1-upload.txt', function(error, result, response) {
|
||||
if (!error) {
|
||||
// file uploaded
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
For page blobs, use **createPageBlobFromLocalFile**. There are other methods for uploading blobs also, such as **createBlockBlobFromText** or **createPageBlobFromStream**.
|
||||
|
||||
There are also several ways to download block and page blobs. For example, **getBlobToStream** downloads the blob to a stream:
|
||||
|
||||
```Javascript
|
||||
var blobService = azure.createBlobService();
|
||||
var fs = require('fs');
|
||||
blobService.getBlobToStream('mycontainer', 'taskblob', fs.createWriteStream('output.txt'), function(error, result, response) {
|
||||
if (!error) {
|
||||
// blob retrieved
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To create a Shared Access Signature (SAS), use the **generateSharedAccessSignature** method. Additionally you can use the **date** helper functions to easily create a SAS that expires at some point relative to the current time.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
var startDate = new Date();
|
||||
var expiryDate = new Date(startDate);
|
||||
expiryDate.setMinutes(startDate.getMinutes() + 100);
|
||||
startDate.setMinutes(startDate.getMinutes() - 100);
|
||||
|
||||
var sharedAccessPolicy = {
|
||||
AccessPolicy: {
|
||||
Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
|
||||
Start: startDate,
|
||||
Expiry: expiryDate
|
||||
}
|
||||
};
|
||||
|
||||
var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
|
||||
var sasUrl = blobService.getUrl(containerName, blobName, token);
|
||||
```
|
||||
|
||||
### Queue Storage
|
||||
|
||||
The **createQueueIfNotExists** method can be used to ensure a queue exists:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var queueService = azure.createQueueService();
|
||||
queueService.createQueueIfNotExists('taskqueue', function(error) {
|
||||
if (!error) {
|
||||
// Queue exists
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The **createMessage** method can then be called to insert the message into the queue:
|
||||
|
||||
```Javascript
|
||||
var queueService = azure.createQueueService();
|
||||
queueService.createMessage('taskqueue', 'Hello world!', function(error) {
|
||||
if (!error) {
|
||||
// Message inserted
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
It is then possible to call the **getMessage** method, process the message and then call **deleteMessage** inside the callback. This two-step process ensures messages don't get lost when they are removed from the queue.
|
||||
|
||||
```Javascript
|
||||
var queueService = azure.createQueueService(),
|
||||
queueName = 'taskqueue';
|
||||
queueService.getMessages(queueName, function(error, serverMessages) {
|
||||
if (!error) {
|
||||
// Process the message in less than 30 seconds, the message
|
||||
// text is available in serverMessages[0].messageText
|
||||
|
||||
queueService.deleteMessage(queueName, serverMessages[0].messageId, serverMessages[0].popReceipt, function(error) {
|
||||
if (!error) {
|
||||
// Message deleted
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### File Storage
|
||||
|
||||
The **createShareIfNotExists** method can be used to create a
|
||||
share in which to store a file or a directory of files:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var fileService = azure.createFileService();
|
||||
fileService.createShareIfNotExists('taskshare', function(error, result, response) {
|
||||
if (!error) {
|
||||
// if result = true, share was created.
|
||||
// if result = false, share already existed.
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To create a directory, the method **createDirectoryIfNotExists** can be used.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var fileService = azure.createFileService();
|
||||
|
||||
fileService.createDirectoryIfNotExists('taskshare', 'taskdirectory', function(error, result, response) {
|
||||
if (!error) {
|
||||
// if result = true, share was created.
|
||||
// if result = false, share already existed.
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method **createFileFromLocalFile** can be used.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var fileService = azure.createFileService();
|
||||
|
||||
fileService.createFileFromLocalFile('taskshare', 'taskdirectory', 'taskfile', 'task1-upload.txt', function(error, result, response) {
|
||||
if (!error) {
|
||||
// file uploaded
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To upload a file from a stream, the method **createFileFromStream** can be used. The var `myFileBuffer` in the script below is a native Node Buffer, or ArrayBuffer object if within a browser environment.
|
||||
|
||||
```Javascript
|
||||
var stream = require('stream');
|
||||
var azure = require('azure-storage');
|
||||
var fileService = azure.createFileService();
|
||||
|
||||
var fileStream = new stream.Readable();
|
||||
fileStream.push(myFileBuffer);
|
||||
fileStream.push(null);
|
||||
|
||||
fileService.createFileFromStream('taskshare', 'taskdirectory', 'taskfile', fileStream, myFileBuffer.length, function(error, result, response) {
|
||||
if (!error) {
|
||||
// file uploaded
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
To create a file from a text string, the method **createFileFromText** can be used. A Node Buffer or ArrayBuffer object containing the text can also be supplied.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var fileService = azure.createFileService();
|
||||
|
||||
var text = 'Hello World!';
|
||||
|
||||
fileService.createFileFromText('taskshare', 'taskdirectory', 'taskfile', text, function(error, result, response) {
|
||||
if (!error) {
|
||||
// file created
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
There are also several ways to download files. For example, **getFileToStream** downloads the file to a stream:
|
||||
|
||||
```Javascript
|
||||
var fileService = azure.createFileService();
|
||||
var fs = require('fs');
|
||||
fileService.getFileToStream('taskshare', 'taskdirectory', 'taskfile', fs.createWriteStream('output.txt'), function(error, result, response) {
|
||||
if (!error) {
|
||||
// file retrieved
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Service Properties
|
||||
|
||||
The **getServiceProperties** method can be used to fetch the logging, metrics and CORS settings on your storage account:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
blobService.getServiceProperties(function(error, result, response) {
|
||||
if (!error) {
|
||||
var serviceProperties = result;
|
||||
// properties are fetched
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The **setServiceProperties** method can be used to modify the logging, metrics and CORS settings on your storage account:
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
var serviceProperties = generateServiceProperties();
|
||||
|
||||
blobService.setServiceProperties(serviceProperties, function(error, result, response) {
|
||||
if (!error) {
|
||||
// properties are set
|
||||
}
|
||||
});
|
||||
|
||||
function generateServiceProperties() {
|
||||
return serviceProperties = {
|
||||
Logging: {
|
||||
Version: '1.0',
|
||||
Delete: true,
|
||||
Read: true,
|
||||
Write: true,
|
||||
RetentionPolicy: {
|
||||
Enabled: true,
|
||||
Days: 10,
|
||||
},
|
||||
},
|
||||
HourMetrics: {
|
||||
Version: '1.0',
|
||||
Enabled: true,
|
||||
IncludeAPIs: true,
|
||||
RetentionPolicy: {
|
||||
Enabled: true,
|
||||
Days: 10,
|
||||
},
|
||||
},
|
||||
MinuteMetrics: {
|
||||
Version: '1.0',
|
||||
Enabled: true,
|
||||
IncludeAPIs: true,
|
||||
RetentionPolicy: {
|
||||
Enabled: true,
|
||||
Days: 10,
|
||||
},
|
||||
},
|
||||
Cors: {
|
||||
CorsRule: [
|
||||
{
|
||||
AllowedOrigins: ['www.azure.com', 'www.microsoft.com'],
|
||||
AllowedMethods: ['GET', 'PUT'],
|
||||
AllowedHeaders: ['x-ms-meta-data*', 'x-ms-meta-target*', 'x-ms-meta-xyz', 'x-ms-meta-foo'],
|
||||
ExposedHeaders: ['x-ms-meta-data*', 'x-ms-meta-source*', 'x-ms-meta-abc', 'x-ms-meta-bcd'],
|
||||
MaxAgeInSeconds: 500,
|
||||
},
|
||||
{
|
||||
AllowedOrigins: ['www.msdn.com', 'www.asp.com'],
|
||||
AllowedMethods: ['GET', 'PUT'],
|
||||
AllowedHeaders: ['x-ms-meta-data*', 'x-ms-meta-target*', 'x-ms-meta-xyz', 'x-ms-meta-foo'],
|
||||
ExposedHeaders: ['x-ms-meta-data*', 'x-ms-meta-source*', 'x-ms-meta-abc', 'x-ms-meta-bcd'],
|
||||
MaxAgeInSeconds: 500,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
When modifying the service properties, you can fetch the properties and then modify the them to prevent overwriting the existing settings.
|
||||
|
||||
```Javascript
|
||||
var azure = require('azure-storage');
|
||||
var blobService = azure.createBlobService();
|
||||
|
||||
blobService.getServiceProperties(function(error, result, response) {
|
||||
if (!error) {
|
||||
var serviceProperties = result;
|
||||
|
||||
// modify the properties
|
||||
|
||||
blobService.setServiceProperties(serviceProperties, function(error, result, response) {
|
||||
if (!error) {
|
||||
// properties are set
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Retry Policies
|
||||
|
||||
By default, no retry will be performed with service instances newly created by Azure storage client library for Node.js.
|
||||
Two pre-written retry polices [ExponentialRetryPolicyFilter](http://azure.github.io/azure-storage-node/ExponentialRetryPolicyFilter.html) and [LinearRetryPolicyFilter](http://azure.github.io/azure-storage-node/LinearRetryPolicyFilter.html) are available with modifiable settings, and can be used through associating filter.
|
||||
Any custom retry logic may be used by customizing RetryPolicyFilter instance.
|
||||
|
||||
For how to use pre-writtern retry policies and how to define customized retry policy, please refer to **retrypolicysample** in samples directory.
|
||||
|
||||
## Code Samples
|
||||
|
||||
How-Tos focused around accomplishing specific tasks are available on the [Microsoft Azure Node.js Developer Center](http://azure.microsoft.com/en-us/develop/nodejs/).
|
||||
|
||||
* [How to use the Blob Service from Node.js](http://azure.microsoft.com/en-us/documentation/articles/storage-nodejs-how-to-use-blob-storage/)
|
||||
|
||||
* [How to use the Table Service from Node.js](http://azure.microsoft.com/en-us/documentation/articles/storage-nodejs-how-to-use-table-storage/)
|
||||
|
||||
* [How to use the Queue Service from Node.js](http://azure.microsoft.com/en-us/documentation/articles/storage-nodejs-how-to-use-queues/)
|
||||
|
||||
# Running Tests
|
||||
|
||||
Unit tests can then be run from the module's root directory using:
|
||||
|
||||
```shell
|
||||
npm test
|
||||
```
|
||||
|
||||
Running test is also supported by Grunt by:
|
||||
|
||||
```shell
|
||||
grunt # mochaTest as the default task
|
||||
```
|
||||
|
||||
By default the unit tests are ran with Nock recording data. To run tests against real storage account, please set environment variable to turn off Nock by:
|
||||
|
||||
```
|
||||
set NOCK_OFF=true
|
||||
```
|
||||
|
||||
and set up the following environment variable for storage account credentials by
|
||||
|
||||
```Batchfile
|
||||
set AZURE_STORAGE_CONNECTION_STRING="valid storage connection string"
|
||||
```
|
||||
|
||||
To record the data in a test pass against real storage account for future Nock usage:
|
||||
|
||||
```Batchfile
|
||||
set AZURE_NOCK_RECORD=true
|
||||
```
|
||||
|
||||
In order to be able to use a proxy like fiddler, an additional environment variable should be set up:
|
||||
|
||||
```Batchfile
|
||||
set NODE_TLS_REJECT_UNAUTHORIZED=0
|
||||
set HTTP_PROXY=http://127.0.0.1:8888
|
||||
```
|
||||
|
||||
On Linux, please use `export` other than `set` to set the variables.
|
||||
|
||||
# Azure Storage JavaScript Client Library for Browsers
|
||||
|
||||
* JavaScript Client Library is currently in Preview. Questions and feedback are welcome in our [Slack channel](https://azurestorageslack.azurewebsites.net).
|
||||
|
||||
Azure Storage Node.js Client Library is compatible with [Browserify](http://browserify.org/). This means you can bundle your Node.js application which depends on the Node.js Client Library using Browserify.
|
||||
|
||||
You can also choose to download the JavaScript Client Library provided by us, or generate the library by yourself. Please refer to the [README.md](browser/README.md) under `browser` folder for detailed usage guidelines.
|
||||
|
||||
## Downloading Azure Storage JavaScript Client Library
|
||||
|
||||
It's recommended to use the Azure Storage JavaScript Client Library provided by us. Please [download the latest library](https://aka.ms/downloadazurestoragejs).
|
||||
|
||||
## Generating Azure Storage JavaScript Client Library
|
||||
|
||||
We also provide browserify bundle scripts which generate Azure Storage JavaScript Client Library. The bundle script reduces the size of the Storage Client Library by splitting into smaller files, one per storage service and a common shared file. For more detailed information, refer to [README.md](browser/README.md) under `browser` folder.
|
||||
|
||||
# JsDoc
|
||||
|
||||
JsDoc can be generated by `grunt jsdoc`.
|
||||
|
||||
To load the docs by devserver after generation, run `grunt doc` and then browse the docs at [http://localhost:8888](http://localhost:8888).
|
||||
|
||||
# Need Help?
|
||||
|
||||
Be sure to check out the Microsoft Azure [Developer Forums on MSDN](http://go.microsoft.com/fwlink/?LinkId=234489) if you have trouble with the provided code or use StackOverflow.
|
||||
|
||||
# Learn More
|
||||
|
||||
- [Microsoft Azure Node.js Developer Center](http://azure.microsoft.com/en-us/develop/nodejs/)
|
||||
- [Azure Storage Team Blog](http://blogs.msdn.com/b/windowsazurestorage/)
|
||||
|
||||
# Contribute
|
||||
|
||||
We gladly accept community contributions.
|
||||
|
||||
- Issues: Please report bugs using the Issues section of GitHub
|
||||
- Forums: Interact with the development teams on StackOverflow or the Microsoft Azure Forums
|
||||
- Source Code Contributions: If you would like to become an active contributor to this project please follow the instructions provided in [Contributing.md](CONTRIBUTING.md).
|
||||
|
||||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
|
||||
|
||||
For general suggestions about Microsoft Azure please use our [UserVoice forum](http://feedback.azure.com/forums/34192--general-feedback).
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
Note: This is the change log file for Azure Storage JavaScript Client Library.
|
||||
|
||||
2018.02 Version 0.2.8-preview.14
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.8.0.
|
||||
|
||||
2017.12 Version 0.2.7-preview.13
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.7.0.
|
||||
|
||||
2017.10 Version 0.2.6-preview.12
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.6.0.
|
||||
|
||||
2017.09 Version 0.2.5-preview.11
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.5.0.
|
||||
|
||||
2017.08 Version 0.2.4-preview.10
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.4.0.
|
||||
|
||||
2017.08 Version 0.2.3-preview.9
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.3.0.
|
||||
|
||||
2017.08 Version 0.2.2-preview.8
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.2.2.
|
||||
|
||||
2017.07 Version 0.2.2-preview.7
|
||||
|
||||
* Added browser specific APIs for blobs and files uploading.
|
||||
* `BlobService.createBlockBlobFromBrowserFile`
|
||||
* `BlobService.createPageBlobFromBrowserFile`
|
||||
* `BlobService.createAppendBlobFromBrowserFile`
|
||||
* `BlobService.appendFromBrowserFile`
|
||||
* `FileService.createFileFromBrowserFile`
|
||||
* Updated samples with above new added APIs.
|
||||
* Dropped dependency to browserify-fs.
|
||||
|
||||
2017.07 Version 0.2.2-preview.6
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.2.1.
|
||||
|
||||
2017.06 Version 0.2.2-preview.5
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.2.0.
|
||||
|
||||
2017.05 Version 0.2.1-preview.4
|
||||
|
||||
* Reduced footprint of the generated JavaScript files.
|
||||
* Removed 7 local-file related APIs which are limited by browser's sandbox.
|
||||
|
||||
2017.03 Version 0.2.1-preview.3
|
||||
|
||||
* Fixed missing 100% upload progress issue in blob sample for uploading blobs smaller than 32MB.
|
||||
* Added speedSummary code example in the blob & file samples.
|
||||
|
||||
2017.03 Version 0.2.1-preview.2
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.1.0.
|
||||
|
||||
2017.03 Version 0.2.0-preview.1
|
||||
|
||||
* Generated browser compatible JavaScript files based on Microsoft Azure Storage SDK for Node.js 2.0.0.
|
||||
* Added bundle scripts to generate Azure Storage JavaScript Client Library.
|
||||
* Added npm command `npm run genjs` to generate JavaScript Client Library.
|
||||
* Added samples for Azure Storage JavaScript Client Library.
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
# Azure Storage JavaScript Client Library for Browsers
|
||||
|
||||
* Join the community discussion on Slack! []( https://azurestorageslack.azurewebsites.net)
|
||||
|
||||
There are 5 generated JavaScript files for Azure Storage JavaScript Client Library:
|
||||
- `azure-storage.common.js` contains the common part for other 4 JavaScript files.
|
||||
- `azure-storage.table.js` contains the Azure Storage table service operation logic, which depends on azure-storage.common.js
|
||||
- `azure-storage.blob.js` contains the Azure Storage blob service operation logic, which depends on azure-storage.common.js
|
||||
- `azure-storage.queue.js` contains the Azure Storage queue service operation logic, which depends on azure-storage.common.js
|
||||
- `azure-storage.file.js` contains the Azure Storage file service operation logic, which depends on azure-storage.common.js
|
||||
|
||||
We also provide samples to guide you quickly start with the Azure Storage JavaScript Client Library. In the [JavaScript Client Library zip file](https://aka.ms/downloadazurestoragejs) or [azure-storage-node/browser/samples](samples), you will find 4 HTML samples:
|
||||
- `sample-table.html` demonstrates how to operate with Azure Storage table service in the browser
|
||||
- `sample-blob.html` demonstrates how to operate with Azure Storage blob service in the browser
|
||||
- `sample-queue.html` demonstrates how to operate with Azure Storage queue service in the browser
|
||||
- `sample-file.html` demonstrates how to operate with Azure Storage file service in the browser
|
||||
|
||||
After generating the JavaScript Client Library, you can try the samples in browsers such as Chrome/Edge/Firefox directly.
|
||||
|
||||
**Note**: An HTTP server should be set to host the samples for IE browser.
|
||||
|
||||
## Limitations
|
||||
|
||||
The Azure Storage JavaScript Client Library is currently in preview stage, there are some known issues or limitations as follows.
|
||||
|
||||
### Compatibility
|
||||
|
||||
Compatibility with mobile browsers have not been fully validated, please open issues when you get errors. Current validated browsers are as below:
|
||||
|
||||
| Chrome | Firefox | Internet Explorer | Microsoft Edge |
|
||||
|------------|----------|--------------------|-----------------|
|
||||
| v64 | v58 | v11 | v41 |
|
||||
|
||||
## Generating a Custom Azure Storage JavaScript Client Library
|
||||
|
||||
If you wish to customize the library and generate the Azure Storage JavaScript Client Library, you can follow the following steps.
|
||||
|
||||
We provide browserify bundle scripts which generate Azure Storage JavaScript Client Library. The bundle script reduces the size of the Storage Client Library by splitting into smaller files, one per storage service and a common shared file.
|
||||
|
||||
The generated JavaScript Client Library includes 5 separated JavaScript files:
|
||||
- `azure-storage.common.js`
|
||||
- `azure-storage.table.js`
|
||||
- `azure-storage.blob.js`
|
||||
- `azure-storage.queue.js`
|
||||
- `azure-storage.file.js`
|
||||
|
||||
Let's get started to generate the Azure Storage JavaScript Client Library!
|
||||
|
||||
### Step 1: Cloning Repo
|
||||
|
||||
Azure Storage JavaScript Client Library is generated from Azure Storage SDK for Node.js. Clone `azure-storage-node` repo with following command:
|
||||
|
||||
```Batchfile
|
||||
git clone https://github.com/Azure/azure-storage-node.git
|
||||
```
|
||||
|
||||
### Step 2: Installing Node.js Modules
|
||||
|
||||
Change to the root directory of the cloned repo:
|
||||
|
||||
```Batchfile
|
||||
cd azure-storage-node
|
||||
```
|
||||
|
||||
Install the dependent Node.js modules:
|
||||
|
||||
```Batchfile
|
||||
npm install
|
||||
```
|
||||
|
||||
### Step 3: Generating JavaScript Client Library with Bundle Scripts
|
||||
|
||||
We provide bundle scripts to help quickly generate the JavaScript Client Library. At the root directory of the cloned repo:
|
||||
|
||||
```Batchfile
|
||||
npm run genjs
|
||||
```
|
||||
|
||||
### Step 4: Finding the Generated JavaScript Files
|
||||
|
||||
If everything goes well, the generated JavaScript files should be saved to `azure-storage-node/browser/bundle`. There will be 5 generated JavaScript files totally:
|
||||
- `azure-storage.common.js`
|
||||
- `azure-storage.table.js`
|
||||
- `azure-storage.blob.js`
|
||||
- `azure-storage.queue.js`
|
||||
- `azure-storage.file.js`
|
||||
|
||||
### Step 5: JavaScript Files Minify
|
||||
|
||||
You are able to minify the generated JavaScript files with your favorite minify tools. Here we show the minify process with Node.js minify tool [uglifyJS](https://github.com/mishoo/UglifyJS2).
|
||||
|
||||
Install uglifyJS:
|
||||
|
||||
```Batchfile
|
||||
npm install -g uglify-js
|
||||
```
|
||||
|
||||
Minify the JavaScript files:
|
||||
|
||||
```Batchfile
|
||||
uglifyjs --compress --mangle -- azure-storage.common.js > azure-storage.common.min.js
|
||||
uglifyjs --compress --mangle -- azure-storage.table.js > azure-storage.table.min.js
|
||||
uglifyjs --compress --mangle -- azure-storage.blob.js > azure-storage.blob.min.js
|
||||
uglifyjs --compress --mangle -- azure-storage.queue.js > azure-storage.queue.min.js
|
||||
uglifyjs --compress --mangle -- azure-storage.file.js > azure-storage.file.min.js
|
||||
```
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var AzureStorage = window.AzureStorage || {};
|
||||
|
||||
AzureStorage.generateDevelopmentStorageCredentials = function (proxyUri) {
|
||||
var devStore = 'UseDevelopmentStorage=true;';
|
||||
if(proxyUri){
|
||||
devStore += 'DevelopmentStorageProxyUri=' + proxyUri;
|
||||
}
|
||||
|
||||
return devStore;
|
||||
};
|
||||
|
||||
var BlobService = require('../lib/services/blob/blobservice.browser');
|
||||
|
||||
AzureStorage.BlobService = BlobService;
|
||||
AzureStorage.BlobUtilities = require('../lib/services/blob/blobutilities');
|
||||
|
||||
AzureStorage.createBlobService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new BlobService(storageAccountOrConnectionString, storageAccessKey, host, null);
|
||||
};
|
||||
|
||||
AzureStorage.createBlobServiceWithSas = function (host, sasToken) {
|
||||
return new BlobService(null, null, host, sasToken);
|
||||
};
|
||||
|
||||
AzureStorage.createBlobServiceAnonymous = function (host) {
|
||||
return new BlobService(null, null, host, null);
|
||||
};
|
||||
|
||||
var azureCommon = require('../lib/common/common.browser');
|
||||
var StorageServiceClient = azureCommon.StorageServiceClient;
|
||||
var SharedKey = azureCommon.SharedKey;
|
||||
|
||||
AzureStorage.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy)
|
||||
{
|
||||
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey);
|
||||
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key);
|
||||
|
||||
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy);
|
||||
};
|
||||
|
||||
AzureStorage.Constants = azureCommon.Constants;
|
||||
AzureStorage.StorageUtilities = azureCommon.StorageUtilities;
|
||||
AzureStorage.AccessCondition = azureCommon.AccessCondition;
|
||||
|
||||
AzureStorage.SR = azureCommon.SR;
|
||||
AzureStorage.StorageServiceClient = StorageServiceClient;
|
||||
AzureStorage.Logger = azureCommon.Logger;
|
||||
AzureStorage.WebResource = azureCommon.WebResource;
|
||||
AzureStorage.Validate = azureCommon.validate;
|
||||
AzureStorage.date = azureCommon.date;
|
||||
|
||||
// Other filters
|
||||
AzureStorage.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter;
|
||||
AzureStorage.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter;
|
||||
AzureStorage.RetryPolicyFilter = azureCommon.RetryPolicyFilter;
|
||||
|
||||
window.AzureStorage = AzureStorage;
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var AzureStorage = window.AzureStorage || {};
|
||||
|
||||
AzureStorage.generateDevelopmentStorageCredentials = function (proxyUri) {
|
||||
var devStore = 'UseDevelopmentStorage=true;';
|
||||
if(proxyUri){
|
||||
devStore += 'DevelopmentStorageProxyUri=' + proxyUri;
|
||||
}
|
||||
|
||||
return devStore;
|
||||
};
|
||||
|
||||
var FileService = require('../lib/services/file/fileservice.browser');
|
||||
|
||||
AzureStorage.FileService = FileService;
|
||||
AzureStorage.FileUtilities = require('../lib/services/file/fileutilities');
|
||||
|
||||
AzureStorage.createFileService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new FileService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
AzureStorage.createFileServiceWithSas = function (hostUri, sasToken) {
|
||||
return new FileService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
var azureCommon = require('../lib/common/common.browser');
|
||||
var StorageServiceClient = azureCommon.StorageServiceClient;
|
||||
var SharedKey = azureCommon.SharedKey;
|
||||
|
||||
AzureStorage.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy)
|
||||
{
|
||||
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey);
|
||||
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key);
|
||||
|
||||
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy);
|
||||
};
|
||||
|
||||
AzureStorage.Constants = azureCommon.Constants;
|
||||
AzureStorage.StorageUtilities = azureCommon.StorageUtilities;
|
||||
AzureStorage.AccessCondition = azureCommon.AccessCondition;
|
||||
|
||||
AzureStorage.SR = azureCommon.SR;
|
||||
AzureStorage.StorageServiceClient = StorageServiceClient;
|
||||
AzureStorage.Logger = azureCommon.Logger;
|
||||
AzureStorage.WebResource = azureCommon.WebResource;
|
||||
AzureStorage.Validate = azureCommon.validate;
|
||||
AzureStorage.date = azureCommon.date;
|
||||
|
||||
// Other filters
|
||||
AzureStorage.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter;
|
||||
AzureStorage.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter;
|
||||
AzureStorage.RetryPolicyFilter = azureCommon.RetryPolicyFilter;
|
||||
|
||||
window.AzureStorage = AzureStorage;
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var AzureStorage = window.AzureStorage || {};
|
||||
|
||||
AzureStorage.generateDevelopmentStorageCredentials = function (proxyUri) {
|
||||
var devStore = 'UseDevelopmentStorage=true;';
|
||||
if(proxyUri){
|
||||
devStore += 'DevelopmentStorageProxyUri=' + proxyUri;
|
||||
}
|
||||
|
||||
return devStore;
|
||||
};
|
||||
|
||||
var QueueService = require('../lib/services/queue/queueservice');
|
||||
|
||||
AzureStorage.QueueService = QueueService;
|
||||
AzureStorage.QueueUtilities = require('../lib/services/queue/queueutilities');
|
||||
AzureStorage.QueueMessageEncoder = require('../lib/services/queue/queuemessageencoder');
|
||||
|
||||
AzureStorage.createQueueService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new QueueService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
AzureStorage.createQueueServiceWithSas = function(hostUri, sasToken) {
|
||||
return new QueueService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
var azureCommon = require('../lib/common/common.browser');
|
||||
var StorageServiceClient = azureCommon.StorageServiceClient;
|
||||
var SharedKey = azureCommon.SharedKey;
|
||||
|
||||
AzureStorage.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy)
|
||||
{
|
||||
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey);
|
||||
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key);
|
||||
|
||||
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy);
|
||||
};
|
||||
|
||||
AzureStorage.Constants = azureCommon.Constants;
|
||||
AzureStorage.StorageUtilities = azureCommon.StorageUtilities;
|
||||
AzureStorage.AccessCondition = azureCommon.AccessCondition;
|
||||
|
||||
AzureStorage.SR = azureCommon.SR;
|
||||
AzureStorage.StorageServiceClient = StorageServiceClient;
|
||||
AzureStorage.Logger = azureCommon.Logger;
|
||||
AzureStorage.WebResource = azureCommon.WebResource;
|
||||
AzureStorage.Validate = azureCommon.validate;
|
||||
AzureStorage.date = azureCommon.date;
|
||||
|
||||
// Other filters
|
||||
AzureStorage.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter;
|
||||
AzureStorage.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter;
|
||||
AzureStorage.RetryPolicyFilter = azureCommon.RetryPolicyFilter;
|
||||
|
||||
window.AzureStorage = AzureStorage;
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var AzureStorage = window.AzureStorage || {};
|
||||
|
||||
AzureStorage.generateDevelopmentStorageCredentials = function (proxyUri) {
|
||||
var devStore = 'UseDevelopmentStorage=true;';
|
||||
if(proxyUri){
|
||||
devStore += 'DevelopmentStorageProxyUri=' + proxyUri;
|
||||
}
|
||||
|
||||
return devStore;
|
||||
};
|
||||
|
||||
var TableService = require('../lib/services/table/tableservice');
|
||||
AzureStorage.TableService = TableService;
|
||||
AzureStorage.TableQuery = require('../lib/services/table/tablequery');
|
||||
AzureStorage.TableBatch = require('../lib/services/table/tablebatch');
|
||||
AzureStorage.TableUtilities = require('../lib/services/table/tableutilities');
|
||||
|
||||
AzureStorage.createTableService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new TableService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
AzureStorage.createTableServiceWithSas = function (hostUri, sasToken) {
|
||||
return new TableService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
var azureCommon = require('../lib/common/common.browser');
|
||||
var StorageServiceClient = azureCommon.StorageServiceClient;
|
||||
var SharedKey = azureCommon.SharedKey;
|
||||
|
||||
AzureStorage.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy)
|
||||
{
|
||||
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey);
|
||||
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key);
|
||||
|
||||
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy);
|
||||
};
|
||||
|
||||
AzureStorage.Constants = azureCommon.Constants;
|
||||
AzureStorage.StorageUtilities = azureCommon.StorageUtilities;
|
||||
AzureStorage.AccessCondition = azureCommon.AccessCondition;
|
||||
|
||||
AzureStorage.SR = azureCommon.SR;
|
||||
AzureStorage.StorageServiceClient = StorageServiceClient;
|
||||
AzureStorage.Logger = azureCommon.Logger;
|
||||
AzureStorage.WebResource = azureCommon.WebResource;
|
||||
AzureStorage.Validate = azureCommon.validate;
|
||||
AzureStorage.date = azureCommon.date;
|
||||
|
||||
// Other filters
|
||||
AzureStorage.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter;
|
||||
AzureStorage.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter;
|
||||
AzureStorage.RetryPolicyFilter = azureCommon.RetryPolicyFilter;
|
||||
|
||||
window.AzureStorage = AzureStorage;
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var browserify = require('browserify');
|
||||
var factor = require('factor-bundle');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
|
||||
var outputFolder = 'bundle';
|
||||
var outputFolderPath = path.resolve(__dirname, outputFolder);
|
||||
|
||||
console.log('Generating Azure Storage JavaScript Client Library to ' + outputFolderPath + ' ...\n');
|
||||
|
||||
if (!fs.existsSync(outputFolderPath)) {
|
||||
fs.mkdirSync(outputFolderPath);
|
||||
}
|
||||
|
||||
var b = browserify([
|
||||
path.resolve(__dirname, 'azure-storage.blob.export.js'),
|
||||
path.resolve(__dirname, 'azure-storage.file.export.js'),
|
||||
path.resolve(__dirname, 'azure-storage.queue.export.js'),
|
||||
path.resolve(__dirname, 'azure-storage.table.export.js')
|
||||
], {require: ['stream', 'util', 'buffer']});
|
||||
|
||||
b.plugin(factor, {
|
||||
outputs: [
|
||||
path.resolve(outputFolderPath, 'azure-storage.blob.js'),
|
||||
path.resolve(outputFolderPath, 'azure-storage.file.js'),
|
||||
path.resolve(outputFolderPath, 'azure-storage.queue.js'),
|
||||
path.resolve(outputFolderPath, 'azure-storage.table.js')
|
||||
]
|
||||
});
|
||||
|
||||
b.bundle().pipe(
|
||||
fs.createWriteStream(path.resolve(outputFolderPath, 'azure-storage.common.js'))
|
||||
);
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
+511
@@ -0,0 +1,511 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>Azure Storage JavaScript Client Library Sample for Blob Operations</title>
|
||||
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="mt-1">
|
||||
<h1>Azure Storage JavaScript Client Library Sample for Blob Operations</h1>
|
||||
</div>
|
||||
<p class="lead">In this sample, we will demonstrate common scenarios for Azure Blob Storage that includes creating, listing and deleting containers and blobs.</p>
|
||||
<hr/>
|
||||
<p>Azure Blob storage is a service for storing large amounts of unstructured object data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world, or to store application data privately.</p>
|
||||
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Note</b>: You may need set up a HTTP server to host this sample for IE browser, because IndexedDB is only available on websites with http or https URL schemes in IE. Azure Storage JavaScript Client Library currently depends on IndexedDB.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Contents:</h2>
|
||||
<ul>
|
||||
<li><a href="#step1">Step 1: Preparing an Azure Storage account with CORS rules set</a></li>
|
||||
<li><a href="#step2">Step 2: Importing Azure Storage JavaScript Client Library</a></li>
|
||||
<li><a href="#step3">Step 3: Creating an Azure Storage BlobService Object</a></li>
|
||||
<li><a href="#step4">Step 4: Container Operations</a></li>
|
||||
<li><a href="#step5">Step 5: Blob Operations</a></li>
|
||||
<li><a href="#step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</a></li>
|
||||
</ul>
|
||||
|
||||
<h2 id="step1">Step 1: Preparing an Azure Storage account with CORS rules set</h2>
|
||||
<p>Cross-origin resource sharing, or CORS, must be configured on the Azure Storage account to be accessed directly from JavaScript in the browser.
|
||||
You are able to set the CORS rules for specific Azure Storage account on the <a href="https://portal.azure.com">Azure Portal</a>.
|
||||
The "Allowed origins" could be set to "*" to allow all the origins in this sample.
|
||||
For more information about CORS, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services">Cross-Origin Resource Sharing (CORS)</a>.</p>
|
||||
<img src="cors.PNG"/>
|
||||
|
||||
<h2 id="step2">Step 2: Importing Azure Storage JavaScript Client Library</h2>
|
||||
<p>
|
||||
Importing <code>azure-storage.common.js</code> and <code>azure-storage.blob.js</code> in your HTML file for blob operations, and make sure <code>azure-storage.common.js</code> is in front of <code>azure-storage.blob.js</code>.
|
||||
<p>
|
||||
<pre>
|
||||
<script src="azure-storage.common.js"></script>
|
||||
<script src="azure-storage.blob.js"></script>
|
||||
</pre>
|
||||
|
||||
<h2 id="step3">Step 3: Creating an Azure Storage Blob Service Object</h2>
|
||||
<p>
|
||||
The <code>BlobService</code> object lets you work with containers and blobs.
|
||||
Following code creates a <code>BlobService</code> object with storage account and SAS Token.
|
||||
</p>
|
||||
<pre>
|
||||
var blobUri = 'https://' + 'STORAGE_ACCOUNT' + '.blob.core.windows.net';
|
||||
var blobService = AzureStorage.createBlobServiceWithSas(blobUri, 'SAS_TOKEN');
|
||||
</pre>
|
||||
<p>
|
||||
In Azure Storage JavaScript Client Library, a global variable <code>AzureStorage</code> is the start point where we can create service objects for blob/table/queue/file and access to the storage utilities.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>How to get full detailed API definitions? </b> Currently, the JavaScript Client Library shares the same API definitions with Node.js SDK.
|
||||
Please check API details on <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js API reference documents</a>. The JavaScript global variable <code>AzureStorage</code> is just like the object <code>require('azure-storage')</code> returns in Node.js.
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Warning</b>: Azure Storage JavaScript Client Library also supports creating <code>BlobService</code> based on Storage Account Key for authentication besides SAS Token.
|
||||
However, for security concerns, we recommend use of a limited time SAS Token, generated by a backend web server using a <a href="https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1">Stored Access Policy</a>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="step4">Step 4: Container Operations</h2>
|
||||
<p>
|
||||
A container provides a grouping of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs. Note that the container name must be lowercase.
|
||||
<code>BlobService</code> object provides plenty of interfaces for container operations.
|
||||
</p>
|
||||
|
||||
<h3>List Containers</h3>
|
||||
<p><code>BlobService</code> provides <code>listContainersSegmented</code> and <code>listContainersSegmentedWithPrefix</code> for retrieving the containers list under a storage account.</p>
|
||||
<pre>
|
||||
blobService.listContainersSegmented(null, function (error, results) {
|
||||
if (error) {
|
||||
// List container error
|
||||
} else {
|
||||
for (var i = 0, container; container = results.entries[i]; i++) {
|
||||
// Deal with container object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create Container</h3>
|
||||
<p><code>BlobService</code> provides <code>createContainer</code> and <code>createContainerIfNotExists</code> for creating a container under a storage account.</p>
|
||||
<pre>
|
||||
blobService.createContainerIfNotExists('mycontainer', function(error, result) {
|
||||
if (error) {
|
||||
// Create container error
|
||||
} else {
|
||||
// Create container successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete Container</h3>
|
||||
<p><code>BlobService</code> provides <code>deleteContainer</code> and <code>deleteContainerIfExists</code> for deleting a container under a storage account.</p>
|
||||
<pre>
|
||||
blobService.deleteContainerIfExists('mycontainer', function(error, result) {
|
||||
if (error) {
|
||||
// Delete container error
|
||||
} else {
|
||||
// Delete container successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Executable Example</h3>
|
||||
<p>
|
||||
The sample will try to create an Azure Storage blob service object based on SAS Token authorization.
|
||||
Enter your Azure Storage account name and SAS Token here, and executable examples in following steps dependent on the settings here.
|
||||
Make sure you have set the CORS rules for the Azure Storage blob service, and the SAS Token is in valid period.
|
||||
</p>
|
||||
<p>
|
||||
<label><b>Storage account:</b> </label> <input type="text" id="account"/>
|
||||
<label><b>SAS Token:</b> </label> <input type="text" id="sas"/>
|
||||
</p>
|
||||
<p>In the following executable example, you can try to list all the containers under your storage account settings, and try to create or delete one container from your account.</p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshContainer()">ListContainers</button> button to view the container list under your Azure Storage account</p></li>
|
||||
<li>
|
||||
<p> Click <button class="btn btn-xs btn-primary" onclick="createContainer()">CreateContainer</button> button to create a container under your Azure Storage account:</p>
|
||||
<p><label><b>New Container Name:</b> </label> <input type="text" value="mycontainer" id="newcontainer"/> </p>
|
||||
</li>
|
||||
<li><p> Click "<b>Delete</b>" button in the container list to delete the container under your Azure Storage account</p></li>
|
||||
<li><p> Click "<b>Select</b>" button to select and operate with the blobs in next step</p></li>
|
||||
</ul>
|
||||
<div id="containers"></div>
|
||||
|
||||
<h2 id="step5">Step 5: Blob Operations</h2>
|
||||
<p><b>Blob:</b> A file of any type and size. Azure Storage offers three types of blobs: block blobs, page blobs, and append blobs.</p>
|
||||
<p><b>Block blobs</b> are ideal for storing text or binary files, such as documents and media files. Append blobs are similar to block blobs in that they are made up of blocks, but they are optimized for append operations, so they are useful for logging scenarios. A single block blob can contain up to 50,000 blocks of up to 100 MB each, for a total size of slightly more than 4.75 TB (100 MB X 50,000). A single append blob can contain up to 50,000 blocks of up to 4 MB each, for a total size of slightly more than 195 GB (4 MB X 50,000).</p>
|
||||
<p><b>Page blobs</b> can be up to 1 TB in size, and are more efficient for frequent read/write operations. Azure Virtual Machines use page blobs as OS and data disks.</p>
|
||||
<p>For details about naming containers and blobs, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Naming-and-Referencing-Containers--Blobs--and-Metadata?redirectedfrom=MSDN">Naming and Referencing Containers, Blobs, and Metadata</a>.</p>
|
||||
|
||||
<h3>List Blobs</h3>
|
||||
<p><code>BlobService</code> provides <code>listBlobsSegmented</code> and <code>listBlobsSegmentedWithPrefix</code> for retrieving the blobs list under a container.</p>
|
||||
<pre>
|
||||
blobService.listBlobsSegmented('mycontainer', null, function (error, results) {
|
||||
if (error) {
|
||||
// List blobs error
|
||||
} else {
|
||||
for (var i = 0, blob; blob = results.entries[i]; i++) {
|
||||
// Deal with blob object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
<h3>Upload Blob</h3>
|
||||
<p><code>BlobService</code> provides <code>createBlockBlobFromBrowserFile</code>, <code>createPageBlobFromBrowserFile</code>, <code>createAppendBlobFromBrowserFile</code> and <code>appendFromBrowserFile</code> for uploading or appending a blob from an HTML file in browsers.
|
||||
</p>
|
||||
|
||||
<p>Uploading blob from stream. You can set up the blob name as well as the size of this uploading session. </p>
|
||||
<pre>
|
||||
// If one file has been selected in the HTML file input element
|
||||
var file = document.getElementById('fileinput').files[0];
|
||||
|
||||
var customBlockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;
|
||||
blobService.singleBlobPutThresholdInBytes = customBlockSize;
|
||||
|
||||
var finishedOrError = false;
|
||||
var speedSummary = blobService.createBlockBlobFromBrowserFile('mycontainer', file.name, file, {blockSize : customBlockSize}, function(error, result, response) {
|
||||
finishedOrError = true;
|
||||
if (error) {
|
||||
// Upload blob failed
|
||||
} else {
|
||||
// Upload successfully
|
||||
}
|
||||
});
|
||||
refreshProgress();
|
||||
</pre>
|
||||
<p>Checking the upload progress with speedSummary object. </p>
|
||||
<pre>
|
||||
function refreshProgress() {
|
||||
setTimeout(function() {
|
||||
if (!finishedOrError) {
|
||||
var process = speedSummary.getCompletePercent();
|
||||
displayProcess(process);
|
||||
refreshProgress();
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
</pre>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Warning:</b>
|
||||
By default, the <code>speedSummary.getCompletePercent()</code> only updates progress when a block is uploaded to server. There are 2 default settings may influence the upload progress display.
|
||||
|
||||
<ul>
|
||||
<li><code>blobService.singleBlobPutThresholdInBytes</code> is the maximum size (default 32MB), in bytes, of a blob before it must be separated into blocks.</li>
|
||||
<li>Option <code>{blockSize: SizeInBytes}</code> of <code>blobService.createBlockBlobFromStream()</code> is the size (default 4MB) of every block in the storage layer.</li>
|
||||
</ul>
|
||||
|
||||
This means, by default, blobs smaller than 32MB will only get the progress update when the upload is over, and blobs larger than 32MB will update the process every 4MB.
|
||||
For slow connections or progress reporting for small blobs, you can customize both the two settings into samller values such as 1MB or 512KB. Thus the progress will update with the smaller step you set.
|
||||
However, very small block sizes will impact the storage performance especially for a large blob.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3>Download Blob</h3>
|
||||
<p>
|
||||
<code>BlobService</code> provides interfaces for downloading a blob into browser memory.
|
||||
Because of browser's sandbox limitation, we cannot save the downloaded data trunks into disk until we get all the data trunks of a blob into browser memory.
|
||||
The browser's memory size is also limited especially for downloading huge blobs, so it's recommended to download a blob in browser with SAS Token authorized link directly.
|
||||
</p>
|
||||
<p>
|
||||
Shared access signatures (SAS) are a secure way to provide granular access to blobs and containers without providing your storage account name or keys. Shared access signatures are often used to provide limited access to your data, such as allowing a mobile app to access blobs.
|
||||
The following code example generates a new shared access policy that allows the shared access signatures holder to perform read operations on the myblob blob, and expires 100 minutes after the time it is created.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>Note: </b> You can choose to use the SAS Token in browser side, or generate a temporary SAS Token dynamically in your server side with Azure Storage C# or Node.js SDKs etc. according to your security requirements.
|
||||
</div>
|
||||
</div>
|
||||
<pre>
|
||||
var downloadLink = blobService.getUrl('mycontainer', 'myblob', 'SAS_TOKEN');
|
||||
</pre>
|
||||
|
||||
<h3>Delete Blob</h3>
|
||||
<p><code>BlobService</code> provides <code>deleteContainer</code> and <code>deleteContainerIfExists</code> for deleting a blob under a storage account.</p>
|
||||
<pre>
|
||||
blobService.deleteBlobIfExists(container, blob, function(error, result) {
|
||||
if (error) {
|
||||
// Delete blob failed
|
||||
} else {
|
||||
// Delete blob successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
<h3>Executable Example</h3>
|
||||
<p>After clicked the "<b>Select</b>" button on the container list in last step, you are able to operate with the blobs under the selected container. </p><p><b><label>Selected container name: </label></b> <input type="text" id="container" disabled=true/></p>
|
||||
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshBlobList()">ListBlobs</button> button to view the blobs under your selected container</p></li>
|
||||
<li><p>
|
||||
Click <button id="upload-button" class="btn btn-xs btn-primary" onclick="uploadBlobByStream(false)">UploadBlob</button> button to upload a local file to current container after selecting a file:
|
||||
</p>
|
||||
<p> <input type="file" id="files" name="file" onclick="displayProcess(0)" /> </p></li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the blob</p></li>
|
||||
<li><p> Click "<b>Download</b>" link to download a blob to local</p></li>
|
||||
</ul>
|
||||
|
||||
<hr/>
|
||||
<div> Uploaded Bytes: <font id="read"> </font> </div>
|
||||
<div class="progress">
|
||||
<div id="progress" class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
|
||||
0%
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="result"></div>
|
||||
|
||||
<h3 id="step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</h3>
|
||||
<ul>
|
||||
<li>1. Setting CORS rules for your selected Azure-Storage account blob service.</li>
|
||||
<li>2. Including "azure-storage.common.js" in the html file.</li>
|
||||
<li>3. Including functional file(s) needed, such as "azure-storage.blob.js" for blob operation.</li>
|
||||
<li>4. Using keyword "AzureStorage" to access to Azure storage JavaScript APIs.</li>
|
||||
<li>5. Referring to <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js SDK documents</a> for detailed API definitions which keep same with JavaScript APIs.</li>
|
||||
</ul>
|
||||
<p> You can view the source code of this sample for detailed reference. </p>
|
||||
</div>
|
||||
|
||||
<!-- azure-storage.common.js also exports Node.js module stream, util and buffer -->
|
||||
<script src="../bundle/azure-storage.common.js"></script>
|
||||
<script src="../bundle/azure-storage.blob.js"></script>
|
||||
|
||||
<script>
|
||||
var account = document.getElementById('account').value;
|
||||
var sas = document.getElementById('sas').value;
|
||||
var container = '';
|
||||
var blobUri = '';
|
||||
|
||||
function checkParameters() {
|
||||
account = document.getElementById('account').value;
|
||||
sas = document.getElementById('sas').value;
|
||||
|
||||
if (account == null || account.length < 1)
|
||||
{
|
||||
alert('Please enter a valid storage account name!');
|
||||
return false;
|
||||
}
|
||||
if (sas == null || sas.length < 1)
|
||||
{
|
||||
alert('Please enter a valid SAS Token!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getBlobService() {
|
||||
if (!checkParameters())
|
||||
return null;
|
||||
|
||||
blobUri = 'https://' + account + '.blob.core.windows.net';
|
||||
var blobService = AzureStorage.createBlobServiceWithSas(blobUri, sas).withFilter(new AzureStorage.ExponentialRetryPolicyFilter());
|
||||
return blobService;
|
||||
}
|
||||
|
||||
function refreshContainer() {
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
document.getElementById('containers').innerHTML = 'Loading...';
|
||||
blobService.listContainersSegmented(null, function (error, results) {
|
||||
if (error) {
|
||||
alert('List container error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>ContainerName</th>',
|
||||
'<th>ContainerETag</th>',
|
||||
'<th>LastModified</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, container; container = results.entries[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', container.name, '</td>',
|
||||
'<td>', container.etag, '</td>',
|
||||
'<td>', container.lastModified, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteContainer(\'', container.name ,'\')">Delete</button> ',
|
||||
'<button class="btn btn-xs btn-success" onclick="viewContainer(\'', container.name ,'\')">Select</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('containers').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteContainer(name) {
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
blobService.deleteContainerIfExists(name, function(error, result) {
|
||||
if (error) {
|
||||
alert('Delete container failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete ' + name + ' successfully!');
|
||||
refreshContainer();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createContainer() {
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
var container = document.getElementById('newcontainer').value;
|
||||
if (!AzureStorage.Validate.containerNameIsValid(container, function(err, res){})) {
|
||||
alert('Invalid container name!');
|
||||
return;
|
||||
}
|
||||
|
||||
blobService.createContainerIfNotExists(container, function(error, result){
|
||||
if (error) {
|
||||
alert('Create container failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Create ' + container + ' successfully!');
|
||||
refreshContainer();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function viewContainer(selectedContainer) {
|
||||
container = selectedContainer;
|
||||
document.getElementById('container').value = container;
|
||||
alert('Selected ' + container + ' !');
|
||||
refreshBlobList();
|
||||
}
|
||||
|
||||
function refreshBlobList() {
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
document.getElementById('result').innerHTML = 'Loading...';
|
||||
blobService.createContainerIfNotExists(container, function(error, result) {
|
||||
if (error) {
|
||||
alert('createContainerIfNotExists error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
blobService.listBlobsSegmented(container, null, function (error, results) {
|
||||
if (error) {
|
||||
alert('List blob error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>BlobName</th>',
|
||||
'<th>ContentLength</th>',
|
||||
'<th>LastModified</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, blob; blob = results.entries[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', blob.name, '</td>',
|
||||
'<td>', blob.contentLength, '</td>',
|
||||
'<td>', blob.lastModified, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteBlob(\'', blob.name ,'\')">Delete</button> ',
|
||||
'<a class="btn btn-xs btn-success" href="', blobUri + '/' + container + '/' + blob.name + sas, '" download>Download</a>' , '</td>',
|
||||
'</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('result').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function deleteBlob(blob) {
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
blobService.deleteBlobIfExists(container, blob, function(error, result) {
|
||||
if (error) {
|
||||
alert('Delete blob failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete ' + blob + ' successfully!');
|
||||
refreshBlobList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function displayProcess(process) {
|
||||
document.getElementById("progress").style.width = process + '%';
|
||||
document.getElementById("progress").innerHTML = process + '%';
|
||||
}
|
||||
|
||||
function uploadBlobByStream(checkMD5) {
|
||||
var files = document.getElementById('files').files;
|
||||
if (!files.length) {
|
||||
alert('Please select a file!');
|
||||
return;
|
||||
}
|
||||
var file = files[0];
|
||||
|
||||
var blobService = getBlobService();
|
||||
if (!blobService)
|
||||
return;
|
||||
|
||||
var btn = document.getElementById("upload-button");
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = "Uploading";
|
||||
|
||||
// Make a smaller block size when uploading small blobs
|
||||
var blockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;
|
||||
var options = {
|
||||
storeBlobContentMD5 : checkMD5,
|
||||
blockSize : blockSize
|
||||
};
|
||||
blobService.singleBlobPutThresholdInBytes = blockSize;
|
||||
|
||||
var finishedOrError = false;
|
||||
var speedSummary = blobService.createBlockBlobFromBrowserFile(container, file.name, file, options, function(error, result, response) {
|
||||
finishedOrError = true;
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = "UploadBlob";
|
||||
if (error) {
|
||||
alert('Upload filed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
displayProcess(0);
|
||||
} else {
|
||||
displayProcess(100);
|
||||
setTimeout(function() { // Prevent alert from stopping UI progress update
|
||||
alert('Upload successfully!');
|
||||
}, 1000);
|
||||
refreshBlobList();
|
||||
}
|
||||
});
|
||||
|
||||
function refreshProgress() {
|
||||
setTimeout(function() {
|
||||
if (!finishedOrError) {
|
||||
var process = speedSummary.getCompletePercent();
|
||||
displayProcess(process);
|
||||
refreshProgress();
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
refreshProgress();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+588
@@ -0,0 +1,588 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>Azure Storage JavaScript Client Library Sample for File Operations</title>
|
||||
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="mt-1">
|
||||
<h1>Azure Storage JavaScript Client Library Sample for File Operations</h1>
|
||||
</div>
|
||||
<p class="lead">In this sample, we will demonstrate common scenarios for Azure File Storage that includes creating, listing and deleting file shares, directories and files.</p>
|
||||
<hr/>
|
||||
<p>Azure File storage is a service for storing large amounts of unstructured object data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use file storage to expose data publicly to the world, or to store application data privately.</p>
|
||||
<p>With Azure File storage, you can migrate legacy applications that rely on file shares to Azure quickly and without costly rewrites. Applications running in Azure virtual machines or cloud services or from on-premises clients can mount a file share in the cloud, just as a desktop application mounts a typical SMB share. Any number of application components can then mount and access the File storage share simultaneously. In this sample, you are able to create a file service with storage account and SAS Token. Based on the file service, you could create a file share, list files, upload files and delete files.</p>
|
||||
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Note</b>: You may need set up a HTTP server to host this sample for IE browser, because IndexedDB is only available on websites with http or https URL schemes in IE. Azure Storage JavaScript Client Library currently depends on IndexedDB.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Contents:</h2>
|
||||
<ul>
|
||||
<li><a href="#step1">Step 1: Preparing an Azure Storage account with CORS rules set</a></li>
|
||||
<li><a href="#step2">Step 2: Importing Azure Storage JavaScript Client Library</a></li>
|
||||
<li><a href="#step3">Step 3: Creating an Azure Storage File Service Object</a></li>
|
||||
<li><a href="#step4">Step 4: File Operations</a></li>
|
||||
<li><a href="#step5">Step 5: Directory and File Operations</a></li>
|
||||
<li><a href="#step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</a></li>
|
||||
</ul>
|
||||
|
||||
<h2 id="step1">Step 1: Preparing an Azure Storage account with CORS rules set</h2>
|
||||
<p>Cross-origin resource sharing, or CORS, must be configured on the Azure Storage account to be accessed directly from JavaScript in the browser.
|
||||
You are able to set the CORS rules for specific Azure Storage account on the <a href="https://portal.azure.com">Azure Portal</a>.
|
||||
The "Allowed origins" could be set to "*" to allow all the origins in this sample.
|
||||
For more information about CORS, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services">Cross-Origin Resource Sharing (CORS)</a>.</p>
|
||||
<img src="cors.PNG"/>
|
||||
|
||||
<h2 id="step2">Step 2: Importing Azure Storage JavaScript Files</h2>
|
||||
<p>
|
||||
Importing <code>azure-storage.common.js</code> and <code>azure-storage.file.js</code> in your HTML file for file operations, and make sure <code>azure-storage.common.js</code> is in front of <code>azure-storage.file.js</code>.
|
||||
<p>
|
||||
<pre>
|
||||
<script src="azure-storage.common.js"></script>
|
||||
<script src="azure-storage.file.js"></script>
|
||||
</pre>
|
||||
|
||||
<h2 id="step3">Step 3: Creating an Azure Storage File Service Object</h2>
|
||||
<p>
|
||||
The <code>FileService</code> object lets you work with files and directories.
|
||||
Following code creates a <code>FileService</code> object with storage account and SAS Token.
|
||||
</p>
|
||||
<pre>
|
||||
var fileUri = 'https://' + 'STORAGE_ACCOUNT' + '.file.core.windows.net';
|
||||
var fileService = AzureStorage.createFileServiceWithSas(fileUri, 'SAS_TOKEN');
|
||||
</pre>
|
||||
<p>
|
||||
In Azure Storage JavaScript Client Library, a global variable <code>AzureStorage</code> is the start point where we can create service objects for blob/table/queue/file and access to the storage utilities.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>How to get full detailed API definitions? </b> Currently, the JavaScript Client Library shares the same API definitions with Node.js SDK.
|
||||
Please check API details on <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js API reference documents</a>. The JavaScript global variable <code>AzureStorage</code> is just like the object <code>require('azure-storage')</code> returns in Node.js.
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Warning</b>: Azure Storage JavaScript Client Library also supports creating <code>FileService</code> based on Storage Account Key for authentication besides SAS Token.
|
||||
However, for security concerns, we recommend use of a limited time SAS Token, generated by a backend web server using a <a href="https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1">Stored Access Policy</a>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="step4">Step 4: File Share Operations</h2>
|
||||
<p><b>Share</b>: A File storage share is an SMB file share in Azure. All directories and files must be created in a parent share. An account can contain an unlimited number of shares, and a share can store an unlimited number of files, up to the 5 TB total capacity of the file share.</p>
|
||||
|
||||
<h3>List File Shares</h3>
|
||||
<p><code>FileService</code> provides <code>listSharesSegmented</code> and <code>listSharesSegmentedWithPrefix</code> for listing file shares under a storage account.</p>
|
||||
<pre>
|
||||
fileService.listSharesSegmented(null, function(error, result) {
|
||||
if(error) {
|
||||
// List shares error
|
||||
} else {
|
||||
for (var i = 0, share; share = results.entries[i]; i++) {
|
||||
// Deal with share object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create File Share</h3>
|
||||
<p><code>FileService</code> provides <code>createShare</code> and <code>createShareIfNotExists</code> for creating a file share under a storage account.</p>
|
||||
<pre>
|
||||
fileService.createShareIfNotExists('myshare', function(error, result) {
|
||||
if(error) {
|
||||
// Create share error
|
||||
} else {
|
||||
// Create share successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete File Share</h3>
|
||||
<p><code>FileService</code> provides <code>deleteShare</code> and <code>deleteShareIfExists</code> for deleting a file share under a storage account.</p>
|
||||
<pre>
|
||||
fileService.deleteShareIfExists('myshare', function(error, result) {
|
||||
if(error) {
|
||||
// Delete share error
|
||||
} else {
|
||||
// Delete share successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Executable Example</h3>
|
||||
<p>The sample will try to create an Azure Storage file service object based on SAS Token authorization. Enter your Azure Storage account name and SAS Token here. Make sure you have set the CORS rules for the Azure Storage file service, and the SAS Token is in valid period.</p>
|
||||
<label><b>Storage account:</b> </label> <input type="text" id="account"/>
|
||||
<label><b>SAS Token:</b> </label> <input type="text" id="sas"/>
|
||||
<p> Azure Storage file service provides plenty of interfaces for file operations. In following example, you can try to list all the file shares under your storage account, and try to create or delete one file share from your account.</p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshFileShareList()">ListFileShares</button> button to view the file share list under your Azure Storage account</p></li>
|
||||
<li>
|
||||
<p> Click <button class="btn btn-xs btn-primary" onclick="createFileShare()">CreateFileShare</button> button to create a file share under your Azure Storage account</p>
|
||||
<p><label><b>New File Share Name:</b> </label> <input type="text" value="myfileshare" id="newfileshare"/> </p>
|
||||
</li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the file share under your Azure Storage account</p></li>
|
||||
<li><p> Click "<b>Select</b>" button to operate with the directories and files in next step</p></li>
|
||||
</ul>
|
||||
<div id="result"></div>
|
||||
|
||||
<h2 id="step5">Step 5: Directory and File Operations</h2>
|
||||
<p><b>Directory</b> in storage is an optional hierarchy of directories.</p>
|
||||
<p><b>File</b>: A file in the share. A file may be up to 1 TB in size.</p>
|
||||
|
||||
<h3>List Files and Directories</h3>
|
||||
<p><code>FileService</code> provides <code>listFilesAndDirectoriesSegmented</code> for listing directories and files under a file share.</p>
|
||||
<pre>
|
||||
fileService.listFilesAndDirectoriesSegmented('myfileshare', '', null, function(error, result, response) {
|
||||
if(error) {
|
||||
// List table entity error
|
||||
} else {
|
||||
for (var i = 0, dir; dir = results.entries.directories[i]; i++) {
|
||||
// Deal with directory object
|
||||
}
|
||||
for (var i = 0, file; file = results.entries.files[i]; i++) {
|
||||
// Deal with file object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create Directory</h3>
|
||||
<p><code>FileService</code> provides <code>createDirectory</code> and <code>createDirectoryIfNotExists</code> for creating directories under a file share.</p>
|
||||
<pre>
|
||||
fileService.createDirectoryIfNotExists('myfileshare', 'mydirectory', function(error, result, response) {
|
||||
if(error) {
|
||||
// Create directory error
|
||||
} else {
|
||||
// Create directory successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete Directory</h3>
|
||||
<p><code>FileService</code> provides <code>deleteDirectory</code> and <code>deleteDirectoryIfExists</code> for deleting directories under a file share.</p>
|
||||
<pre>
|
||||
fileService.deleteDirectoryIfExists('myfileshare', 'mydirectory', function(error, result, response) {
|
||||
if(error) {
|
||||
// Delete directory error
|
||||
} else {
|
||||
// Delete directory successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Upload File</h3>
|
||||
<p><code>FileService</code> provides <code>createFileFromBrowserFile</code> for uploading a file from an HTML file in browsers.
|
||||
</p>
|
||||
|
||||
<p>Uploading file from stream. You can set up the file name as well as the size of this uploading session. </p>
|
||||
<pre>
|
||||
// If one file has been selected in the HTML file input element
|
||||
var files = document.getElementById('fileinput').files;
|
||||
var file = files[0];
|
||||
|
||||
var finishedOrError = false;
|
||||
var speedSummary = fileService.createFileFromBrowserFile('myfileshare', 'mydirectory', file.name, file, {}, function(error, result, response) {
|
||||
finishedOrError = true;
|
||||
if (error) {
|
||||
// Upload file failed
|
||||
} else {
|
||||
// Upload successfully
|
||||
}
|
||||
});
|
||||
refreshProgress();
|
||||
</pre>
|
||||
<p>Checking the upload progress with speedSummary object. </p>
|
||||
<pre>
|
||||
function refreshProgress() {
|
||||
setTimeout(function() {
|
||||
if (!finishedOrError) {
|
||||
var process = speedSummary.getCompletePercent();
|
||||
displayProcess(process);
|
||||
refreshProgress();
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
</pre>
|
||||
<h3>Download File</h3>
|
||||
<p>
|
||||
<code>FileService</code> provides interfaces for downloading a file into browser memory directly.
|
||||
Because of browser's sandbox limitation, we cannot save the downloaded data trunks into disk until we get all the data trunks of a file into browser memory.
|
||||
The browser's memory size is also limited especially for downloading huge files, so it's recommended to download a file in browser with SAS Token authorized link directly.
|
||||
</p>
|
||||
<p>
|
||||
Shared access signatures (SAS) are a secure way to provide granular access to files and directories without providing your storage account name or keys. Shared access signatures are often used to provide limited access to your data, such as allowing a mobile app to access files.
|
||||
The following code example generates a new shared access policy that allows the shared access signatures holder to perform read operations on the myfile file, and expires 100 minutes after the time it is created.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>Note: </b> You can choose to use the SAS Token in browser side, or generate a temporary SAS Token dynamically in your server side with Azure Storage C# or Node.js SDKs etc. according to your security requirements.
|
||||
</div>
|
||||
</div>
|
||||
<pre>
|
||||
var downloadLink = fileService.getUrl('myshare', 'mydirectory', 'myfile', 'SAS_TOKEN');
|
||||
</pre>
|
||||
|
||||
<h3>Delete File</h3>
|
||||
<p><code>FileService</code> provides <code>deleteFile</code> and <code>deleteFileIfExists</code> for deleting files under a file share.</p>
|
||||
<pre>
|
||||
fileService.deleteFileIfExists('myfileshare', 'mydirectory', 'myfile', function(error, result, response) {
|
||||
if(error) {
|
||||
// Delete file error
|
||||
} else {
|
||||
// Delete file successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Executable Example</h3>
|
||||
<p>After clicked the "<b>Select</b>" button on the file share list, you are able to operate with the directories and files under the selected file share.<p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshDirectoryFileList()">ListFilesAndDirectories</button> button to view the directories and files under your selected file share</p></li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="createDirectory()">CreateDirectory</button> button to create a directory share under your current directory</p>
|
||||
<p><label><b>New Directory Name:</b> </label> <input type="text" value="mydirectory" id="newdirectory"/> </p>
|
||||
</li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="backDirectory()">GoToUpperDirectory</button> button to return to upper level directory</p></li>
|
||||
<li><p> Click <button id="upload-button" class="btn btn-xs btn-primary" onclick="createFileFromStream(false)">Upload</button> button to upload a local file to current directory</p>
|
||||
<p><input type="file" id="files" name="file" onclick="displayProcess(0)" /></p></li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the directory or file</p></li>
|
||||
<li><p> Click "<b>Download</b>" link to download a file to local</p></li>
|
||||
</ul>
|
||||
|
||||
<b>Current Path:<a id="path"></a></b>
|
||||
<div> Uploaded Bytes: <font id="read"> </font> </div>
|
||||
<div class="progress">
|
||||
<div id="progress" class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
|
||||
0%
|
||||
</div>
|
||||
</div>
|
||||
<div id="directoryFiles"></div>
|
||||
|
||||
<h3 id="step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</h3>
|
||||
<ul>
|
||||
<li>1. Setting CORS rules for your selected Azure-Storage account file service.</li>
|
||||
<li>2. Including "azure-storage.common.js" in the html file.</li>
|
||||
<li>3. Including functional file(s) needed, such as "azure-storage.file.js" for file operation.</li>
|
||||
<li>4. Using keyword "AzureStorage" to access to Azure storage JavaScript APIs.</li>
|
||||
<li>5. Referring to <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js SDK documents</a> for detailed API definitions which keep same with JavaScript APIs.</li>
|
||||
</ul>
|
||||
<p> You can view the source code of this sample for detailed reference. </p>
|
||||
</div>
|
||||
|
||||
<script src="../bundle/azure-storage.common.js"></script>
|
||||
<script src="../bundle/azure-storage.file.js"></script>
|
||||
|
||||
<script>
|
||||
var account = document.getElementById('account').value;
|
||||
var sas = document.getElementById('sas').value;
|
||||
var fileShare = '';
|
||||
var currentPath = '';
|
||||
var fileUri = '';
|
||||
var currentPath = [];
|
||||
|
||||
function checkParameters() {
|
||||
account = document.getElementById('account').value;
|
||||
sas = document.getElementById('sas').value;
|
||||
|
||||
if (account == null || account.length < 1)
|
||||
{
|
||||
alert('Please enter a valid storage account name!');
|
||||
return false;
|
||||
}
|
||||
if (sas == null || sas.length < 1)
|
||||
{
|
||||
alert('Please enter a valid SAS Token!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getFileService() {
|
||||
if (!checkParameters())
|
||||
return null;
|
||||
|
||||
fileUri = 'https://' + account + '.file.core.windows.net';
|
||||
var fileService = AzureStorage.createFileServiceWithSas(fileUri, sas).withFilter(new AzureStorage.ExponentialRetryPolicyFilter());
|
||||
return fileService;
|
||||
}
|
||||
|
||||
function refreshFileShareList()
|
||||
{
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
document.getElementById('result').innerHTML = 'Loading...';
|
||||
fileService.listSharesSegmented(null, function (error, results) {
|
||||
if (error) {
|
||||
alert('List file share error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>ShareName</th>',
|
||||
'<th>ShareETag</th>',
|
||||
'<th>ShareQuota</th>',
|
||||
'<th>LastModified</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, share; share = results.entries[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', share.name, '</td>',
|
||||
'<td>', share.etag, '</td>',
|
||||
'<td>', share.quota, '</td>',
|
||||
'<td>', share.lastModified, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteFileShare(\'', share.name ,'\')">Delete</button> ',
|
||||
'<button class="btn btn-xs btn-success" onclick="viewFileShare(\'', share.name ,'\')">Select</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('result').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteFileShare(name) {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
fileService.deleteShareIfExists(name, function(error, result){
|
||||
if (error) {
|
||||
alert('Delete file share failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete ' + name + ' successfully!');
|
||||
refreshFileShareList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createFileShare() {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
var share = document.getElementById('newfileshare').value;
|
||||
if (!AzureStorage.Validate.shareNameIsValid(share, function(err, res){})) {
|
||||
alert('Invalid share name!');
|
||||
return;
|
||||
}
|
||||
|
||||
fileService.createShareIfNotExists(share, function(error, result){
|
||||
if (error) {
|
||||
alert('Create file share failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Create ' + share + ' successfully!');
|
||||
refreshFileShareList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function viewFileShare(selectedFileShare) {
|
||||
fileShare = selectedFileShare;
|
||||
alert('Selected ' + fileShare + ' !');
|
||||
currentPath = [];
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
|
||||
function backDirectory() {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
if (fileShare.length < 1) {
|
||||
alert('Please select one file share!');
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentPath.length > 0)
|
||||
currentPath.pop();
|
||||
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
|
||||
function refreshDirectoryFileList(directory)
|
||||
{
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
if (fileShare.length < 1) {
|
||||
alert('Please select one file share!');
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof directory === 'undefined')
|
||||
var directory = '';
|
||||
if (directory.length > 0)
|
||||
currentPath.push(directory);
|
||||
directory = currentPath.join('\\\\');
|
||||
|
||||
document.getElementById('directoryFiles').innerHTML = 'Loading...';
|
||||
fileService.listFilesAndDirectoriesSegmented(fileShare, directory, null, function (error, results) {
|
||||
if (error) {
|
||||
alert('List directories and files error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
document.getElementById('path').innerHTML = directory;
|
||||
|
||||
var outputDirectory = [];
|
||||
outputDirectory.push('<tr>',
|
||||
'<th>Type</th>',
|
||||
'<th>Name</th>',
|
||||
'<th>ContentLength</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.directories.length < 1 && results.entries.files.length < 1) {
|
||||
outputDirectory.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, dir; dir = results.entries.directories[i]; i++) {
|
||||
outputDirectory.push('<tr>',
|
||||
'<td>', 'DIR', '</td>',
|
||||
'<td>', dir.name, '</td>',
|
||||
'<td>', dir.contentLength, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteDirectory(\'', dir.name ,'\')">Delete</button> ',
|
||||
'<button class="btn btn-xs btn-success" onclick="refreshDirectoryFileList(\'', dir.name ,'\')">View</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
|
||||
var outputFiles = [];
|
||||
var currentDir = currentPath.join('\\');
|
||||
if (currentPath.length > 0)
|
||||
currentDir += '/';
|
||||
|
||||
for (var i = 0, file; file = results.entries.files[i]; i++) {
|
||||
outputFiles.push('<tr>',
|
||||
'<td>', 'FILE', '</td>',
|
||||
'<td>', file.name, '</td>',
|
||||
'<td>', file.contentLength, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteFile(\'', file.name ,'\')">Delete</button> ',
|
||||
'<a class="btn btn-xs btn-success" href="', fileUri + '/' + fileShare + '/' + currentDir + file.name + sas, '" download>Download</a>' , '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('directoryFiles').innerHTML = '<table class="table table-condensed table-bordered">' + outputDirectory.join('') + outputFiles.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDirectory(name) {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
if (fileShare.length < 1) {
|
||||
alert("Please select a file share!");
|
||||
return;
|
||||
}
|
||||
|
||||
fileService.deleteDirectoryIfExists(fileShare, currentPath.join('\\\\') + '\\' + name, function(error, results) {
|
||||
if (error) {
|
||||
alert("Delete directory failed, open brower console for more detailed info.");
|
||||
console.log(error);
|
||||
} else {
|
||||
alert("Delete " + name + " successfully!");
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteFile(file) {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
fileService.deleteFileIfExists(fileShare, currentPath.join('\\\\'), file, function(error, results) {
|
||||
if (error) {
|
||||
alert("Delete file failed, open brower console for more detailed info.");
|
||||
console.log(error);
|
||||
} else {
|
||||
alert("Delete " + file + " successfully!");
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createDirectory() {
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
var directoryName = document.getElementById('newdirectory').value;
|
||||
fileService.createDirectoryIfNotExists(fileShare, currentPath.join('\\\\') + '\\' + directoryName, function(error, results) {
|
||||
if (error) {
|
||||
alert("Create directory failed, open brower console for more detailed info.");
|
||||
console.log(error);
|
||||
} else {
|
||||
alert("Create " + directoryName + " successfully!");
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function displayProcess(process) {
|
||||
document.getElementById("progress").style.width = process + '%';
|
||||
document.getElementById("progress").innerHTML = process + '%';
|
||||
}
|
||||
|
||||
function createFileFromStream(checkMD5) {
|
||||
var files = document.getElementById('files').files;
|
||||
if (!files.length) {
|
||||
alert('Please select a file!');
|
||||
return;
|
||||
}
|
||||
var file = files[0];
|
||||
|
||||
var fileService = getFileService();
|
||||
if (!fileService)
|
||||
return;
|
||||
|
||||
var btn = document.getElementById("upload-button");
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = "Uploading";
|
||||
var finishedOrError = false;
|
||||
var options = {
|
||||
contentSettings: {
|
||||
contentType: file.type
|
||||
},
|
||||
storeFileContentMD5 : checkMD5
|
||||
};
|
||||
|
||||
var speedSummary = fileService.createFileFromBrowserFile(fileShare, currentPath.join('\\\\'), file.name, file, options, function(error, result, response) {
|
||||
finishedOrError = true;
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = "Upload";
|
||||
if (error) {
|
||||
alert("Upload filed, open brower console for more detailed info.");
|
||||
console.log(error);
|
||||
displayProcess(0);
|
||||
} else {
|
||||
displayProcess(100);
|
||||
setTimeout(function() { // Prevent alert from stopping UI progress update
|
||||
alert('Upload successfully!');
|
||||
}, 1000);
|
||||
refreshDirectoryFileList();
|
||||
}
|
||||
});
|
||||
|
||||
function refreshProgress() {
|
||||
setTimeout(function() {
|
||||
if (!finishedOrError) {
|
||||
var process = speedSummary.getCompletePercent();
|
||||
displayProcess(process);
|
||||
refreshProgress();
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
refreshProgress();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+465
@@ -0,0 +1,465 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>Azure Storage JavaScript Client Library Sample for Queue Operations</title>
|
||||
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="mt-1">
|
||||
<h1>Azure Storage JavaScript Client Library Sample for Queue Operations</h1>
|
||||
</div>
|
||||
<p class="lead">In this sample, we will demonstrate common scenarios for Azure Queue Storage that includes creating, listing and deleting queues and messages.</p>
|
||||
<hr/>
|
||||
|
||||
<p>Azure Storage queue service provides cloud messaging between application components. In designing applications for scale, application components are often decoupled, so that they can scale independently. Queue storage delivers asynchronous messaging for communication between application components, whether they are running in the cloud, on the desktop, on an on-premises server, or on a mobile device. Queue storage also supports managing asynchronous tasks and building process work flows.
|
||||
</p>
|
||||
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Note</b>: You may need set up a HTTP server to host this sample for IE browser, because IndexedDB is only available on websites with http or https URL schemes in IE. Azure Storage JavaScript Client Library currently depends on IndexedDB.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Contents:</h2>
|
||||
<ul>
|
||||
<li><a href="#step1">Step 1: Preparing an Azure Storage account with CORS rules set</a></li>
|
||||
<li><a href="#step2">Step 2: Importing Azure Storage JavaScript Client Library</a></li>
|
||||
<li><a href="#step3">Step 3: Creating an Azure Storage Queue Service Object</a></li>
|
||||
<li><a href="#step4">Step 4: Queue Operations</a></li>
|
||||
<li><a href="#step5">Step 5: Message Operations</a></li>
|
||||
<li><a href="#step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</a></li>
|
||||
</ul>
|
||||
|
||||
<h2 id="step1">Step 1: Preparing an Azure Storage account with CORS rules set</h2>
|
||||
<p>Cross-origin resource sharing, or CORS, must be configured on the Azure Storage account to be accessed directly from JavaScript in the browser.
|
||||
You are able to set the CORS rules for specific Azure Storage account on the <a href="https://portal.azure.com">Azure Portal</a>.
|
||||
The "Allowed origins" could be set to "*" to allow all the origins in this sample.
|
||||
For more information about CORS, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services">Cross-Origin Resource Sharing (CORS)</a>.</p>
|
||||
<img src="cors.PNG"/>
|
||||
|
||||
<h2 id="step2">Step 2: Importing Azure Storage JavaScript Client Library</h2>
|
||||
<p>
|
||||
Importing <code>azure-storage.common.js</code> and <code>azure-storage.queue.js</code> in your HTML file for queue operations, and make sure <code>azure-storage.common.js</code> is in front of <code>azure-storage.queue.js</code>.
|
||||
<p>
|
||||
<pre>
|
||||
<script src="azure-storage.common.js"></script>
|
||||
<script src="azure-storage.queue.js"></script>
|
||||
</pre>
|
||||
|
||||
<h2 id="step3">Step 3: Creating an Azure Storage Queue Service Object</h2>
|
||||
<p>
|
||||
The <code>QueueService</code> object lets you work with queues and messages.
|
||||
Following code creates a <code>QueueService</code> object with storage account and SAS Token.
|
||||
</p>
|
||||
<pre>
|
||||
var queueUri = 'https://' + 'STORAGE_ACCOUNT' + '.queue.core.windows.net';
|
||||
var queueService = AzureStorage.createQueueServiceWithSas(queueUri, 'SAS_TOKEN');
|
||||
</pre>
|
||||
<p>
|
||||
In Azure Storage JavaScript Client Library, a global variable <code>AzureStorage</code> is the start point where we can create service objects for blob/table/queue/file and access to the storage utilities.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>How to get full detailed API definitions? </b> Currently, the JavaScript Client Library shares the same API definitions with Node.js SDK.
|
||||
Please check API details on <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js API reference documents</a>. The JavaScript global variable <code>AzureStorage</code> is just like the object <code>require('azure-storage')</code> returns in Node.js.
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Warning</b>: Azure Storage JavaScript Client Library also supports creating <code>QueueService</code> based on Storage Account Key for authentication besides SAS Token.
|
||||
However, for security concerns, we recommend use of a limited time SAS Token, generated by a backend web server using a <a href="https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1">Stored Access Policy</a>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="step4">Step 4: Queue Operations</h2>
|
||||
<p>
|
||||
Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account.
|
||||
</p>
|
||||
|
||||
<h3>List Queues</h3>
|
||||
<p><code>QueueService</code> provides <code>listQueuesSegmented</code> and <code>listQueuesSegmentedWithPrefix</code> for retrieving the queue list under your storage account.</p>
|
||||
<pre>
|
||||
queueService.listQueuesSegmented(null, function (error, results) {
|
||||
if (error) {
|
||||
// List queue error
|
||||
} else {
|
||||
for (var i = 0, queue; queue = results.entries[i]; i++) {
|
||||
// Deal with queue object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create Queue</h3>
|
||||
<p><code>QueueService</code> provides <code>createQueue</code> and <code>createQueueIfNotExists</code> for creating a queue under a storage account.</p>
|
||||
<pre>
|
||||
queueService.createQueueIfNotExists('myqueue', function(error, result) {
|
||||
if (error) {
|
||||
// Create queue error
|
||||
} else {
|
||||
// Create queue successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete Queue</h3>
|
||||
<p><code>QueueService</code> provides <code>deleteQueue</code> and <code>deleteQueueIfExists</code> for deleting a queue under a storage account.</p>
|
||||
<pre>
|
||||
queueService.deleteQueueIfExists('myqueue', function(error, result) {
|
||||
if (error) {
|
||||
// Delete queue error
|
||||
} else {
|
||||
// Delete queue successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3> Executable Example </h3>
|
||||
<p>The sample will try to create an Azure Storage queue service object based on SAS Token authorization. Enter your Azure Storage account name and SAS Token here. Make sure you have set the CORS rules for the Azure Storage queue service, and the SAS Token is in valid period.</p>
|
||||
<label><b>Storage account:</b> </label> <input type="text" id="account"/>
|
||||
<label><b>SAS Token:</b> </label> <input type="text" id="sas"/>
|
||||
<p> Azure Storage queue service provides plenty of interfaces for queue operations. In following example, you can try to list all the queues under your storage account, and try to create or delete one queue from your account.</p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshQueueList()">ListQueues</button> button to view the queue list under your Azure Storage account</p></li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="createQueue()">CreateQueue</button> button to create a queue under your Azure Storage account:</p>
|
||||
<p><label><b>New Queue Name:</b> </label> <input type="text" value="myqueue" id="newqueue"/> </p>
|
||||
</li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the queue under your Azure Storage account</p></li>
|
||||
<li><p> Click "<b>Select</b>" button to select a queue and operate with the queue messages in next step</p></li>
|
||||
</ul>
|
||||
<div id="queues"></div>
|
||||
|
||||
<h2 id="step5">Step 5: Message Operations</h2>
|
||||
<p> A storage <b>Message</b>, in any format, of up to 64 KB. The maximum time that a message can remain in the queue is 7 days.</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>Note: </b> Azure Storage JavaScript Client Library provides <code>var encoder = new AzureStorage.QueueMessageEncoder.TextBase64QueueMessageEncoder()</code> which is a Base64 encoder and docoder.
|
||||
If a message content string is encoded with <code>encoder.encode()</code>, remember to decode it with <code>encoder.decode()</code> after peek the message.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3>Peek Messages</h3>
|
||||
<p><code>QueueService</code> provides <code>peekMessage</code> and <code>peekMessages</code> for retrieving the messages list under a queue.</p>
|
||||
<pre>
|
||||
queueService.peekMessages('myqueue', {numOfMessages: 32}, function (error, results) {
|
||||
if (error) {
|
||||
// Peek messages error
|
||||
} else {
|
||||
for (var i = 0, message; message = results[i]; i++) {
|
||||
// Deal with message object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create Message</h3>
|
||||
<p><code>QueueService</code> provides <code>createMessage</code> for creating a new message to a queue.</p>
|
||||
<pre>
|
||||
var encoder = new AzureStorage.QueueMessageEncoder.TextBase64QueueMessageEncoder();
|
||||
queueService.createMessage('myqueue', encoder.encode('mymessage'), function (error, results, response) {
|
||||
if (error) {
|
||||
// Create message error
|
||||
} else {
|
||||
// Create message successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Dequeue Message</h3>
|
||||
<p><code>QueueService</code> provides <code>getMessages</code> and <code>deleteMessage</code> for dequeuing next message in a queue.</p>
|
||||
<pre>
|
||||
queueService.getMessages('myqueue', function(error, result, response) {
|
||||
if(!error){
|
||||
// Message text is in messages[0].messageText
|
||||
var message = result[0];
|
||||
queueService.deleteMessage('myqueue', message.messageId, message.popReceipt, function(error, response){
|
||||
if(!error){
|
||||
//message deleted
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Update Message</h3>
|
||||
<p><code>QueueService</code> provides <code>getMessages</code> and <code>updateMessage</code> for updating next message in a queue.</p>
|
||||
<pre>
|
||||
var encoder = new AzureStorage.QueueMessageEncoder.TextBase64QueueMessageEncoder();
|
||||
queueService.getMessages('myqueue', function(error, result, response) {
|
||||
if(!error){
|
||||
// Got the message
|
||||
var message = result[0];
|
||||
queueService.updateMessage('myqueue', message.messageId, message.popReceipt, 10, {messageText: encoder.encode('new text')}, function(error, result, response){
|
||||
if(!error){
|
||||
// Message updated successfully
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3> Executable Example </h3>
|
||||
<p>After clicked the "<b>Select</b>" button on the queue list in last step, you are able to operate with the queue messages under the selected queue.<p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshMessageList()">PeekMessages</button> button to refresh the message list in your selected queue</p></li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="addMessage()">AddMessage</button> button to create a message in your selected queue: </p>
|
||||
<p><label><b>New Message Content:</b> </label> <input type="text" value="Message Content" id="newmessage"/> </p>
|
||||
</li>
|
||||
<li><p> Click <button class="btn btn-xs btn-success" onclick="update()">Update</button> button to update the top queue message in your selected queue (Dequeued messages will be invisible for 30s by default.):</p>
|
||||
<p><label><b>Update Message Content:</b> </label> <input type="text" value="Updated Message Content" id="updatemessage"/> </p>
|
||||
</li>
|
||||
<li><p> Click <button class="btn btn-xs btn-danger" onclick="dequeue()">Dequeue</button> button to dequeue the top queue message in your selected queue:</p></li>
|
||||
</ul>
|
||||
|
||||
<div id="result"></div>
|
||||
|
||||
<h2 id="step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</h2>
|
||||
<ul>
|
||||
<li>1. Setting CORS rules for your selected Azure-Storage account queue service.</li>
|
||||
<li>2. Including "azure-storage.common.js" in the html file.</li>
|
||||
<li>3. Including functional file(s) needed, such as "azure-storage.queue.js" for queue operation.</li>
|
||||
<li>4. Using keyword "AzureStorage" to access to Azure storage JavaScript APIs.</li>
|
||||
<li>5. Referring to <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js SDK documents</a> for detailed API definitions which keep same with JavaScript APIs.</li>
|
||||
</ul>
|
||||
<p> You can view the source code of this sample for detailed reference. </p>
|
||||
</div>
|
||||
|
||||
<!-- azure-storage.common.js also exports Node.js module stream, util and buffer -->
|
||||
<script src="../bundle/azure-storage.common.js"></script>
|
||||
<script src="../bundle/azure-storage.queue.js"></script>
|
||||
<script>
|
||||
var account = document.getElementById('account').value;
|
||||
var sas = document.getElementById('sas').value;
|
||||
var queue = '';
|
||||
var queueUri = '';
|
||||
var encoder = new AzureStorage.QueueMessageEncoder.TextBase64QueueMessageEncoder();
|
||||
|
||||
function checkParameters() {
|
||||
account = document.getElementById('account').value;
|
||||
sas = document.getElementById('sas').value;
|
||||
|
||||
if (account == null || account.length < 1)
|
||||
{
|
||||
alert('Please enter a valid storage account name!');
|
||||
return false;
|
||||
}
|
||||
if (sas == null || sas.length < 1)
|
||||
{
|
||||
alert('Please enter a valid SAS Token!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getQueueService() {
|
||||
if (!checkParameters())
|
||||
return null;
|
||||
|
||||
queueUri = 'https://' + account + '.queue.core.windows.net';
|
||||
var queueService = AzureStorage.createQueueServiceWithSas(queueUri, sas).withFilter(new AzureStorage.ExponentialRetryPolicyFilter());
|
||||
return queueService;
|
||||
}
|
||||
|
||||
function refreshQueueList()
|
||||
{
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
document.getElementById('queues').innerHTML = 'Loading queue list...';
|
||||
queueService.listQueuesSegmented(null, {maxResults : 200}, function(error, results) {
|
||||
if (error) {
|
||||
alert('List queue list error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>QueueName</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
|
||||
for (var i = 0, queue; queue = results.entries[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', queue.name, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteQueue(\'', queue.name ,'\')">Delete</button> ',
|
||||
'<button class="btn btn-xs btn-success" onclick="viewQueue(\'', queue.name ,'\')">Select</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('queues').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createQueue() {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
var queue = document.getElementById('newqueue').value;
|
||||
if (!AzureStorage.Validate.queueNameIsValid(queue, function(err, res){})) {
|
||||
alert('Invalid queue name!');
|
||||
return;
|
||||
}
|
||||
queueService.createQueueIfNotExists(queue.toLowerCase(), function(error, result) {
|
||||
if (error) {
|
||||
alert('Create queue failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Create ' + queue + ' successfully!');
|
||||
refreshQueueList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteQueue(queue) {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
queueService.deleteQueueIfExists(queue, function(error, result) {
|
||||
if (error) {
|
||||
alert('Delete queue failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete ' + queue + ' successfully!');
|
||||
refreshQueueList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function viewQueue(selectedQueue) {
|
||||
queue = selectedQueue;
|
||||
alert('Selected ' + queue + ' !');
|
||||
refreshMessageList();
|
||||
}
|
||||
|
||||
function refreshMessageList() {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
if (queue == null || queue.length < 1) {
|
||||
alert('Please select a queue from queue list!')
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('result').innerHTML = 'Loading queue messages...';
|
||||
queueService.peekMessages(queue, {numOfMessages: 32}, function (error, results) {
|
||||
if (error) {
|
||||
alert('List queue messages error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>MessageId</th>',
|
||||
'<th>MessageContent</th>',
|
||||
'<th>DequeueCount</th>',
|
||||
'<th>InsertionTime</th>',
|
||||
'</tr>');
|
||||
if (results.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, message; message = results[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', message.messageId, '</td>',
|
||||
'<td>', encoder.decode(message.messageText), '</td>',
|
||||
'<td>', message.dequeueCount, '</td>',
|
||||
'<td>', message.insertionTime, '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('result').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addMessage() {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
if (queue == null || queue.length < 1) {
|
||||
alert('Please select a queue from queue list!')
|
||||
return;
|
||||
}
|
||||
|
||||
var message = document.getElementById('newmessage').value;
|
||||
queueService.createMessage(queue, encoder.encode(message), function(error, result, response) {
|
||||
if(error) {
|
||||
alert('Create messages error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Create message successfully!');
|
||||
refreshMessageList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function dequeue() {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
if (queue == null || queue.length < 1) {
|
||||
alert('Please select a queue from queue list!')
|
||||
return;
|
||||
}
|
||||
|
||||
queueService.getMessages(queue, function(error, result, response) {
|
||||
if(!error) {
|
||||
if (result.length < 1)
|
||||
return;
|
||||
|
||||
var message = result[0];
|
||||
queueService.deleteMessage(queue, message.messageId, message.popReceipt, function(error, response) {
|
||||
if(error) {
|
||||
alert('Delete message error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete message successfully!');
|
||||
refreshMessageList();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function update() {
|
||||
var queueService = getQueueService();
|
||||
if (!queueService)
|
||||
return;
|
||||
|
||||
if (queue == null || queue.length < 1) {
|
||||
alert('Please select a queue from queue list!')
|
||||
return;
|
||||
}
|
||||
|
||||
var updateMessage = document.getElementById("updatemessage").value;
|
||||
queueService.getMessages(queue, function(error, result, response) {
|
||||
if(!error) {
|
||||
if (result.length < 1)
|
||||
return;
|
||||
|
||||
var message = result[0];
|
||||
queueService.updateMessage(queue, message.messageId, message.popReceipt, 10, {messageText: encoder.encode(updateMessage)}, function(error, result, response){
|
||||
if(error) {
|
||||
alert('Update message error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Update message successfully!');
|
||||
refreshMessageList();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+440
@@ -0,0 +1,440 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>Azure Storage JavaScript Client Library Sample for Table Operations</title>
|
||||
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="mt-1">
|
||||
<h1>Azure Storage JavaScript Client Library Sample for Table Operations</h1>
|
||||
</div>
|
||||
<p class="lead">In this sample, we will demonstrate common scenarios for Azure Table Storage that includes creating, listing and deleting tables and entities.</p>
|
||||
<hr/>
|
||||
|
||||
<p>Azure Storage table is a service that stores structured NoSQL data in the cloud. Table storage is a key/attribute store with a schemaless design. Because Table storage is schemaless, it's easy to adapt your data as the needs of your application evolve. Access to data is fast and cost-effective for all kinds of applications. Table storage is typically significantly lower in cost than traditional SQL for similar volumes of data.
|
||||
</p>
|
||||
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Note</b>: You may need set up a HTTP server to host this sample for IE browser, because IndexedDB is only available on websites with http or https URL schemes in IE. Azure Storage JavaScript Client Library currently depends on IndexedDB.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Contents:</h2>
|
||||
<ul>
|
||||
<li><a href="#step1">Step 1: Preparing an Azure Storage account with CORS rules set</a></li>
|
||||
<li><a href="#step2">Step 2: Importing Azure Storage JavaScript Client Library</a></li>
|
||||
<li><a href="#step3">Step 3: Creating an Azure Storage Table Service Object</a></li>
|
||||
<li><a href="#step4">Step 4: Table Operations</a></li>
|
||||
<li><a href="#step5">Step 5: Table Entities Operations</a></li>
|
||||
<li><a href="#step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</a></li>
|
||||
</ul>
|
||||
|
||||
<h2 id="step1">Step 1: Preparing an Azure Storage account with CORS rules set</h2>
|
||||
<p>Cross-origin resource sharing, or CORS, must be configured on the Azure Storage account to be accessed directly from JavaScript in the browser.
|
||||
You are able to set the CORS rules for specific Azure Storage account on the <a href="https://portal.azure.com">Azure Portal</a>.
|
||||
The "Allowed origins" could be set to "*" to allow all the origins in this sample.
|
||||
For more information about CORS, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services">Cross-Origin Resource Sharing (CORS)</a>.</p>
|
||||
<img src="cors.PNG"/>
|
||||
|
||||
<h2 id="step2">Step 2: Importing Azure Storage JavaScript Client Library</h2>
|
||||
<p>
|
||||
Importing <code>azure-storage.common.js</code> and <code>azure-storage.table.js</code> in your HTML file for table operations, and make sure <code>azure-storage.common.js</code> is in front of <code>azure-storage.table.js</code>.
|
||||
<p>
|
||||
<pre>
|
||||
<script src="azure-storage.common.js"></script>
|
||||
<script src="azure-storage.table.js"></script>
|
||||
</pre>
|
||||
|
||||
<h2 id="step3">Step 3: Creating an Azure Storage Table Service Object</h2>
|
||||
<p>
|
||||
The <code>TableService</code> object lets you work with table and entities.
|
||||
Following code creates a <code>TableService</code> object with storage account and SAS Token.
|
||||
</p>
|
||||
<pre>
|
||||
var tableUri = 'https://' + 'STORAGE_ACCOUNT' + '.table.core.windows.net';
|
||||
var tableService = AzureStorage.createTableServiceWithSas(tableUri, 'SAS_TOKEN');
|
||||
</pre>
|
||||
<p>
|
||||
In Azure Storage JavaScript Client Library, a global variable <code>AzureStorage</code> is the start point where we can create service objects for blob/table/queue/file and access to the storage utilities.
|
||||
</p>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-body">
|
||||
<b>How to get full detailed API definitions? </b> Currently, the JavaScript Client Library shares the same API definitions with Node.js SDK.
|
||||
Please check API details on <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js API reference documents</a>. The JavaScript global variable <code>AzureStorage</code> is just like the object <code>require('azure-storage')</code> returns in Node.js.
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-body">
|
||||
<b>Warning</b>: Azure Storage JavaScript Client Library also supports creating <code>TableService</code> based on Storage Account Key for authentication besides SAS Token.
|
||||
However, for security concerns, we recommend use of a limited time SAS Token, generated by a backend web server using a <a href="https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1">Stored Access Policy</a>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="step4">Step 4: Table Operations</h2>
|
||||
<p>
|
||||
<b>Table</b>: A table is a collection of entities. Tables don't enforce a schema on entities, which means a single table can contain entities that have different sets of properties. The number of tables that a storage account can contain is limited only by the storage account capacity limit.
|
||||
</p>
|
||||
|
||||
<h3>List Tables</h3>
|
||||
<p><code>TableService</code> provides <code>listTablesSegmented</code> and <code>listTablesSegmentedWithPrefix</code> for retrieving the table list under your storage account.</p>
|
||||
<pre>
|
||||
tableService.listTablesSegmented(null, {maxResults : 200}, function (error, results) {
|
||||
if (error) {
|
||||
// List tables error
|
||||
} else {
|
||||
for (var i = 0, table; table = results.entries[i]; i++) {
|
||||
// Deal with table object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Create Table</h3>
|
||||
<p><code>TableService</code> provides <code>createTable</code> and <code>createTableIfNotExists</code> for creating a table under a storage account.</p>
|
||||
<pre>
|
||||
tableService.createTableIfNotExists('mytable', function(error, result) {
|
||||
if (error) {
|
||||
// Create table error
|
||||
} else {
|
||||
// Create table successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete Table</h3>
|
||||
<p><code>TableService</code> provides <code>deleteTable</code> and <code>deleteTableIfExists</code> for deleting a table under a storage account.</p>
|
||||
<pre>
|
||||
tableService.deleteTableIfExists('mytable', function(error, result) {
|
||||
if (error) {
|
||||
// Delete table error
|
||||
} else {
|
||||
// Delete table successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3> Executable Example </h3>
|
||||
<p>The sample will try to create an Azure Storage table service object based on SAS Token authorization. Enter your Azure Storage account name and SAS Token here. Make sure you have set the CORS rules for the Azure Storage table service, and the SAS Token is in valid period.</p>
|
||||
<label><b>Storage account:</b> </label> <input type="text" id="account"/>
|
||||
<label><b>SAS Token:</b> </label> <input type="text" id="sas"/>
|
||||
<p> Azure Storage table service provides plenty of interfaces for table operations. In following example, you can try to list all the tables under your storage account, and try to create or delete one table from your account.</p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshTableList()">ListTables</button> button to view the table list under your Azure Storage account</p></li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="createTable()">CreateTable</button> button to create a table under your Azure Storage account</p>
|
||||
<p><label><b>New Table Name:</b> </label> <input type="text" value="mytable" id="newtable"/> </p>
|
||||
</li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the table under your Azure Storage account</p></li>
|
||||
<li><p> Click "<b>Select</b>" button to operate with the table entities in next step</p></li>
|
||||
</ul>
|
||||
<div id="tables"></div>
|
||||
|
||||
<h2 id="step5">Step 5: Table Entities Operations</h2>
|
||||
<p><b>Entity</b>: An entity is a set of properties, similar to a database row. An entity can be up to 1MB in size.</p>
|
||||
<p><b>Properties</b>: A property is a name-value pair. Each entity can include up to 252 properties to store data. Each entity also has 3 system properties that specify a partition key, a row key, and a timestamp. Entities with the same partition key can be queried more quickly, and inserted/updated in atomic operations. An entity's row key is its unique identifier within a partition.</p>
|
||||
|
||||
<h3>Query Entities</h3>
|
||||
<p><code>TableService</code> provides <code>queryEntities</code> for querying a table under a storage account.</p>
|
||||
<pre>
|
||||
var tableQuery = new AzureStorage.TableQuery().top(200);
|
||||
tableService.queryEntities('mytable', tableQuery, null, function(error, result) {
|
||||
if (error) {
|
||||
// Query entities error
|
||||
} else {
|
||||
for (var i = 0, entity; entity = results.entries[i]; i++) {
|
||||
// Deal with entity object
|
||||
}
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Insert or Replace Entity</h3>
|
||||
<p><code>TableService</code> provides <code>insertEntity</code>, <code>insertOrReplaceEntity</code> and <code>insertOrMergeEntity</code> for adding a table entity under a storage account.</p>
|
||||
<pre>
|
||||
var insertEntity = {
|
||||
PartitionKey: {'_': 'partitionKey'},
|
||||
RowKey: {'_': 'rowKey'}
|
||||
};
|
||||
|
||||
tableService.insertOrReplaceEntity('mytable', insertEntity, function(error, result, response) {
|
||||
if(error) {
|
||||
// Insert table entity error
|
||||
} else {
|
||||
// Insert table entity successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3>Delete Entity</h3>
|
||||
<p><code>TableService</code> provides <code>deleteEntity</code> for deleting a table entity under a storage account.</p>
|
||||
<pre>
|
||||
var deleteEntity = {
|
||||
PartitionKey: {'_': 'partitionKey'},
|
||||
RowKey: {'_': 'rowKey'}
|
||||
};
|
||||
|
||||
tableService.deleteEntity('mytable', deleteEntity, function(error, result, response) {
|
||||
if(error) {
|
||||
// Delete table entity error
|
||||
} else {
|
||||
// Delete table entity successfully
|
||||
}
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h3> Executable Example </h3>
|
||||
<p>After clicked the "<b>Select</b>" button on the table list, you are able to operate with the table entities under the selected table.<p>
|
||||
<ul>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshEntityList()">QueryEntities</button> button to refresh the entity list in your selected table</p></li>
|
||||
<li><p> Click <button class="btn btn-xs btn-primary" onclick="addEntity()">AddOrUpdateEntity</button> button to create an entity in your selected table. If existing entity with the sampe PartitionKey and RowKey, old entity will be merged.</p>
|
||||
<p>
|
||||
<label><b>PK</b> </label> <input type="text" value="PartitionKey" id="pk"/>
|
||||
<label><b>RK</b> </label> <input type="text" value="RowKey" id="rk"/>
|
||||
<label><b>CustomProperty1</b> </label> <input type="text" value="Custom Property 1" id="cp1"/>
|
||||
<label><b>CustomProperty2</b> </label> <input type="text" value="Custom Property 2" id="cp2"/>
|
||||
</p>
|
||||
</li>
|
||||
<li><p> Click "<b>Delete</b>" button to delete the selected table entity in your selected table</p></li>
|
||||
</ul>
|
||||
<div id="result"></div>
|
||||
|
||||
<h3 id="step6">Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library</h3>
|
||||
<ul>
|
||||
<li>1. Setting CORS rules for your selected Azure-Storage account table service.</li>
|
||||
<li>2. Including "azure-storage.common.js" in the html file.</li>
|
||||
<li>3. Including functional file(s) needed, such as "azure-storage.table.js" for table operation.</li>
|
||||
<li>4. Using keyword "AzureStorage" to access to Azure storage JavaScript APIs.</li>
|
||||
<li>5. Referring to <a href="http://azure.github.io/azure-storage-node/">Azure Storage Node.js SDK documents</a> for detailed API definitions which keep same with JavaScript APIs.</li>
|
||||
</ul>
|
||||
<p> You can view the source code of this sample for detailed reference. </p>
|
||||
</div>
|
||||
|
||||
<!-- azure-storage.common.js also exports Node.js module stream, util and buffer -->
|
||||
<script src="../bundle/azure-storage.common.js"></script>
|
||||
<script src="../bundle/azure-storage.table.js"></script>
|
||||
|
||||
<script>
|
||||
var account = document.getElementById('account').value;
|
||||
var sas = document.getElementById('sas').value;
|
||||
var table = '';
|
||||
var tableUri = '';
|
||||
|
||||
function checkParameters() {
|
||||
account = document.getElementById('account').value;
|
||||
sas = document.getElementById('sas').value;
|
||||
|
||||
if (account == null || account.length < 1)
|
||||
{
|
||||
alert('Please enter a valid storage account name!');
|
||||
return false;
|
||||
}
|
||||
if (sas == null || sas.length < 1)
|
||||
{
|
||||
alert('Please enter a valid SAS Token!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getTableService() {
|
||||
if (!checkParameters())
|
||||
return null;
|
||||
|
||||
tableUri = 'https://' + account + '.table.core.windows.net';
|
||||
var tableService = AzureStorage.createTableServiceWithSas(tableUri, sas).withFilter(new AzureStorage.ExponentialRetryPolicyFilter());
|
||||
return tableService;
|
||||
}
|
||||
|
||||
function refreshTableList()
|
||||
{
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
document.getElementById('tables').innerHTML = 'Loading table list...';
|
||||
tableService.listTablesSegmented(null, {maxResults : 200}, function(error, results) {
|
||||
if (error) {
|
||||
alert('List table list error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>TableName</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, table; table = results.entries[i]; i++) {
|
||||
output.push('<tr>',
|
||||
'<td>', table, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteTable(\'', table ,'\')">Delete</button> ',
|
||||
'<button class="btn btn-xs btn-success" onclick="viewTable(\'', table ,'\')">Select</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('tables').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createTable() {
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
var table = document.getElementById('newtable').value;
|
||||
if (!AzureStorage.Validate.tableNameIsValid(table, function(err, res){})) {
|
||||
alert('Invalid table name!');
|
||||
return;
|
||||
}
|
||||
tableService.createTableIfNotExists(table.toLowerCase(), function(error, result) {
|
||||
if (error) {
|
||||
alert('Create table failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Create ' + table + ' successfully!');
|
||||
refreshTableList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteTable(table) {
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
tableService.deleteTableIfExists(table, function(error, result) {
|
||||
if (error) {
|
||||
alert('Delete table failed, open brower console for more detailed info.');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete ' + table + ' successfully!');
|
||||
refreshTableList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function viewTable(selectedTable) {
|
||||
table = selectedTable;
|
||||
alert('Selected ' + table + ' !');
|
||||
refreshEntityList();
|
||||
}
|
||||
|
||||
function refreshEntityList() {
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
if (table == null || table.length < 1) {
|
||||
alert('Please select a table from table list!')
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('result').innerHTML = 'Loading table entities...';
|
||||
var tableQuery = new AzureStorage.TableQuery().top(200);
|
||||
tableService.queryEntities(table, tableQuery, null, function(error, results) {
|
||||
if (error) {
|
||||
alert('List table entities error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
var output = [];
|
||||
output.push('<tr>',
|
||||
'<th>PartitionKey</th>',
|
||||
'<th>RowKey</th>',
|
||||
'<th>Custom Property 1</th>',
|
||||
'<th>Custom Property 2</th>',
|
||||
'<th>Timestamp</th>',
|
||||
'<th>Operations</th>',
|
||||
'</tr>');
|
||||
if (results.entries.length < 1) {
|
||||
output.push('<tr><td>Empty results...</td></tr>');
|
||||
}
|
||||
for (var i = 0, entity; entity = results.entries[i]; i++) {
|
||||
var cp1 = '';
|
||||
var cp2 = '';
|
||||
|
||||
if (typeof entity.CustomProperty1 !== 'undefined') {
|
||||
cp1 = entity.CustomProperty1._;
|
||||
}
|
||||
if (typeof entity.CustomProperty2 !== 'undefined') {
|
||||
cp2 = entity.CustomProperty2._;
|
||||
}
|
||||
|
||||
output.push('<tr>',
|
||||
'<td>', entity.PartitionKey._, '</td>',
|
||||
'<td>', entity.RowKey._, '</td>',
|
||||
'<td>', cp1, '</td>',
|
||||
'<td>', cp2, '</td>',
|
||||
'<td>', entity.Timestamp._, '</td>',
|
||||
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteEntity(\'', entity.PartitionKey._ ,'\'', ',', '\'', entity.RowKey._ ,'\'',')">Delete</button>', '</td>',
|
||||
'</tr>');
|
||||
}
|
||||
document.getElementById('result').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addEntity() {
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
if (table == null || table.length < 1) {
|
||||
alert('Invalid table name!')
|
||||
return;
|
||||
}
|
||||
|
||||
var partitionKey = document.getElementById('pk').value;
|
||||
var rowKey = document.getElementById('rk').value;
|
||||
var customProperty1 = document.getElementById('cp1').value;
|
||||
var customProperty2 = document.getElementById('cp2').value;
|
||||
var insertEntity = {
|
||||
PartitionKey: {'_': partitionKey},
|
||||
RowKey: {'_': rowKey},
|
||||
CustomProperty1: {'_': customProperty1},
|
||||
CustomProperty2: {'_': customProperty2}
|
||||
};
|
||||
|
||||
tableService.insertOrMergeEntity(table, insertEntity, function(error, result, response) {
|
||||
if(error) {
|
||||
alert('Insert table entity error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Insert table entity successfully!');
|
||||
refreshEntityList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteEntity(partitionKey, rowKey) {
|
||||
var tableService = getTableService();
|
||||
if (!tableService)
|
||||
return;
|
||||
|
||||
if (table == null || table.length < 1) {
|
||||
alert('Invalid table name!')
|
||||
return;
|
||||
}
|
||||
|
||||
var deleteEntity = {
|
||||
PartitionKey: {'_': partitionKey},
|
||||
RowKey: {'_': rowKey}
|
||||
};
|
||||
|
||||
tableService.deleteEntity(table, deleteEntity, function(error, result, response) {
|
||||
if(error) {
|
||||
alert('Delete table entity error, please open browser console to view detailed error');
|
||||
console.log(error);
|
||||
} else {
|
||||
alert('Delete table entity successfully!');
|
||||
refreshEntityList();
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+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();
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
module.exports = function(grunt) {
|
||||
//init stuff
|
||||
grunt.initConfig({
|
||||
|
||||
nsp: {
|
||||
package: grunt.file.readJSON('package.json')
|
||||
},
|
||||
|
||||
mochaTest: {
|
||||
test: {
|
||||
options: {
|
||||
reporter: 'spec',
|
||||
quiet: false,
|
||||
clearRequireCache: false,
|
||||
timeout: 100000
|
||||
},
|
||||
src: ['test/**/*.js']
|
||||
}
|
||||
},
|
||||
|
||||
//jsdoc config
|
||||
jsdoc: {
|
||||
dist: {
|
||||
src: [
|
||||
'README.md',
|
||||
'lib/azure-storage.js',
|
||||
'lib/common/filters/retrypolicyfilter.js',
|
||||
'lib/common/filters/linearretrypolicyfilter.js',
|
||||
'lib/common/filters/exponentialretrypolicyfilter.js',
|
||||
'lib/common/services/storageutilities.js',
|
||||
'lib/services/blob/blobservice.core.js',
|
||||
'lib/services/blob/blobservice.node.js',
|
||||
'lib/services/blob/blobservice.browser.js',
|
||||
'lib/services/blob/models/blobresult.js',
|
||||
'lib/services/blob/models/containerresult.js',
|
||||
'lib/services/blob/models/leaseresult.js',
|
||||
'lib/services/blob/blobutilities.js',
|
||||
'lib/services/queue/queueservice.js',
|
||||
'lib/services/queue/queuemessageencoder.js',
|
||||
'lib/services/queue/queueutilities.js',
|
||||
'lib/services/queue/models/queueresult.js',
|
||||
'lib/services/queue/models/queuemessageresult.js',
|
||||
'lib/services/table/tableservice.js',
|
||||
'lib/services/table/tablebatch.js',
|
||||
'lib/services/table/tablequery.js',
|
||||
'lib/services/table/tableutilities.js',
|
||||
'lib/services/file/fileservice.core.js',
|
||||
'lib/services/file/fileservice.node.js',
|
||||
'lib/services/file/fileservice.browser.js',
|
||||
'lib/services/file/fileutilities.js',
|
||||
'lib/services/file/models/shareresult.js',
|
||||
'lib/services/file/models/directoryresult.js',
|
||||
'lib/services/file/models/fileresult.js',
|
||||
'lib/common/services/storageserviceclient.js',
|
||||
'lib/common/diagnostics/logger.js'
|
||||
],
|
||||
options: {
|
||||
destination: 'docs',
|
||||
template: 'node_modules/ink-docstrap/template',
|
||||
configure: 'jsdoc/jsdoc.json'
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// devserver config
|
||||
devserver: {
|
||||
server : {},
|
||||
options: {
|
||||
'base': 'docs'
|
||||
}
|
||||
},
|
||||
|
||||
jshint: {
|
||||
all: ['Gruntfile.js', 'lib/**/*.js'],
|
||||
options: {
|
||||
jshintrc: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
grunt.loadNpmTasks('grunt-jsdoc');
|
||||
grunt.loadNpmTasks('grunt-mocha-test');
|
||||
grunt.loadNpmTasks('grunt-devserver');
|
||||
grunt.loadNpmTasks('grunt-nsp');
|
||||
grunt.loadNpmTasks('grunt-contrib-jshint');
|
||||
|
||||
grunt.registerTask('doc', ['jsdoc', 'devserver']);
|
||||
grunt.registerTask('validate', ['jshint', 'nsp']);
|
||||
grunt.registerTask('default', ['validate', 'mochaTest']);
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"tags" : {
|
||||
"allowUnknownTags" : true
|
||||
},
|
||||
"plugins" : ["plugins/markdown"],
|
||||
|
||||
"templates" : {
|
||||
"cleverLinks" : false,
|
||||
"monospaceLinks" : false,
|
||||
"dateFormat" : "ddd MMM Do YYYY",
|
||||
"outputSourceFiles" : true,
|
||||
"outputSourcePath" : true,
|
||||
"systemName" : "Microsoft Azure Storage Client Library for node.js",
|
||||
"footer" : "",
|
||||
"navType" : "vertical",
|
||||
"theme" : "cosmo",
|
||||
"linenums" : true,
|
||||
"collapseSymbols" : false,
|
||||
"inverseNav" : true,
|
||||
"highlightTutorialCode" : false,
|
||||
"protocol": "fred://"
|
||||
},
|
||||
"markdown" : {
|
||||
"parser" : "gfm",
|
||||
"hardwrap" : true
|
||||
}
|
||||
}
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var exports = module.exports;
|
||||
|
||||
/**
|
||||
* Creates a connection string that can be used to create a service which runs on the storage emulator. The emulator must be downloaded separately.
|
||||
*
|
||||
* @param {string} [proxyUri] The proxyUri. By default, http://127.0.0.1
|
||||
* @return {string} A connection string representing the development storage credentials.
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var devStoreCreds = azure.generateDevelopmentStorageCredentials();
|
||||
* var blobService = azure.createBlobService(devStoreCreds);
|
||||
*/
|
||||
exports.generateDevelopmentStorageCredentials = function (proxyUri) {
|
||||
var devStore = 'UseDevelopmentStorage=true;';
|
||||
if(proxyUri){
|
||||
devStore += 'DevelopmentStorageProxyUri=' + proxyUri;
|
||||
}
|
||||
|
||||
return devStore;
|
||||
};
|
||||
|
||||
/**
|
||||
* Table client exports.
|
||||
* @ignore
|
||||
*/
|
||||
var TableService = require('./services/table/tableservice');
|
||||
|
||||
exports.TableService = TableService;
|
||||
exports.TableQuery = require('./services/table/tablequery');
|
||||
exports.TableBatch = require('./services/table/tablebatch');
|
||||
exports.TableUtilities = require('./services/table/tableutilities');
|
||||
|
||||
/**
|
||||
* Creates a new {@link TableService} object.
|
||||
* If no storageaccount or storageaccesskey are provided, the AZURE_STORAGE_CONNECTION_STRING and then the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY
|
||||
* environment variables will be used.
|
||||
*
|
||||
* @param {string} [storageAccountOrConnectionString] The storage account or the connection string.
|
||||
* @param {string} [storageAccessKey] The storage access key.
|
||||
* @param {string|object} [host] The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @return {TableService} A new TableService object.
|
||||
*
|
||||
*/
|
||||
exports.createTableService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new TableService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link TableService} object using the host Uri and the SAS credentials provided.
|
||||
*
|
||||
* @param {string|object} host The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @param {string} sasToken The Shared Access Signature token.
|
||||
* @return {TableService} A new TableService object with the SAS credentials.
|
||||
*/
|
||||
exports.createTableServiceWithSas = function (hostUri, sasToken) {
|
||||
return new TableService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
/**
|
||||
* Blob client exports.
|
||||
* @ignore
|
||||
*/
|
||||
var BlobService = require('./services/blob/blobservice.node');
|
||||
|
||||
exports.BlobService = BlobService;
|
||||
exports.BlobUtilities = require('./services/blob/blobutilities');
|
||||
|
||||
/**
|
||||
* Creates a new {@link BlobService} object.
|
||||
* If no storageaccount or storageaccesskey are provided, the AZURE_STORAGE_CONNECTION_STRING and then the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY
|
||||
* environment variables will be used.
|
||||
*
|
||||
* @param {string} storageAccountOrConnectionString The storage account or the connection string.
|
||||
* @param {string} [storageAccessKey] The storage access key.
|
||||
* @param {string|object} [host] The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @return {BlobService} A new BlobService object.
|
||||
*/
|
||||
exports.createBlobService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new BlobService(storageAccountOrConnectionString, storageAccessKey, host, null);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link BlobService} object using the host Uri and the SAS credentials provided.
|
||||
*
|
||||
* @param {string|object} host The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @param {string} sasToken The Shared Access Signature token.
|
||||
* @return {BlobService} A new BlobService object with the SAS credentials.
|
||||
*/
|
||||
exports.createBlobServiceWithSas = function (host, sasToken) {
|
||||
return new BlobService(null, null, host, sasToken);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link BlobService} object using the host uri and anonymous access.
|
||||
*
|
||||
* @param {string|object} host The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @return {BlobService} A new BlobService object with the anonymous credentials.
|
||||
*/
|
||||
exports.createBlobServiceAnonymous = function (host) {
|
||||
return new BlobService(null, null, host, null);
|
||||
};
|
||||
|
||||
/**
|
||||
* File client exports.
|
||||
* @ignore
|
||||
*/
|
||||
var FileService = require('./services/file/fileservice.node');
|
||||
|
||||
exports.FileService = FileService;
|
||||
exports.FileUtilities = require('./services/file/fileutilities');
|
||||
|
||||
/**
|
||||
* Creates a new {@link FileService} object.
|
||||
* If no storageaccount or storageaccesskey are provided, the AZURE_STORAGE_CONNECTION_STRING and then the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY
|
||||
* environment variables will be used.
|
||||
*
|
||||
* @param {string} storageAccountOrConnectionString The storage account or the connection string.
|
||||
* @param {string} [storageAccessKey] The storage access key.
|
||||
* @param {string|object} [host] The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @return {FileService} A new FileService object.
|
||||
*/
|
||||
exports.createFileService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new FileService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link FileService} object using the host Uri and the SAS credentials provided.
|
||||
*
|
||||
* @param {string|object} host The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @param {string} sasToken The Shared Access Signature token.
|
||||
* @return {FileService} A new FileService object with the SAS credentials.
|
||||
*/
|
||||
exports.createFileServiceWithSas = function (hostUri, sasToken) {
|
||||
return new FileService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
/**
|
||||
* Queue client exports.
|
||||
* @ignore
|
||||
*/
|
||||
var QueueService = require('./services/queue/queueservice');
|
||||
|
||||
exports.QueueService = QueueService;
|
||||
exports.QueueUtilities = require('./services/queue/queueutilities');
|
||||
exports.QueueMessageEncoder = require('./services/queue/queuemessageencoder');
|
||||
|
||||
/**
|
||||
* Creates a new {@link QueueService} object.
|
||||
* If no storageaccount or storageaccesskey are provided, the AZURE_STORAGE_CONNECTION_STRING and then the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY
|
||||
* environment variables will be used.
|
||||
*
|
||||
* @param {string} [storageAccountOrConnectionString] The storage account or the connection string.
|
||||
* @param {string} [storageAccessKey] The storage access key.
|
||||
* @param {string|object} [host] The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @return {QueueService} A new QueueService object.
|
||||
*/
|
||||
exports.createQueueService = function (storageAccountOrConnectionString, storageAccessKey, host) {
|
||||
return new QueueService(storageAccountOrConnectionString, storageAccessKey, host);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link QueueService} object using the host Uri and the SAS credentials provided.
|
||||
*
|
||||
* @param {string|object} host The host address. To define primary only, pass a string.
|
||||
* Otherwise 'host.primaryHost' defines the primary host and 'host.secondaryHost' defines the secondary host.
|
||||
* @param {string} sasToken The Shared Access Signature token.
|
||||
* @return {QueueService} A new QueueService object with the SAS credentials.
|
||||
*/
|
||||
exports.createQueueServiceWithSas = function(hostUri, sasToken) {
|
||||
return new QueueService(null, null, hostUri, sasToken);
|
||||
};
|
||||
|
||||
/**
|
||||
* Account SAS
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
var azureCommon = require('./common/common.node');
|
||||
var StorageServiceClient = azureCommon.StorageServiceClient;
|
||||
var SharedKey = azureCommon.SharedKey;
|
||||
/**
|
||||
* Generates an account shared access signature token
|
||||
*
|
||||
* @param {string} [storageAccountOrConnectionString] The storage account or the connection string.
|
||||
* @param {string} [storageAccessKey] The storage access key.
|
||||
* @param {object} sharedAccessPolicy The shared access policy.
|
||||
* @param {SharedAccessServices} sharedAccessPolicy.AccessPolicy.Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.Services`.
|
||||
* @param {SharedAccessResourceTypes} sharedAccessPolicy.AccessPolicy.ResourceTypes The resource type for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.ResourceTypes`.
|
||||
* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permissions for a shared access signature.
|
||||
* Refer to `Constants.AccountSasConstants.Permissions`.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Start The time at which the Shared Access Signature becomes valid.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.IPAddressOrRange The permission type. Refer to `Constants.AccountSasConstants.ResourceTypes`.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.Protocols The possible protocols. Refer to `Constants.AccountSasConstants.ResourceTypes`.
|
||||
*/
|
||||
exports.generateAccountSharedAccessSignature = function(storageAccountOrConnectionString, storageAccessKey, sharedAccessAccountPolicy)
|
||||
{
|
||||
var storageSettings = StorageServiceClient.getStorageSettings(storageAccountOrConnectionString, storageAccessKey);
|
||||
var sharedKey = new SharedKey(storageSettings._name, storageSettings._key);
|
||||
|
||||
return sharedKey.generateAccountSignedQueryString(sharedAccessAccountPolicy);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A callback that returns a response object.
|
||||
* @callback errorOrResponse
|
||||
* @param {object} error If an error occurs, will contain information about the error.
|
||||
* @param {object} response Contains information about the response returned for the operation.
|
||||
* For example, HTTP status codes and headers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A callback that returns result and response objects.
|
||||
* @callback errorOrResult
|
||||
* @param {object} error If an error occurs, will contain information about the error.
|
||||
* @param {object} result The result of the operation.
|
||||
* @param {object} response Contains information about the response returned for the operation.
|
||||
* For example, HTTP status codes and headers.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Specifying conditional headers for blob service operations. See http://msdn.microsoft.com/en-us/library/dd179371.aspx for more information.
|
||||
* @typedef {object} AccessConditions
|
||||
* @property {string} EtagMatch If the ETag for the blob matches the specified ETag.
|
||||
* Specify the wildcard character (*) to perform the operation only if the resource does exist, and fail the operation if it does not exist.
|
||||
* @property {string} EtagNonMatch If the ETag for the blob does not match the specified ETag.
|
||||
* Specify the wildcard character (*) to perform the operation only if the resource does not exist, and fail the operation if it does exist.
|
||||
* @property {Date|string} DateModifedSince If the blob has been modified since the specified date.
|
||||
* @property {Date|string} DateUnModifiedSince If the blob has not been modified since the specified date.
|
||||
* @property {Number|string} SequenceNumberLessThanOrEqual If the blob's sequence number is less than or equal to the specified value.
|
||||
* For Put Page operation only. See https://msdn.microsoft.com/en-us/library/azure/ee691975.aspx for more information.
|
||||
* @property {Number|string} SequenceNumberLessThan If the blob's sequence number is less than the specified value.
|
||||
* For Put Page operation only. See https://msdn.microsoft.com/en-us/library/azure/ee691975.aspx for more information.
|
||||
* @property {Number|string} SequenceNumberEqual If the blob's sequence number is equal to the specified value.
|
||||
* For Put Page operation only. See https://msdn.microsoft.com/en-us/library/azure/ee691975.aspx for more information.
|
||||
* @property {Number|string} MaxBlobSize If the Append Block operation would cause the blob to exceed that limit or if the blob size is already greater than the specified value.
|
||||
* For Append Block operation only. See https://msdn.microsoft.com/en-us/library/mt427365.aspx for more information.
|
||||
* @property {Number|string} MaxAppendPosition If the append position is equal to the specified value.
|
||||
* For Append Block operation only. See https://msdn.microsoft.com/en-us/library/mt427365.aspx for more information.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The properties of a storage service, including properties of Storage Analytics and CORS (Cross-Origin Resource Sharing) rules.
|
||||
* @typedef {object} ServiceProperties
|
||||
* @property {string} DefaultServiceVersion The default version of Storage Analytics currently in use.
|
||||
* @property {LoggingProperties} Logging The Logging settings.
|
||||
* @property {MetricsProperties} HourMetrics The HourMetrics settings provide a summary of request statistics grouped by API in hourly aggregates.
|
||||
* @property {MetricsProperties} MinuteMetrics The HourMetrics settings provide request statistics grouped by API for each minute.
|
||||
* @property {object} Cors Groups all CORS rules.
|
||||
* @property {CorsRule[]} Cors.CorsRules Groups settings for a `[CORS rule]{@link CorsRule}`.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Azure Analytics logging settings.
|
||||
* @typedef {object} LoggingProperties
|
||||
* @property {string} Version The version of Storage Analytics currently in use for logging.
|
||||
* @property {boolean} Delete Indicates whether delete requests are being logged.
|
||||
* @property {boolean} Read Indicates whether read requests are being logged.
|
||||
* @property {boolean} Write Indicates whether write requests are being logged.
|
||||
* @property {RetentionPolicy} RetentionPolicy The retention policy of the log data.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The setting of Azure Analytics summary of request stastics.
|
||||
* @typedef {object} MetricsProperties
|
||||
* @property {string} Version The version of Storage Analytics currently in use for hour metrics.
|
||||
* @property {string} Enabled Indicates whether metrics are enabled
|
||||
* @property {boolean} IncludeAPIs Indicates whether metrics generate summary statistics for called API operations.
|
||||
* @property {RetentionPolicy} RetentionPolicy The retention policy of the metrics data.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The CORS rule of a storage service.
|
||||
* @typedef {object} CorsRule
|
||||
* @property {string[]} AllowedMethods A list of HTTP methods that are allowed to be executed by the origin. For Azure Storage, permitted methods are DELETE, GET, HEAD, MERGE, POST, OPTIONS or PUT.
|
||||
* @property {string[]} AllowedOrigins A list of origin domains that are allowed via CORS, or "*" if all domains are allowed.
|
||||
* @property {string[]} AllowedHeaders A list of headers allowed to be part of the cross-origin request.
|
||||
* @property {string[]} ExposedHeaders A list of response headers to expose to CORS clients.
|
||||
* @property {number} MaxAgeInSeconds The number of seconds that the client/browser should cache a preflight response.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Azure Analytics logging or metrics retention policy.
|
||||
* @typedef {object} RetentionPolicy
|
||||
* @property {boolean} Enabled Indicates whether a retention policy is enabled for the storage service.
|
||||
* @property {number} Days Indicates the number of days that logging data is retained. All data older than this value will be deleted.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The access policy.
|
||||
* @typedef {object} AccessPolicy
|
||||
* @property {string} Permissions The permission type.
|
||||
* @property {Date} Start The time at which the access policy becomes valid.
|
||||
* @property {Date} Expiry The time at which the access policy becomes expired.
|
||||
* @property {string} IPAddressOrRange An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive.
|
||||
* @property {string} Protocols The protocols permitted for a request made with the SAS.
|
||||
* @property {string} Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy.
|
||||
* @property {string} ResourceTypes The resource type for a shared access signature associated with this shared access policy.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The service statistics.
|
||||
* @typedef {object} ServiceStats
|
||||
* @property {object} GeoReplication The geo replication stastics.
|
||||
* @property {string} GeoReplication.Status The status of the secondary location.
|
||||
* @property {Date} GeoReplication.LastSyncTime A GMT date/time value, to the second.
|
||||
* All primary writes preceding this value are guaranteed to be available for read operations at the secondary.
|
||||
* Primary writes after this point in time may or may not be available for reads.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The range.
|
||||
* @typedef {object} Range
|
||||
* @property {number} start The start of the range.
|
||||
* @property {number} end The end of the range.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The range diff. Refer to https://msdn.microsoft.com/en-us/library/azure/mt736912.aspx
|
||||
* @typedef {object} RangeDiff
|
||||
* @property {number} start The start of the range.
|
||||
* @property {number} end The end of the range.
|
||||
* @property {boolean} isCleared If the range is cleared or not.
|
||||
|
||||
*/
|
||||
|
||||
exports.Constants = azureCommon.Constants;
|
||||
exports.StorageUtilities = azureCommon.StorageUtilities;
|
||||
exports.AccessCondition = azureCommon.AccessCondition;
|
||||
|
||||
exports.SR = azureCommon.SR;
|
||||
exports.StorageServiceClient = StorageServiceClient;
|
||||
exports.Logger = azureCommon.Logger;
|
||||
exports.WebResource = azureCommon.WebResource;
|
||||
exports.Validate = azureCommon.validate;
|
||||
exports.date = azureCommon.date;
|
||||
|
||||
// Other filters
|
||||
exports.LinearRetryPolicyFilter = azureCommon.LinearRetryPolicyFilter;
|
||||
exports.ExponentialRetryPolicyFilter = azureCommon.ExponentialRetryPolicyFilter;
|
||||
exports.RetryPolicyFilter = azureCommon.RetryPolicyFilter;
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var azureCommon = require('./common.core');
|
||||
|
||||
azureCommon.BrowserFileReadStream = require('./streams/browserfilereadstream');
|
||||
|
||||
module.exports = azureCommon;
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var exports = module.exports;
|
||||
|
||||
var azureutil = require('./util/util');
|
||||
|
||||
require('./util/patch-xmlbuilder');
|
||||
|
||||
var nodeVersion = azureutil.getNodeVersion();
|
||||
if (nodeVersion.major === 0 && nodeVersion.minor > 8 && !(nodeVersion.minor > 10 || (nodeVersion.minor === 10 && nodeVersion.patch >= 3))) {
|
||||
throw new Error('The Microsoft Azure node SDK does not work with node versions > 0.9.0 and < 0.10.3. Please upgrade to node >= 0.10.3');
|
||||
}
|
||||
|
||||
exports.xmlbuilder = require('xmlbuilder');
|
||||
exports.xml2js = require('xml2js');
|
||||
|
||||
exports.Logger = require('./diagnostics/logger');
|
||||
exports.WebResource = require('./http/webresource');
|
||||
|
||||
// Services
|
||||
exports.StorageServiceClient = require('./services/storageserviceclient');
|
||||
|
||||
// Models
|
||||
exports.ServicePropertiesResult = require('./models/servicepropertiesresult');
|
||||
exports.ServiceStatsParser = require('./models/servicestatsparser');
|
||||
exports.AclResult = require('./models/aclresult');
|
||||
|
||||
// Filters
|
||||
exports.LinearRetryPolicyFilter = require('./filters/linearretrypolicyfilter');
|
||||
exports.ExponentialRetryPolicyFilter = require('./filters/exponentialretrypolicyfilter');
|
||||
exports.RetryPolicyFilter = require('./filters/retrypolicyfilter');
|
||||
|
||||
// Signing
|
||||
exports.SharedAccessSignature = require('./signing/sharedaccesssignature');
|
||||
exports.SharedKey = require('./signing/sharedkey');
|
||||
|
||||
// Streams
|
||||
exports.BatchOperation = require('./streams/batchoperation');
|
||||
exports.ChunkAllocator = require('./streams/chunkallocator');
|
||||
exports.ChunkStream = require('./streams/chunkstream');
|
||||
exports.ChunkStreamWithStream = require('./streams/chunkstreamwithstream');
|
||||
exports.SpeedSummary = require('./streams/speedsummary');
|
||||
exports.BufferStream = require('./streams/bufferstream');
|
||||
|
||||
// Utilities
|
||||
exports.Constants = require('./util/constants');
|
||||
exports.SR = require('./util/sr');
|
||||
exports.date = require('./util/date');
|
||||
exports.ISO8061Date = require('./util/iso8061date');
|
||||
exports.util = require('./util/util');
|
||||
exports.validate = require('./util/validate');
|
||||
exports.StorageUtilities = require('./util/storageutilities');
|
||||
exports.AccessCondition = require('./util/accesscondition');
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var azureCommon = require('./common.core');
|
||||
|
||||
// Streams
|
||||
azureCommon.FileReadStream = require('./streams/filereadstream');
|
||||
|
||||
module.exports = azureCommon;
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
/**
|
||||
* Creates a new Logger object
|
||||
* @class
|
||||
* The Logger class is used to write log information.
|
||||
*
|
||||
* @constructor
|
||||
*
|
||||
* @param {string} [level] The log level. Refer to Logger.LogLevels.
|
||||
* @param {object} [loggerFunction] The function to write log information.
|
||||
*/
|
||||
function Logger(level, loggerFunction) {
|
||||
/**
|
||||
* The log level. Refer to the Logger.LogLevels for available log levels.
|
||||
* @name Logger#level
|
||||
* @type {string}
|
||||
* @see Logger.LogLevels
|
||||
*/
|
||||
this.level = level;
|
||||
|
||||
this.loggerFunction = loggerFunction;
|
||||
|
||||
if (!this.loggerFunction) {
|
||||
this.loggerFunction = this.defaultLoggerFunction;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The available log levels.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
Logger.LogLevels = {
|
||||
/**
|
||||
* System is unusable.
|
||||
*/
|
||||
EMERGENCY: 'emergency',
|
||||
|
||||
/**
|
||||
* Action must be taken immediately.
|
||||
*/
|
||||
ALERT : 'alert',
|
||||
|
||||
/**
|
||||
* Critical condition.
|
||||
*/
|
||||
CRITICAL : 'critical',
|
||||
|
||||
/**
|
||||
* Error condition.
|
||||
*/
|
||||
ERROR : 'error',
|
||||
|
||||
/**
|
||||
* Warning condition.
|
||||
*/
|
||||
WARNING : 'warning',
|
||||
|
||||
/**
|
||||
* Normal but significant condition.
|
||||
*/
|
||||
NOTICE : 'notice',
|
||||
|
||||
/**
|
||||
* Purely informational message.
|
||||
*/
|
||||
INFO : 'info',
|
||||
|
||||
/**
|
||||
* Application debug messages.
|
||||
*/
|
||||
DEBUG : 'debug'
|
||||
};
|
||||
|
||||
Logger.logPriority = [
|
||||
Logger.LogLevels.EMERGENCY,
|
||||
Logger.LogLevels.ALERT,
|
||||
Logger.LogLevels.CRITICAL,
|
||||
Logger.LogLevels.ERROR,
|
||||
Logger.LogLevels.WARNING,
|
||||
Logger.LogLevels.NOTICE,
|
||||
Logger.LogLevels.INFO,
|
||||
Logger.LogLevels.DEBUG
|
||||
];
|
||||
|
||||
Logger.prototype.log = function (level, msg) {
|
||||
this.loggerFunction(level, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.emergency = function(msg) {
|
||||
this.log(Logger.LogLevels.EMERGENCY, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.critical = function(msg) {
|
||||
this.log(Logger.LogLevels.CRITICAL, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.alert = function(msg) {
|
||||
this.log(Logger.LogLevels.ALERT, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.error = function(msg) {
|
||||
this.log(Logger.LogLevels.ERROR, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.warn = function(msg) {
|
||||
this.log(Logger.LogLevels.WARNING, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.notice = function(msg) {
|
||||
this.log(Logger.LogLevels.NOTICE, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.info = function(msg) {
|
||||
this.log(Logger.LogLevels.INFO, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.debug = function(msg) {
|
||||
this.log(Logger.LogLevels.DEBUG, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.defaultLoggerFunction = function(logLevel , msg) {
|
||||
var currentLevelIndex = Logger.logPriority.indexOf(this.level);
|
||||
var logLevelIndex = Logger.logPriority.indexOf(logLevel);
|
||||
var time = new Date();
|
||||
var timeStamp = time.toISOString();
|
||||
if (logLevelIndex <= currentLevelIndex) {
|
||||
console.log('[' + timeStamp + ']' + this.level + ' : ' + msg);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Logger;
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var util = require('util');
|
||||
var _ = require('underscore');
|
||||
|
||||
function captureStackTrace(targetObject, constructorOpt) {
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(targetObject, constructorOpt);
|
||||
}
|
||||
}
|
||||
|
||||
function ArgumentError(argumentName, message) {
|
||||
captureStackTrace(this, this.constructor);
|
||||
this.name = this.constructor.name;
|
||||
this.argumentName = argumentName;
|
||||
this.message = message || util.format('Invalid or missing argument supplied: %s', argumentName);
|
||||
}
|
||||
util.inherits(ArgumentError, Error);
|
||||
|
||||
function ArgumentNullError(argumentName, message) {
|
||||
captureStackTrace(this, this.constructor);
|
||||
this.name = this.constructor.name;
|
||||
this.argumentName = argumentName;
|
||||
this.message = message || util.format('Missing argument: %s', argumentName);
|
||||
}
|
||||
|
||||
util.inherits(ArgumentNullError, Error);
|
||||
|
||||
function StorageError(message, properties) {
|
||||
captureStackTrace(this, this.constructor);
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
|
||||
if(properties){
|
||||
_.extend(this, properties);
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(StorageError, Error);
|
||||
|
||||
function TimeoutError(message) {
|
||||
captureStackTrace(this, this.constructor);
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
util.inherits(TimeoutError, Error);
|
||||
|
||||
module.exports.ArgumentError = ArgumentError;
|
||||
module.exports.ArgumentNullError = ArgumentNullError;
|
||||
module.exports.StorageError = StorageError;
|
||||
module.exports.TimeoutError = TimeoutError;
|
||||
module.exports.captureStackTrace = captureStackTrace;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
'use strict';
|
||||
|
||||
var RetryPolicyFilter = require('./retrypolicyfilter');
|
||||
/**
|
||||
* Creates a new 'ExponentialRetryPolicyFilter' instance.
|
||||
* @class
|
||||
* The ExponentialRetryPolicyFilter allows you to retry operations,
|
||||
* using an exponential back-off interval between retries.
|
||||
* To apply a filter to service operations, use `withFilter`
|
||||
* and specify the filter to be used when creating a service.
|
||||
* @constructor
|
||||
* @param {number} [retryCount=3] The client retry count.
|
||||
* @param {number} [retryInterval=30000] The client retry interval, in milliseconds.
|
||||
* @param {number} [minRetryInterval=3000] The minimum retry interval, in milliseconds.
|
||||
* @param {number} [maxRetryInterval=90000] The maximum retry interval, in milliseconds.
|
||||
*
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var retryOperations = new azure.ExponentialRetryPolicyFilter();
|
||||
* var blobService = azure.createBlobService().withFilter(retryOperations)
|
||||
*/
|
||||
function ExponentialRetryPolicyFilter(retryCount, retryInterval, minRetryInterval, maxRetryInterval) {
|
||||
this.retryCount = retryCount ? retryCount : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT;
|
||||
this.retryInterval = retryInterval ? retryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL;
|
||||
this.minRetryInterval = minRetryInterval ? minRetryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MIN_RETRY_INTERVAL;
|
||||
this.maxRetryInterval = maxRetryInterval ? maxRetryInterval : ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MAX_RETRY_INTERVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the default client retry interval, in milliseconds.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30;
|
||||
|
||||
/**
|
||||
* Represents the default client retry count.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3;
|
||||
|
||||
/**
|
||||
* Represents the default maximum retry interval, in milliseconds.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MAX_RETRY_INTERVAL = 1000 * 90;
|
||||
|
||||
/**
|
||||
* Represents the default minimum retry interval, in milliseconds.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.DEFAULT_CLIENT_MIN_RETRY_INTERVAL = 1000 * 3;
|
||||
|
||||
/**
|
||||
* Determines if the operation should be retried and how long to wait until the next retry.
|
||||
*
|
||||
* @param {number} statusCode The HTTP status code.
|
||||
* @param {object} requestOptions The request options.
|
||||
* @return {retryInfo} Information about whether the operation qualifies for a retry and the retryInterval.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.prototype.shouldRetry = function (statusCode, requestOptions) {
|
||||
var retryData = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {};
|
||||
|
||||
// Adjust retry interval
|
||||
var incrementDelta = Math.pow(2, retryData.retryCount) - 1;
|
||||
var boundedRandDelta = this.retryInterval * 0.8 + Math.floor(Math.random() * (this.retryInterval * 1.2 - this.retryInterval * 0.8));
|
||||
incrementDelta *= boundedRandDelta;
|
||||
|
||||
retryData.retryInterval = Math.min(this.minRetryInterval + incrementDelta, this.maxRetryInterval);
|
||||
|
||||
return RetryPolicyFilter._shouldRetryOnError(statusCode, requestOptions);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles an operation with an exponential retry policy.
|
||||
*
|
||||
* @param {Object} requestOptions The original request options.
|
||||
* @param {function} next The next filter to be handled.
|
||||
*/
|
||||
ExponentialRetryPolicyFilter.prototype.handle = function (requestOptions, next) {
|
||||
RetryPolicyFilter._handle(this, requestOptions, next);
|
||||
};
|
||||
|
||||
module.exports = ExponentialRetryPolicyFilter;
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
'use strict';
|
||||
|
||||
var RetryPolicyFilter = require('./retrypolicyfilter');
|
||||
|
||||
/**
|
||||
* Creates a new LinearRetryPolicyFilter instance.
|
||||
* @class
|
||||
* The LinearRetryPolicyFilter allows you to retry operations,
|
||||
* using an linear back-off interval between retries.
|
||||
* To apply a filter to service operations, use `withFilter`
|
||||
* and specify the filter to be used when creating a service.
|
||||
* @constructor
|
||||
* @param {number} [retryCount=3] The client retry count.
|
||||
* @param {number} [retryInterval=30000] The client retry interval, in milliseconds.
|
||||
*
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var retryOperations = new azure.LinearRetryPolicyFilter();
|
||||
* var blobService = azure.createBlobService().withFilter(retryOperations)
|
||||
*/
|
||||
function LinearRetryPolicyFilter(retryCount, retryInterval) {
|
||||
this.retryCount = retryCount ? retryCount : LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT;
|
||||
this.retryInterval = retryInterval ? retryInterval : LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the default client retry interval, in milliseconds.
|
||||
*/
|
||||
LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30;
|
||||
|
||||
/**
|
||||
* Represents the default client retry count.
|
||||
*/
|
||||
LinearRetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3;
|
||||
|
||||
/**
|
||||
* Determines if the operation should be retried and how long to wait until the next retry.
|
||||
*
|
||||
* @param {number} statusCode The HTTP status code.
|
||||
* @param {object} requestOptions The request options.
|
||||
* @return {retryInfo} Information about whether the operation qualifies for a retry and the retryInterval.
|
||||
*/
|
||||
LinearRetryPolicyFilter.prototype.shouldRetry = function (statusCode, requestOptions) {
|
||||
var retryData = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {};
|
||||
retryData.retryInterval = this.retryInterval;
|
||||
|
||||
return RetryPolicyFilter._shouldRetryOnError(statusCode, requestOptions);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles an operation with a linear retry policy.
|
||||
*
|
||||
* @param {Object} requestOptions The original request options.
|
||||
* @param {function} next The next filter to be handled.
|
||||
*/
|
||||
LinearRetryPolicyFilter.prototype.handle = function (requestOptions, next) {
|
||||
RetryPolicyFilter._handle(this, requestOptions, next);
|
||||
};
|
||||
|
||||
module.exports = LinearRetryPolicyFilter;
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var azureutil = require('../util/util');
|
||||
var Constants = require('../util/constants');
|
||||
var StorageUtilities = require('../util/storageutilities');
|
||||
var extend = require('util')._extend;
|
||||
|
||||
/**
|
||||
* Creates a new RetryPolicyFilter instance.
|
||||
* @class
|
||||
* The RetryPolicyFilter allows you to retry operations,
|
||||
* using a custom retry policy. Users are responsible to
|
||||
* define the shouldRetry method.
|
||||
* To apply a filter to service operations, use `withFilter`
|
||||
* and specify the filter to be used when creating a service.
|
||||
* @constructor
|
||||
* @param {number} [retryCount=30000] The client retry count.
|
||||
* @param {number} [retryInterval=3] The client retry interval, in milliseconds.
|
||||
*
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var retryPolicy = new azure.RetryPolicyFilter();
|
||||
* retryPolicy.retryCount = 3;
|
||||
* retryPolicy.retryInterval = 3000;
|
||||
* retryPolicy.shouldRetry = function(statusCode, retryContext) {
|
||||
*
|
||||
* };
|
||||
* var blobService = azure.createBlobService().withFilter(retryPolicy);
|
||||
*/
|
||||
function RetryPolicyFilter(retryCount, retryInterval) {
|
||||
this.retryCount = retryCount ? retryCount : RetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT;
|
||||
this.retryInterval = retryInterval ? retryInterval : RetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the default client retry interval, in milliseconds.
|
||||
*/
|
||||
RetryPolicyFilter.DEFAULT_CLIENT_RETRY_INTERVAL = 1000 * 30;
|
||||
|
||||
/**
|
||||
* Represents the default client retry count.
|
||||
*/
|
||||
RetryPolicyFilter.DEFAULT_CLIENT_RETRY_COUNT = 3;
|
||||
|
||||
/**
|
||||
* Handles an operation with a retry policy.
|
||||
*
|
||||
* @param {Object} requestOptions The original request options.
|
||||
* @param {function} next The next filter to be handled.
|
||||
*/
|
||||
RetryPolicyFilter.prototype.handle = function (requestOptions, next) {
|
||||
RetryPolicyFilter._handle(this, requestOptions, next);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles an operation with a retry policy.
|
||||
*
|
||||
* @param {Object} requestOptions The original request options.
|
||||
* @param {function} next The next filter to be handled.
|
||||
*/
|
||||
RetryPolicyFilter._handle = function (self, requestOptions, next) {
|
||||
|
||||
var retryRequestOptions = extend({}, requestOptions);
|
||||
retryRequestOptions.retryInterval = 0;
|
||||
|
||||
// Initialize retryContext because that will be passed to the shouldRetry method which users will implement
|
||||
retryRequestOptions.retryContext = {
|
||||
retryCount: 0,
|
||||
error: null,
|
||||
retryInterval: retryRequestOptions.retryInterval,
|
||||
locationMode: retryRequestOptions.locationMode,
|
||||
currentLocation: retryRequestOptions.currentLocation
|
||||
};
|
||||
|
||||
var lastPrimaryAttempt;
|
||||
var lastSecondaryAttempt;
|
||||
var operation = function () {
|
||||
// retry policies dont really do anything to the request options
|
||||
// so move on to next
|
||||
if (next) {
|
||||
next(retryRequestOptions, function (returnObject, finalCallback, nextPostCallback) {
|
||||
// Previous operation ended so update the retry data
|
||||
if (returnObject.error) {
|
||||
if (retryRequestOptions.retryContext.error) {
|
||||
returnObject.error.innerError = retryRequestOptions.retryContext.error;
|
||||
}
|
||||
|
||||
retryRequestOptions.retryContext.error = returnObject.error;
|
||||
}
|
||||
|
||||
// If a request sent to the secondary location fails with 404 (Not Found), it is possible
|
||||
// that the resource replication is not finished yet. So, in case of 404 only in the secondary
|
||||
// location, the failure should still be retryable.
|
||||
var secondaryNotFound = (retryRequestOptions.currentLocation === Constants.StorageLocation.SECONDARY) && ((returnObject.response && returnObject.response.statusCode === 404) || (returnObject.error && returnObject.error.code === 'ENOTFOUND'));
|
||||
|
||||
var notExceedMaxRetryCount = retryRequestOptions.retryContext.retryCount ? retryRequestOptions.retryContext.retryCount <= self.retryCount : true;
|
||||
var retryInfo = self.shouldRetry(secondaryNotFound ? 500 : (azureutil.objectIsNull(returnObject.response) ? 306 : returnObject.response.statusCode), retryRequestOptions);
|
||||
retryRequestOptions.retryContext.retryCount++;
|
||||
|
||||
if (retryInfo.ignore) {
|
||||
returnObject.error = null;
|
||||
}
|
||||
|
||||
// If the custom retry logic(shouldRetry) does not return a targetLocation, calculate based on the previous location and locationMode.
|
||||
if(azureutil.objectIsNull(retryInfo.targetLocation)) {
|
||||
retryInfo.targetLocation = azureutil.getNextLocation(retryRequestOptions.currentLocation, retryRequestOptions.locationMode);
|
||||
}
|
||||
|
||||
// If the custom retry logic(shouldRetry) does not return a retryInterval, try to set it to the value on the instance if it is available. Otherwise, the default(30000) will be used.
|
||||
if(azureutil.objectIsNull(retryInfo.retryInterval)) {
|
||||
retryInfo.retryInterval = self.retryInterval;
|
||||
}
|
||||
|
||||
// Only in the case of success from server but client side failure like MD5 or length mismatch, returnObject.retryable has a value(we explicitly set it to false).
|
||||
// In this case, we should not retry the request.
|
||||
// If the output stream already get sent to server and get error back,
|
||||
// we should NOT retry within the SDK as the stream data is not valid anymore if we retry directly.
|
||||
if (
|
||||
!returnObject.outputStreamSent && returnObject.error && azureutil.objectIsNull(returnObject.retryable) && notExceedMaxRetryCount &&
|
||||
(
|
||||
(!azureutil.objectIsNull(returnObject.response) && retryInfo.retryable) ||
|
||||
(
|
||||
returnObject.error.code === 'ECONNREFUSED' ||
|
||||
returnObject.error.code === 'ETIMEDOUT' ||
|
||||
returnObject.error.code === 'ESOCKETTIMEDOUT' ||
|
||||
returnObject.error.code === 'ECONNRESET' ||
|
||||
returnObject.error.code === 'EAI_AGAIN'
|
||||
)
|
||||
)
|
||||
) {
|
||||
if (retryRequestOptions.currentLocation === Constants.StorageLocation.PRIMARY) {
|
||||
lastPrimaryAttempt = returnObject.operationEndTime;
|
||||
} else {
|
||||
lastSecondaryAttempt = returnObject.operationEndTime;
|
||||
}
|
||||
|
||||
// Moreover, in case of 404 when trying the secondary location, instead of retrying on the
|
||||
// secondary, further requests should be sent only to the primary location, as it most
|
||||
// probably has a higher chance of succeeding there.
|
||||
if (secondaryNotFound && (retryRequestOptions.locationMode !== StorageUtilities.LocationMode.SECONDARY_ONLY))
|
||||
{
|
||||
retryInfo.locationMode = StorageUtilities.LocationMode.PRIMARY_ONLY;
|
||||
retryInfo.targetLocation = Constants.StorageLocation.PRIMARY;
|
||||
}
|
||||
|
||||
// Now is the time to calculate the exact retry interval. ShouldRetry call above already
|
||||
// returned back how long two requests to the same location should be apart from each other.
|
||||
// However, for the reasons explained above, the time spent between the last attempt to
|
||||
// the target location and current time must be subtracted from the total retry interval
|
||||
// that ShouldRetry returned.
|
||||
var lastAttemptTime = retryInfo.targetLocation === Constants.StorageLocation.PRIMARY ? lastPrimaryAttempt : lastSecondaryAttempt;
|
||||
if (!azureutil.objectIsNull(lastAttemptTime)) {
|
||||
var sinceLastAttempt = new Date().getTime() - lastAttemptTime.getTime();
|
||||
if (sinceLastAttempt < 0) {
|
||||
sinceLastAttempt = 0;
|
||||
}
|
||||
|
||||
retryRequestOptions.retryInterval = retryInfo.retryInterval - sinceLastAttempt;
|
||||
}
|
||||
else {
|
||||
retryRequestOptions.retryInterval = 0;
|
||||
}
|
||||
|
||||
if(!azureutil.objectIsNull(retryInfo.locationMode)) {
|
||||
retryRequestOptions.locationMode = retryInfo.locationMode;
|
||||
}
|
||||
|
||||
retryRequestOptions.currentLocation = retryInfo.targetLocation;
|
||||
operation();
|
||||
} else {
|
||||
if (nextPostCallback) {
|
||||
nextPostCallback(returnObject);
|
||||
} else if (finalCallback) {
|
||||
finalCallback(returnObject);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
operation();
|
||||
};
|
||||
|
||||
RetryPolicyFilter._shouldRetryOnError = function (statusCode, requestOptions) {
|
||||
var retryInfo = (requestOptions && requestOptions.retryContext) ? requestOptions.retryContext : {};
|
||||
|
||||
// Non-timeout Cases
|
||||
if (statusCode >= 300 && statusCode != 408) {
|
||||
// Always no retry on "not implemented" and "version not supported"
|
||||
if (statusCode == 501 || statusCode == 505) {
|
||||
retryInfo.retryable = false;
|
||||
return retryInfo;
|
||||
}
|
||||
|
||||
// When absorbConditionalErrorsOnRetry is set (for append blob)
|
||||
if (requestOptions && requestOptions.absorbConditionalErrorsOnRetry) {
|
||||
if (statusCode == 412) {
|
||||
// When appending block with precondition failure and their was a server error before, we ignore the error.
|
||||
if (retryInfo.lastServerError) {
|
||||
retryInfo.ignore = true;
|
||||
retryInfo.retryable = true;
|
||||
} else {
|
||||
retryInfo.retryable = false;
|
||||
}
|
||||
} else if (retryInfo.retryable && statusCode >= 500 && statusCode < 600) {
|
||||
// Retry on the server error
|
||||
retryInfo.retryable = true;
|
||||
retryInfo.lastServerError = true;
|
||||
}
|
||||
} else if (statusCode < 500) {
|
||||
// No retry on the client error
|
||||
retryInfo.retryable = false;
|
||||
}
|
||||
}
|
||||
|
||||
return retryInfo;
|
||||
};
|
||||
|
||||
module.exports = RetryPolicyFilter;
|
||||
+324
@@ -0,0 +1,324 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureutil = require('../util/util');
|
||||
var SR = require('../util/sr');
|
||||
var Constants = require('../util/constants');
|
||||
var errors = require('../errors/errors');
|
||||
var ArgumentError = errors.ArgumentError;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
var HttpConstants = Constants.HttpConstants;
|
||||
var HttpConstants = Constants.HttpConstants;
|
||||
var HttpVerbs = HttpConstants.HttpVerbs;
|
||||
|
||||
function encodeSpecialCharacters(path) {
|
||||
return path.replace(/'/g, '%27');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new WebResource object.
|
||||
*
|
||||
* This class provides an abstraction over a REST call by being library / implementation agnostic and wrapping the necessary
|
||||
* properties to initiate a request.
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function WebResource() {
|
||||
this.rawResponse = false;
|
||||
this.queryString = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new put request web resource.
|
||||
*
|
||||
* @param {string} path The path for the put operation.
|
||||
* @return {WebResource} A new webresource with a put operation for the given path.
|
||||
*/
|
||||
WebResource.put = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.PUT;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new get request web resource.
|
||||
*
|
||||
* @param {string} path The path for the get operation.
|
||||
* @return {WebResource} A new webresource with a get operation for the given path.
|
||||
*/
|
||||
WebResource.get = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.GET;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new head request web resource.
|
||||
*
|
||||
* @param {string} path The path for the head operation.
|
||||
* @return {WebResource} A new webresource with a head operation for the given path.
|
||||
*/
|
||||
WebResource.head = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.HEAD;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new delete request web resource.
|
||||
*
|
||||
* @param {string} path The path for the delete operation.
|
||||
* @return {WebResource} A new webresource with a delete operation for the given path.
|
||||
*/
|
||||
WebResource.del = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.DELETE;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new post request web resource.
|
||||
*
|
||||
* @param {string} path The path for the post operation.
|
||||
* @return {WebResource} A new webresource with a post operation for the given path.
|
||||
*/
|
||||
WebResource.post = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.POST;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new merge request web resource.
|
||||
*
|
||||
* @param {string} path The path for the merge operation.
|
||||
* @return {WebResource} A new webresource with a merge operation for the given path.
|
||||
*/
|
||||
WebResource.merge = function (path) {
|
||||
var webResource = new WebResource();
|
||||
webResource.path = path ? encodeSpecialCharacters(path) : null;
|
||||
webResource.method = HttpConstants.HttpVerbs.MERGE;
|
||||
return webResource;
|
||||
};
|
||||
|
||||
/**
|
||||
* Specifies a custom property in the web resource.
|
||||
*
|
||||
* @param {string} name The property name.
|
||||
* @param {string} value The property value.
|
||||
* @return {WebResource} The webresource.
|
||||
*/
|
||||
WebResource.prototype.withProperty = function (name, value) {
|
||||
if (!this.properties) {
|
||||
this.properties = {};
|
||||
}
|
||||
|
||||
this.properties[name] = value;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Specifies if the response should be parsed or not.
|
||||
*
|
||||
* @param {bool} rawResponse true if the response should not be parsed; false otherwise.
|
||||
* @return {WebResource} The webresource.
|
||||
*/
|
||||
WebResource.prototype.withRawResponse = function (rawResponse) {
|
||||
this.rawResponse = rawResponse;
|
||||
|
||||
if (azureutil.objectIsNull(this.rawResponse)) {
|
||||
this.rawResponse = true;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
WebResource.prototype.withHeadersOnly = function (headersOnly) {
|
||||
if (headersOnly !== undefined) {
|
||||
this.headersOnly = headersOnly;
|
||||
} else {
|
||||
this.headersOnly = true;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an optional query string parameter.
|
||||
*
|
||||
* @param {Object} name The name of the query string parameter.
|
||||
* @param {Object} value The value of the query string parameter.
|
||||
* @param {Object} defaultValue The default value for the query string parameter to be used if no value is passed.
|
||||
* @return {Object} The web resource.
|
||||
*/
|
||||
WebResource.prototype.withQueryOption = function (name, value, defaultValue) {
|
||||
if (!azureutil.objectIsNull(value)) {
|
||||
this.queryString[name] = value;
|
||||
} else if (defaultValue) {
|
||||
this.queryString[name] = defaultValue;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds optional query string parameters.
|
||||
*
|
||||
* Additional arguments will be the needles to search in the haystack.
|
||||
*
|
||||
* @param {Object} object The haystack of query string parameters.
|
||||
* @return {Object} The web resource.
|
||||
*/
|
||||
WebResource.prototype.withQueryOptions = function (object) {
|
||||
if (object) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
if (object[arguments[i]]) {
|
||||
this.withQueryOption(arguments[i], object[arguments[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an optional header parameter.
|
||||
*
|
||||
* @param {Object} name The name of the header parameter.
|
||||
* @param {Object} value The value of the header parameter.
|
||||
* @return {Object} The web resource.
|
||||
*/
|
||||
WebResource.prototype.withHeader = function (name, value) {
|
||||
if (!this.headers) {
|
||||
this.headers = {};
|
||||
}
|
||||
|
||||
if (!azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) {
|
||||
value = value instanceof Date ? value.toUTCString() : value;
|
||||
|
||||
this.headers[name] = value;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an optional body.
|
||||
*
|
||||
* @param {Object} body The request body.
|
||||
* @return {Object} The web resource.
|
||||
*/
|
||||
WebResource.prototype.withBody = function (body) {
|
||||
this.body = body;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds optional query string parameters.
|
||||
*
|
||||
* Additional arguments will be the needles to search in the haystack.
|
||||
*
|
||||
* @param {Object} object The haystack of headers.
|
||||
* @return {Object} The web resource.
|
||||
*/
|
||||
WebResource.prototype.withHeaders = function (object) {
|
||||
if (object) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
if (object[arguments[i]]) {
|
||||
this.withHeader(arguments[i], object[arguments[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
WebResource.prototype.addOptionalMetadataHeaders = function (metadata) {
|
||||
var self = this;
|
||||
|
||||
if (metadata) {
|
||||
Object.keys(metadata).forEach(function (metadataKey) {
|
||||
if (azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(metadataKey)) {
|
||||
throw new ArgumentError('metadata', SR.METADATA_KEY_INVALID);
|
||||
}
|
||||
|
||||
var value = metadata[metadataKey];
|
||||
if (azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) {
|
||||
throw new ArgumentError('metadata', SR.METADATA_VALUE_INVALID);
|
||||
}
|
||||
|
||||
var metadataHeaderName = HeaderConstants.PREFIX_FOR_STORAGE_METADATA + metadataKey;
|
||||
var existingMetadataHeaderName = '';
|
||||
var headers = self.headers ? self.headers : {};
|
||||
if (Object.keys(headers).some(function (headerName) {
|
||||
existingMetadataHeaderName = headerName;
|
||||
return headerName.toString().toLowerCase() === metadataHeaderName.toLowerCase();
|
||||
})) {
|
||||
self.withHeader(existingMetadataHeaderName, self.headers[existingMetadataHeaderName] + ',' + value);
|
||||
} else {
|
||||
self.withHeader(metadataHeaderName, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if a status code corresponds to a valid response according to the WebResource's expected status codes.
|
||||
*
|
||||
* @param {int} statusCode The response status code.
|
||||
* @return true if the response is valid; false otherwise.
|
||||
*/
|
||||
WebResource.validResponse = function (statusCode) {
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
function isMethodWithBody(verb) {
|
||||
return verb === HttpVerbs.PUT ||
|
||||
verb === HttpVerbs.POST ||
|
||||
verb === HttpVerbs.MERGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook up the given input stream to a destination output stream if the WebResource method
|
||||
* requires a request body and a body is not already set.
|
||||
*
|
||||
* @param {Stream} inputStream the stream to pipe from
|
||||
* @param {Stream} outputStream the stream to pipe to
|
||||
*
|
||||
* @return destStream
|
||||
*/
|
||||
WebResource.prototype.pipeInput = function(inputStream, destStream) {
|
||||
if (isMethodWithBody(this.method) && !this.hasOwnProperty('body')) {
|
||||
inputStream.pipe(destStream);
|
||||
}
|
||||
|
||||
return destStream;
|
||||
};
|
||||
|
||||
module.exports = WebResource;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var MD5 = require('md5.js');
|
||||
|
||||
var Md5Wrapper = function () {
|
||||
};
|
||||
|
||||
Md5Wrapper.prototype.createMd5Hash = function() {
|
||||
return new MD5();
|
||||
};
|
||||
|
||||
module.exports = Md5Wrapper;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var crypto = require('crypto');
|
||||
|
||||
var Md5Wrapper = function () {
|
||||
};
|
||||
|
||||
Md5Wrapper.prototype.createMd5Hash = function() {
|
||||
return crypto.createHash('md5');
|
||||
};
|
||||
|
||||
module.exports = Md5Wrapper;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "azure-storage-md5-wrapper",
|
||||
"author": "Microsoft Corporation",
|
||||
"license": "Apache-2.0",
|
||||
"main": "md5.node.js",
|
||||
"browser": "md5.browser.js"
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
var xmlbuilder = require('xmlbuilder');
|
||||
|
||||
var azureutil = require('../util/util');
|
||||
var ISO8061Date = require('../util/iso8061date');
|
||||
var Constants = require('../util/constants');
|
||||
var AclConstants = Constants.AclConstants;
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* Builds an XML representation for container acl permissions.
|
||||
*
|
||||
* @param {Object.<string, AccessPolicy>} entity The signed identifiers.
|
||||
* @return {string} The XML container acl permissions.
|
||||
*/
|
||||
exports.serialize = function (signedIdentifiersJs) {
|
||||
var doc = xmlbuilder.create();
|
||||
doc = doc.begin(AclConstants.SIGNED_IDENTIFIERS_ELEMENT, { version: '1.0', encoding: 'utf-8' });
|
||||
|
||||
var keys = Object.keys(signedIdentifiersJs);
|
||||
if (keys.length > 0) {
|
||||
keys.forEach(function (key) {
|
||||
var accessPolicy = signedIdentifiersJs[key];
|
||||
doc = doc
|
||||
.ele(AclConstants.SIGNED_IDENTIFIER_ELEMENT)
|
||||
.ele(AclConstants.ID)
|
||||
.txt(key)
|
||||
.up()
|
||||
.ele(AclConstants.ACCESS_POLICY);
|
||||
|
||||
if (accessPolicy.Start) {
|
||||
var startIsoString = accessPolicy.Start;
|
||||
if (!_.isDate(startIsoString)) {
|
||||
startIsoString = new Date(startIsoString);
|
||||
}
|
||||
|
||||
// Convert to expected ISO 8061 date format
|
||||
startIsoString = ISO8061Date.format(startIsoString);
|
||||
|
||||
doc = doc
|
||||
.ele(AclConstants.START)
|
||||
.txt(startIsoString)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (accessPolicy.Expiry) {
|
||||
var expiryIsoString = accessPolicy.Expiry;
|
||||
if (!_.isDate(expiryIsoString)) {
|
||||
expiryIsoString = new Date(expiryIsoString);
|
||||
}
|
||||
|
||||
// Convert to expected ISO 8061 date format
|
||||
expiryIsoString = ISO8061Date.format(expiryIsoString);
|
||||
|
||||
doc = doc
|
||||
.ele(AclConstants.EXPIRY)
|
||||
.txt(expiryIsoString)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (accessPolicy.Permissions) {
|
||||
doc = doc
|
||||
.ele(AclConstants.PERMISSION)
|
||||
.txt(accessPolicy.Permissions)
|
||||
.up();
|
||||
}
|
||||
|
||||
doc = doc.up().up();
|
||||
});
|
||||
}
|
||||
return doc.doc().toString();
|
||||
};
|
||||
|
||||
exports.parse = function (signedIdentifiersXml) {
|
||||
var signedIdentifiers = {};
|
||||
|
||||
signedIdentifiersXml = azureutil.tryGetValueChain(signedIdentifiersXml, [ 'SignedIdentifiers', 'SignedIdentifier' ]);
|
||||
if (signedIdentifiersXml) {
|
||||
if (!_.isArray(signedIdentifiersXml)) {
|
||||
signedIdentifiersXml = [ signedIdentifiersXml ];
|
||||
}
|
||||
|
||||
signedIdentifiersXml.forEach(function (signedIdentifier) {
|
||||
var accessPolicy = {};
|
||||
if (signedIdentifier.AccessPolicy) {
|
||||
if (signedIdentifier.AccessPolicy.Start) {
|
||||
accessPolicy.Start = ISO8061Date.parse(signedIdentifier.AccessPolicy.Start);
|
||||
}
|
||||
|
||||
if (signedIdentifier.AccessPolicy.Expiry) {
|
||||
accessPolicy.Expiry = ISO8061Date.parse(signedIdentifier.AccessPolicy.Expiry);
|
||||
}
|
||||
|
||||
if (signedIdentifier.AccessPolicy.Permission) {
|
||||
accessPolicy.Permissions = signedIdentifier.AccessPolicy.Permission;
|
||||
}
|
||||
}
|
||||
|
||||
signedIdentifiers[signedIdentifier.Id] = accessPolicy;
|
||||
});
|
||||
}
|
||||
|
||||
return signedIdentifiers;
|
||||
};
|
||||
+427
@@ -0,0 +1,427 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
var xmlbuilder = require('xmlbuilder');
|
||||
|
||||
var Constants = require('../util/constants');
|
||||
var ServicePropertiesConstants = Constants.ServicePropertiesConstants;
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
function serializeRetentionPolicy(doc, policy) {
|
||||
if (policy !== null) {
|
||||
if (typeof policy === 'undefined') {
|
||||
policy = {};
|
||||
}
|
||||
|
||||
doc = doc.ele(ServicePropertiesConstants.RETENTION_POLICY_ELEMENT);
|
||||
if (typeof policy.Enabled !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(policy.Enabled)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof policy.Days !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT)
|
||||
.txt(policy.Days)
|
||||
.up();
|
||||
} else if (policy.Enabled === true) {
|
||||
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT)
|
||||
.txt(1)
|
||||
.up();
|
||||
}
|
||||
|
||||
doc = doc.up();
|
||||
}
|
||||
}
|
||||
|
||||
function serializeDeleteRetentionPolicy(doc, policy) {
|
||||
if (policy !== null) {
|
||||
if (typeof policy === 'undefined') {
|
||||
policy = {};
|
||||
}
|
||||
|
||||
if (typeof policy.Enabled !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(policy.Enabled)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof policy.Days !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT)
|
||||
.txt(policy.Days)
|
||||
.up();
|
||||
} else if (policy.Enabled === true) {
|
||||
doc = doc.ele(ServicePropertiesConstants.DAYS_ELEMENT)
|
||||
.txt(1)
|
||||
.up();
|
||||
}
|
||||
|
||||
doc = doc.up();
|
||||
}
|
||||
}
|
||||
|
||||
function serializeLogging(doc, logging) {
|
||||
if (typeof logging.Version !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT)
|
||||
.txt(logging.Version)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT)
|
||||
.txt(ServicePropertiesConstants.DEFAULT_ANALYTICS_VERSION)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof logging.Delete !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.DELETE_ELEMENT)
|
||||
.txt(logging.Delete)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.DELETE_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof logging.Read !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.READ_ELEMENT)
|
||||
.txt(logging.Read)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.READ_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof logging.Write !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.WRITE_ELEMENT)
|
||||
.txt(logging.Write)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.WRITE_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
serializeRetentionPolicy(doc, logging.RetentionPolicy);
|
||||
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
function serializeMetrics(doc, metrics) {
|
||||
if (typeof metrics.Version !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT)
|
||||
.txt(metrics.Version)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.VERSION_ELEMENT)
|
||||
.txt(ServicePropertiesConstants.DEFAULT_ANALYTICS_VERSION)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof metrics.Enabled !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(metrics.Enabled)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.ENABLED_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (metrics.Enabled) {
|
||||
if (typeof metrics.IncludeAPIs !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.INCLUDE_APIS_ELEMENT)
|
||||
.txt(metrics.IncludeAPIs)
|
||||
.up();
|
||||
} else if (metrics.Enabled === true) {
|
||||
doc = doc.ele(ServicePropertiesConstants.INCLUDE_APIS_ELEMENT)
|
||||
.txt(false)
|
||||
.up();
|
||||
}
|
||||
}
|
||||
serializeRetentionPolicy(doc, metrics.RetentionPolicy);
|
||||
}
|
||||
|
||||
function serializeCorsRules(doc, rules) {
|
||||
if (typeof rules !== 'undefined' && rules !== null && _.isArray(rules)) {
|
||||
rules.forEach(function (rule) {
|
||||
doc = doc.ele(ServicePropertiesConstants.CORS_RULE_ELEMENT);
|
||||
|
||||
if (typeof rule.AllowedMethods !== 'undefined' && _.isArray(rule.AllowedMethods)) {
|
||||
doc = doc.ele(ServicePropertiesConstants.ALLOWED_METHODS_ELEMENT)
|
||||
.txt(rule.AllowedMethods.join(','))
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof rule.AllowedOrigins !== 'undefined' && _.isArray(rule.AllowedOrigins)) {
|
||||
doc = doc.ele(ServicePropertiesConstants.ALLOWED_ORIGINS_ELEMENT)
|
||||
.txt(rule.AllowedOrigins.join(','))
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof rule.AllowedHeaders !== 'undefined' && _.isArray(rule.AllowedHeaders)) {
|
||||
doc = doc.ele(ServicePropertiesConstants.ALLOWED_HEADERS_ELEMENT)
|
||||
.txt(rule.AllowedHeaders.join(','))
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.ALLOWED_HEADERS_ELEMENT)
|
||||
.txt('')
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof rule.ExposedHeaders !== 'undefined' && _.isArray(rule.ExposedHeaders)) {
|
||||
doc = doc.ele(ServicePropertiesConstants.EXPOSED_HEADERS_ELEMENT)
|
||||
.txt(rule.ExposedHeaders.join(','))
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.EXPOSED_HEADERS_ELEMENT)
|
||||
.txt('')
|
||||
.up();
|
||||
}
|
||||
|
||||
if (typeof rule.MaxAgeInSeconds !== 'undefined') {
|
||||
doc = doc.ele(ServicePropertiesConstants.MAX_AGE_IN_SECONDS_ELEMENT)
|
||||
.txt(rule.MaxAgeInSeconds)
|
||||
.up();
|
||||
} else {
|
||||
doc = doc.ele(ServicePropertiesConstants.MAX_AGE_IN_SECONDS_ELEMENT)
|
||||
.txt('0')
|
||||
.up();
|
||||
}
|
||||
|
||||
doc = doc.up();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
exports.serialize = function (servicePropertiesJs) {
|
||||
var doc = xmlbuilder.create();
|
||||
doc = doc.begin(ServicePropertiesConstants.STORAGE_SERVICE_PROPERTIES_ELEMENT, { version: '1.0', encoding: 'utf-8' });
|
||||
|
||||
if (servicePropertiesJs.Logging) {
|
||||
doc = doc.ele(ServicePropertiesConstants.LOGGING_ELEMENT);
|
||||
serializeLogging(doc, servicePropertiesJs.Logging);
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
if (servicePropertiesJs.HourMetrics) {
|
||||
doc = doc.ele(ServicePropertiesConstants.HOUR_METRICS_ELEMENT);
|
||||
serializeMetrics(doc, servicePropertiesJs.HourMetrics);
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
if (servicePropertiesJs.MinuteMetrics) {
|
||||
doc = doc.ele(ServicePropertiesConstants.MINUTE_METRICS_ELEMENT);
|
||||
serializeMetrics(doc, servicePropertiesJs.MinuteMetrics);
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
if (servicePropertiesJs.Cors) {
|
||||
doc = doc.ele(ServicePropertiesConstants.CORS_ELEMENT);
|
||||
serializeCorsRules(doc, servicePropertiesJs.Cors.CorsRule);
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
if (servicePropertiesJs.DefaultServiceVersion) {
|
||||
doc = doc.ele(ServicePropertiesConstants.DEFAULT_SERVICE_VERSION_ELEMENT)
|
||||
.txt(servicePropertiesJs.DefaultServiceVersion)
|
||||
.up();
|
||||
}
|
||||
|
||||
if (servicePropertiesJs.DeleteRetentionPolicy) {
|
||||
doc = doc.ele(ServicePropertiesConstants.DEFAULT_DELETE_RETENTION_POLICY_ELEMENT);
|
||||
serializeDeleteRetentionPolicy(doc, servicePropertiesJs.DeleteRetentionPolicy);
|
||||
doc = doc.up();
|
||||
}
|
||||
|
||||
return doc.doc().toString();
|
||||
};
|
||||
|
||||
function parseRetentionPolicy(policyXml) {
|
||||
var policy = {};
|
||||
|
||||
if (typeof policyXml.Enabled !== 'undefined') {
|
||||
policy.Enabled = policyXml.Enabled === 'true';
|
||||
}
|
||||
|
||||
if (typeof policyXml.Days !== 'undefined') {
|
||||
policy.Days = parseInt(policyXml.Days, 10);
|
||||
}
|
||||
|
||||
return policy;
|
||||
}
|
||||
|
||||
function parseLogging(loggingXml) {
|
||||
var logging = {};
|
||||
|
||||
if (typeof loggingXml.Version !== 'undefined') {
|
||||
logging.Version = loggingXml.Version;
|
||||
}
|
||||
|
||||
if (typeof loggingXml.Delete !== 'undefined') {
|
||||
logging.Delete = loggingXml.Delete === 'true';
|
||||
}
|
||||
|
||||
if (typeof loggingXml.Read !== 'undefined') {
|
||||
logging.Read = loggingXml.Read === 'true';
|
||||
}
|
||||
|
||||
if (typeof loggingXml.Write !== 'undefined') {
|
||||
logging.Write = loggingXml.Write === 'true';
|
||||
}
|
||||
|
||||
if (typeof loggingXml.RetentionPolicy !== 'undefined') {
|
||||
logging.RetentionPolicy = parseRetentionPolicy(loggingXml.RetentionPolicy);
|
||||
}
|
||||
|
||||
return logging;
|
||||
}
|
||||
|
||||
function parseMetrics(metricsXml) {
|
||||
var metrics = {};
|
||||
|
||||
if (typeof metricsXml.Version !== 'undefined') {
|
||||
metrics.Version = metricsXml.Version;
|
||||
}
|
||||
|
||||
if (typeof metricsXml.Enabled !== 'undefined') {
|
||||
metrics.Enabled = metricsXml.Enabled === 'true';
|
||||
}
|
||||
|
||||
if (typeof metricsXml.IncludeAPIs !== 'undefined') {
|
||||
metrics.IncludeAPIs = metricsXml.IncludeAPIs === 'true';
|
||||
}
|
||||
|
||||
if (typeof metricsXml.RetentionPolicy !== 'undefined') {
|
||||
metrics.RetentionPolicy = parseRetentionPolicy(metricsXml.RetentionPolicy);
|
||||
}
|
||||
|
||||
return metrics;
|
||||
}
|
||||
|
||||
function parseCors(corsXml) {
|
||||
var cors = {};
|
||||
|
||||
if (typeof corsXml.CorsRule !== 'undefined') {
|
||||
var rulesXml = corsXml.CorsRule;
|
||||
if (!_.isArray(rulesXml)) {
|
||||
rulesXml = [rulesXml];
|
||||
}
|
||||
|
||||
cors.CorsRule = [];
|
||||
rulesXml.forEach(function (ruleXml) {
|
||||
var rule = {};
|
||||
|
||||
if (typeof ruleXml.AllowedMethods !== 'undefined') {
|
||||
if (ruleXml.AllowedMethods !== '') {
|
||||
rule.AllowedMethods = ruleXml.AllowedMethods.split(',');
|
||||
}
|
||||
else {
|
||||
rule.AllowedMethods = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof ruleXml.AllowedOrigins !== 'undefined') {
|
||||
if (ruleXml.AllowedOrigins !== '') {
|
||||
rule.AllowedOrigins = ruleXml.AllowedOrigins.split(',');
|
||||
}
|
||||
else {
|
||||
rule.AllowedOrigins = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof ruleXml.AllowedHeaders !== 'undefined') {
|
||||
if (ruleXml.AllowedHeaders !== '') {
|
||||
rule.AllowedHeaders = ruleXml.AllowedHeaders.split(',');
|
||||
}
|
||||
else {
|
||||
rule.AllowedHeaders = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof ruleXml.ExposedHeaders !== 'undefined') {
|
||||
if (ruleXml.ExposedHeaders !== '') {
|
||||
rule.ExposedHeaders = ruleXml.ExposedHeaders.split(',');
|
||||
}
|
||||
else {
|
||||
rule.ExposedHeaders = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof ruleXml.MaxAgeInSeconds !== 'undefined') {
|
||||
rule.MaxAgeInSeconds = parseInt(ruleXml.MaxAgeInSeconds, 10);
|
||||
}
|
||||
|
||||
cors.CorsRule.push(rule);
|
||||
});
|
||||
}
|
||||
|
||||
return cors;
|
||||
}
|
||||
|
||||
function parseDeleteRetentionPolicy(deleteRetentionPolicyXml) {
|
||||
var deleteRetentionPolicy = {};
|
||||
|
||||
if (typeof deleteRetentionPolicyXml.Enabled !== 'undefined') {
|
||||
deleteRetentionPolicy.Enabled = deleteRetentionPolicyXml.Enabled === 'true';
|
||||
}
|
||||
|
||||
if (typeof deleteRetentionPolicyXml.Days !== 'undefined') {
|
||||
deleteRetentionPolicy.Days = deleteRetentionPolicyXml.Days;
|
||||
}
|
||||
|
||||
return deleteRetentionPolicy;
|
||||
}
|
||||
|
||||
exports.parse = function (servicePropertiesXml) {
|
||||
var serviceProperties = {};
|
||||
|
||||
if (typeof servicePropertiesXml.Logging !== 'undefined') {
|
||||
serviceProperties.Logging = parseLogging(servicePropertiesXml.Logging);
|
||||
}
|
||||
|
||||
if (typeof servicePropertiesXml.HourMetrics !== 'undefined') {
|
||||
serviceProperties.HourMetrics = parseMetrics(servicePropertiesXml.HourMetrics);
|
||||
}
|
||||
|
||||
if (typeof servicePropertiesXml.MinuteMetrics !== 'undefined') {
|
||||
serviceProperties.MinuteMetrics = parseMetrics(servicePropertiesXml.MinuteMetrics);
|
||||
}
|
||||
|
||||
if (typeof servicePropertiesXml.Cors !== 'undefined') {
|
||||
serviceProperties.Cors = parseCors(servicePropertiesXml.Cors);
|
||||
}
|
||||
|
||||
if (typeof servicePropertiesXml.DefaultServiceVersion !== 'undefined') {
|
||||
serviceProperties.DefaultServiceVersion = servicePropertiesXml.DefaultServiceVersion;
|
||||
}
|
||||
|
||||
if (typeof servicePropertiesXml.DeleteRetentionPolicy !== 'undefined') {
|
||||
serviceProperties.DeleteRetentionPolicy = parseDeleteRetentionPolicy(servicePropertiesXml.DeleteRetentionPolicy);
|
||||
}
|
||||
|
||||
return serviceProperties;
|
||||
};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
exports = module.exports;
|
||||
|
||||
exports.parse = function (serviceStatsXml) {
|
||||
var serviceStats = {};
|
||||
|
||||
if (typeof serviceStatsXml.GeoReplication !== 'undefined') {
|
||||
serviceStats.GeoReplication = {};
|
||||
|
||||
if (typeof serviceStatsXml.GeoReplication.Status !== 'undefined') {
|
||||
serviceStats.GeoReplication.Status = serviceStatsXml.GeoReplication.Status;
|
||||
}
|
||||
|
||||
if (typeof serviceStatsXml.GeoReplication.LastSyncTime !== 'undefined' && serviceStatsXml.GeoReplication.LastSyncTime !== '') {
|
||||
serviceStats.GeoReplication.LastSyncTime = new Date(serviceStatsXml.GeoReplication.LastSyncTime);
|
||||
}
|
||||
}
|
||||
|
||||
return serviceStats;
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "azure-storage-request-wrapper",
|
||||
"author": "Microsoft Corporation",
|
||||
"license": "Apache-2.0",
|
||||
"main": "request.node.js",
|
||||
"browser": "request.browser.js"
|
||||
}
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var azureCommon = require('../common.browser');
|
||||
var Duplex = require('stream').Duplex;
|
||||
var extend = require('extend');
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var url = require('url');
|
||||
var util = require('util');
|
||||
|
||||
/**
|
||||
* Creates a new Request object.
|
||||
*
|
||||
* @constructor
|
||||
* @param {object} options The options for a Request object.
|
||||
* @param {function} callback Callback
|
||||
*/
|
||||
function Request(options, callback) {
|
||||
Duplex.call(this);
|
||||
|
||||
this._init(options, callback);
|
||||
this._send();
|
||||
}
|
||||
|
||||
util.inherits(Request, Duplex);
|
||||
|
||||
Request.prototype._init = function (options, callback) {
|
||||
this.callback = callback;
|
||||
|
||||
var nonReservedProperties = azureCommon.util.filterOutNonReservedProperties(this, options);
|
||||
extend(this, nonReservedProperties);
|
||||
|
||||
this.agent = this.agent || false;
|
||||
this.timeout = this.timeout || Math.pow(2, 32) * 1000;
|
||||
|
||||
this._initUri();
|
||||
this._initHeaders();
|
||||
};
|
||||
|
||||
Request.prototype._initUri = function () {
|
||||
if (!this.uri) {
|
||||
return this.emit('error', new Error('options.uri is a required argument'));
|
||||
}
|
||||
|
||||
if (typeof this.uri === 'string') {
|
||||
this.uri = url.parse(this.uri);
|
||||
}
|
||||
|
||||
if (!this.uri.href) {
|
||||
this.uri.href = url.format(this.uri);
|
||||
}
|
||||
};
|
||||
|
||||
Request.prototype._initHeaders = function () {
|
||||
this.headers = this.headers || {};
|
||||
this.headers['content-length'] = this.headers['content-length'] || 0;
|
||||
};
|
||||
|
||||
Request.prototype._send = function () {
|
||||
this._sent = true;
|
||||
|
||||
var protocol = this.uri.protocol || '';
|
||||
var iface = (protocol === 'https:' ? https : http);
|
||||
|
||||
var options = {
|
||||
scheme: protocol.replace(/:$/, ''),
|
||||
method: this.method,
|
||||
host: this.uri.hostname,
|
||||
port: Number(this.uri.port) || (protocol === 'https:' ? 443 : 80),
|
||||
path: this.uri.path,
|
||||
agent: this.agent,
|
||||
headers: this.headers,
|
||||
withCredentials: this.withCredentials,
|
||||
localAddress: this.localAddress,
|
||||
mode: this.mode
|
||||
};
|
||||
|
||||
if (protocol === 'https:') {
|
||||
options.pfx = this.pfx;
|
||||
options.key = this.key;
|
||||
options.cert = this.cert;
|
||||
options.ca = this.ca;
|
||||
options.ciphers = this.ciphers;
|
||||
options.rejectUnauthorized = this.rejectUnauthorized;
|
||||
options.secureProtocol = this.secureProtocol;
|
||||
}
|
||||
|
||||
var httpRequest = iface.request(options);
|
||||
if (this.timeout && httpRequest.setTimeout) {
|
||||
httpRequest.setTimeout(this.timeout);
|
||||
}
|
||||
|
||||
this.httpRequest = httpRequest;
|
||||
this.emit('request', httpRequest);
|
||||
|
||||
this._sendBody();
|
||||
this._listenHttpResponse();
|
||||
this._listenHttpError();
|
||||
|
||||
return httpRequest;
|
||||
};
|
||||
|
||||
Request.prototype._sendBody = function () {
|
||||
if (this.body) {
|
||||
this.httpRequest.write(this.body);
|
||||
this.httpRequest.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.headers['content-length'] == '0') {
|
||||
this.httpRequest.end();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
Request.prototype._listenHttpError = function() {
|
||||
var self = this;
|
||||
|
||||
self.httpRequest.on('error', function(error) {
|
||||
self.emit('error', error);
|
||||
});
|
||||
|
||||
self.on('error', function(error) {
|
||||
if (self.callback) {
|
||||
self.callback(error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Request.prototype._listenHttpResponse = function () {
|
||||
var self = this;
|
||||
|
||||
self.httpRequest.on('response', function (response) {
|
||||
var buffers = [];
|
||||
var bufferLength = 0;
|
||||
|
||||
self.response = response;
|
||||
|
||||
response.on('data', function (chunk) {
|
||||
self.push(chunk);
|
||||
buffers.push(chunk);
|
||||
bufferLength += chunk.length;
|
||||
});
|
||||
|
||||
response.on('error', function (error) {
|
||||
self.emit('error', error);
|
||||
});
|
||||
|
||||
response.on('end', function () {
|
||||
self.push(null);
|
||||
|
||||
response.body = '';
|
||||
if (bufferLength > 0) {
|
||||
response.body = Buffer.concat(buffers, bufferLength);
|
||||
}
|
||||
|
||||
if (self.encoding !== null) {
|
||||
response.body = response.body.toString(self.encoding);
|
||||
}
|
||||
|
||||
if (self.callback) {
|
||||
self.callback(null, response);
|
||||
}
|
||||
});
|
||||
|
||||
self.emit('response', response);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Set a Request header.
|
||||
*
|
||||
* @param {string} key The user provided header key.
|
||||
* @param {string} value The valid header value.
|
||||
*/
|
||||
Request.prototype.setHeader = function (key, value) {
|
||||
if (this._sent) {
|
||||
throw new Error('Request already sent');
|
||||
}
|
||||
|
||||
this.headers[key] = value;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set a Request URI.
|
||||
*
|
||||
* @param {string} uri The user provided uri.
|
||||
*/
|
||||
Request.prototype.setLocation = function (uri) {
|
||||
this.uri = uri;
|
||||
return this;
|
||||
};
|
||||
|
||||
Request.prototype.end = function (chunk) {
|
||||
if (chunk) {
|
||||
this.httpRequest.write(chunk);
|
||||
}
|
||||
|
||||
this.httpRequest.end();
|
||||
};
|
||||
|
||||
Request.prototype._write = function (chunk, encoding, callback) {
|
||||
this.httpRequest.write(chunk);
|
||||
callback();
|
||||
};
|
||||
|
||||
Request.prototype._read = function () {
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a Request object
|
||||
* @ignore
|
||||
*
|
||||
* @param {object} options Options
|
||||
* @param {function} callback Callback
|
||||
* @return {Request} The created request object.
|
||||
*/
|
||||
function req(optionsOrCallback, callback) {
|
||||
var reqDefaults = req.defaults();
|
||||
return reqDefaults(optionsOrCallback, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Request creator with default options
|
||||
* @ignore
|
||||
*
|
||||
* @param {object} defaultOptions Default options
|
||||
* @return {function}
|
||||
*/
|
||||
req.defaults = function (defaultOptions) {
|
||||
return function (optionsOrCallback, callback) {
|
||||
var options;
|
||||
azureCommon.util.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
|
||||
|
||||
var nonReservedProperties = azureCommon.util.filterOutNonReservedProperties(options, defaultOptions);
|
||||
extend(options, nonReservedProperties);
|
||||
|
||||
var request = new Request(options, callback);
|
||||
return request;
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = req;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
module.exports = require('request');
|
||||
+262
@@ -0,0 +1,262 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var azureUtil = require('../util/util');
|
||||
var errors = require('../errors/errors');
|
||||
var SR = require('../util/sr');
|
||||
var Constants = require('../util/constants');
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* The default protocol.
|
||||
*/
|
||||
exports.DEFAULT_PROTOCOL = Constants.HTTPS;
|
||||
|
||||
var NoMatchError = function (msg, constr) {
|
||||
errors.captureStackTrace(this, constr || this);
|
||||
this.message = msg || 'Error';
|
||||
};
|
||||
|
||||
util.inherits(NoMatchError, Error);
|
||||
NoMatchError.prototype.name = 'NoMatchError';
|
||||
|
||||
exports.NoMatchError = NoMatchError;
|
||||
|
||||
/**
|
||||
* Throws an exception if the connection string format does not match any of the
|
||||
* available formats.
|
||||
*
|
||||
* @param {string} connectionString The invalid formatted connection string.
|
||||
* @return none
|
||||
*/
|
||||
exports.noMatchConnectionString = function (connectionString) {
|
||||
throw new NoMatchError('The provided connection string "' + connectionString + '" does not have complete configuration settings.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Throws an exception if the settings dont match any of the
|
||||
* available formats.
|
||||
*
|
||||
* @param {object} settings The invalid settings.
|
||||
* @return none
|
||||
*/
|
||||
exports.noMatchSettings = function (settings) {
|
||||
throw new NoMatchError('The provided settings ' + JSON.stringify(settings) + ' are not complete.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Parses the connection string and then validate that the parsed keys belong to
|
||||
* the validSettingKeys
|
||||
*
|
||||
* @param {string} connectionString The user provided connection string.
|
||||
* @param {array} validKeys The valid keys.
|
||||
* @return {array} The tokenized connection string keys.
|
||||
*/
|
||||
exports.parseAndValidateKeys = function (connectionString, validKeys) {
|
||||
var parsedConnectionString = { };
|
||||
|
||||
// parse key/value pairs from connection string
|
||||
var pairs = connectionString.split(';');
|
||||
for (var m = 0; m < pairs.length; m++) {
|
||||
if (pairs[m].length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var equalDex = pairs[m].indexOf('=');
|
||||
if (equalDex < 0) {
|
||||
throw new SyntaxError(SR.INVALID_CONNECTION_STRING);
|
||||
} else if (equalDex === 0) {
|
||||
// empty key name.
|
||||
throw new SyntaxError(SR.INVALID_CONNECTION_STRING_EMPTY_KEY);
|
||||
}
|
||||
|
||||
var key = pairs[m].substring(0, equalDex);
|
||||
|
||||
// assure that all given keys are valid.
|
||||
if (!azureUtil.inArrayInsensitive(key, validKeys)) {
|
||||
throw new SyntaxError(util.format(SR.INVALID_CONNECTION_STRING_BAD_KEY, key));
|
||||
}
|
||||
|
||||
var value = pairs[m].substring(equalDex + 1);
|
||||
|
||||
if(typeof parsedConnectionString[key] === 'undefined'){
|
||||
parsedConnectionString[key] = value;
|
||||
} else {
|
||||
// duplicate key name
|
||||
throw new SyntaxError(util.format(SR.INVALID_CONNECTION_STRING_DUPLICATE_KEY, key));
|
||||
}
|
||||
}
|
||||
|
||||
return parsedConnectionString;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an anonymous function that acts as predicate to perform a validation.
|
||||
*
|
||||
* @param array {requirements} The array of conditions to satisfy.
|
||||
* @param boolean {isRequired} Either these conditions are all required or all
|
||||
* optional.
|
||||
* @param boolean {atLeastOne} Indicates that at least one requirement must
|
||||
* succeed.
|
||||
* @return {function}
|
||||
*/
|
||||
exports.getValidator = function (requirements, isRequired, atLeastOne) {
|
||||
return function (userSettings) {
|
||||
var oneFound = false;
|
||||
var result = { };
|
||||
|
||||
for (var key in userSettings) {
|
||||
if (userSettings.hasOwnProperty(key)) {
|
||||
result[key.toLowerCase()] = userSettings[key];
|
||||
}
|
||||
}
|
||||
|
||||
for (var requirement in requirements) {
|
||||
if (requirements.hasOwnProperty(requirement)) {
|
||||
var settingName = requirements[requirement].SettingName.toLowerCase();
|
||||
|
||||
// Check if the setting name exists in the provided user settings.
|
||||
if (result[settingName]) {
|
||||
// Check if the provided user setting value is valid.
|
||||
var validationFunc = requirements[requirement].SettingConstraint;
|
||||
var isValid = validationFunc(result[settingName]);
|
||||
|
||||
if (isValid) {
|
||||
// Remove the setting as indicator for successful validation.
|
||||
delete result[settingName];
|
||||
oneFound = true;
|
||||
}
|
||||
} else if (isRequired) {
|
||||
// If required then fail because the setting does not exist
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (atLeastOne) {
|
||||
// At least one requirement must succeed, otherwise fail.
|
||||
return oneFound ? result : null;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a setting value condition that validates it is one of the
|
||||
* passed valid values.
|
||||
*
|
||||
* @param {string} name The setting key name.
|
||||
* @return {array}
|
||||
*/
|
||||
exports.setting = function (name) {
|
||||
var validValues = Array.prototype.slice.call(arguments, 1, arguments.length);
|
||||
|
||||
var predicate = function (settingValue) {
|
||||
var validValuesString = JSON.stringify(validValues);
|
||||
if (validValues.length === 0) {
|
||||
// No restrictions, succeed.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check to find if the settingValue is valid or not.
|
||||
for (var index = 0; index < validValues.length; index++) {
|
||||
if (settingValue.toString() == validValues[index].toString()) {
|
||||
// SettingValue is found in valid values set, succeed.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// settingValue is missing in valid values set, fail.
|
||||
throw new RangeError('The provided config value ' + settingValue + ' does not belong to the valid values subset:\n' + validValuesString);
|
||||
};
|
||||
|
||||
return exports.settingWithFunc(name, predicate);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an "at lease one" predicate for the provided list of requirements.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
exports.atLeastOne = function () {
|
||||
var allSettings = arguments;
|
||||
return exports.getValidator(allSettings, false, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an optional predicate for the provided list of requirements.
|
||||
*
|
||||
* @return {function}
|
||||
*/
|
||||
exports.optional = function () {
|
||||
var optionalSettings = arguments;
|
||||
return exports.getValidator(optionalSettings, false, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an required predicate for the provided list of requirements.
|
||||
*
|
||||
* @return {function}
|
||||
*/
|
||||
exports.allRequired = function () {
|
||||
var requiredSettings = arguments;
|
||||
return exports.getValidator(requiredSettings, true, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a setting value condition using the passed predicate.
|
||||
*
|
||||
* @param {string} name The setting key name.
|
||||
* @param {function} predicate The setting value predicate.
|
||||
* @return {array}
|
||||
*/
|
||||
exports.settingWithFunc = function (name, predicate) {
|
||||
var requirement = {};
|
||||
requirement.SettingName = name;
|
||||
requirement.SettingConstraint = predicate;
|
||||
|
||||
return requirement;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Tests to see if a given list of settings matches a set of filters exactly.
|
||||
*
|
||||
* @param array $settings The settings to check.
|
||||
* @return boolean If any filter returns null, false. If there are any settings
|
||||
* left over after all filters are processed, false. Otherwise true.
|
||||
*/
|
||||
exports.matchedSpecification = function (settings) {
|
||||
var constraints = Array.prototype.slice.call(arguments, 1, arguments.length);
|
||||
|
||||
for (var constraint in constraints) {
|
||||
if (constraints.hasOwnProperty(constraint)) {
|
||||
var remainingSettings = constraints[constraint](settings);
|
||||
|
||||
if (!remainingSettings) {
|
||||
return false;
|
||||
} else {
|
||||
settings = remainingSettings;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return azureUtil.objectKeysLength(settings) === 0;
|
||||
};
|
||||
+1355
File diff suppressed because it is too large.
Load diff
+420
@@ -0,0 +1,420 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
'use strict';
|
||||
|
||||
var _ = require('underscore');
|
||||
var url = require('url');
|
||||
|
||||
var util = require('../util/util');
|
||||
var ServiceSettings = require('./servicesettings');
|
||||
var Constants = require('../util/constants');
|
||||
var StorageServiceClientConstants = Constants.StorageServiceClientConstants;
|
||||
var ConnectionStringKeys = Constants.ConnectionStringKeys;
|
||||
var Validate = require('../util/validate');
|
||||
var SR = require('../util/sr');
|
||||
|
||||
var useDevelopmentStorageSetting = ServiceSettings.setting(ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, true);
|
||||
var developmentStorageProxyUriSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, Validate.isValidUri);
|
||||
var defaultEndpointsProtocolSetting = ServiceSettings.setting(ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, 'http', 'https');
|
||||
var accountNameSetting = ServiceSettings.setting(ConnectionStringKeys.ACCOUNT_NAME_NAME);
|
||||
var accountKeySetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.ACCOUNT_KEY_NAME, Validate.isBase64Encoded);
|
||||
var sasSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME, _.isString);
|
||||
|
||||
var blobEndpointSetting = ServiceSettings.settingWithFunc(
|
||||
ConnectionStringKeys.BLOB_ENDPOINT_NAME,
|
||||
Validate.isValidHost
|
||||
);
|
||||
|
||||
var queueEndpointSetting = ServiceSettings.settingWithFunc(
|
||||
ConnectionStringKeys.QUEUE_ENDPOINT_NAME,
|
||||
Validate.isValidHost
|
||||
);
|
||||
|
||||
var tableEndpointSetting = ServiceSettings.settingWithFunc(
|
||||
ConnectionStringKeys.TABLE_ENDPOINT_NAME,
|
||||
Validate.isValidHost
|
||||
);
|
||||
|
||||
var fileEndpointSetting = ServiceSettings.settingWithFunc(
|
||||
ConnectionStringKeys.FILE_ENDPOINT_NAME,
|
||||
Validate.isValidHost
|
||||
);
|
||||
|
||||
var endpointSuffixSetting = ServiceSettings.settingWithFunc(
|
||||
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME,
|
||||
Validate.isValidHost
|
||||
);
|
||||
|
||||
var validKeys = [
|
||||
ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME,
|
||||
ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME,
|
||||
ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME,
|
||||
ConnectionStringKeys.ACCOUNT_NAME_NAME,
|
||||
ConnectionStringKeys.ACCOUNT_KEY_NAME,
|
||||
ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME,
|
||||
ConnectionStringKeys.BLOB_ENDPOINT_NAME,
|
||||
ConnectionStringKeys.QUEUE_ENDPOINT_NAME,
|
||||
ConnectionStringKeys.TABLE_ENDPOINT_NAME,
|
||||
ConnectionStringKeys.FILE_ENDPOINT_NAME,
|
||||
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME
|
||||
];
|
||||
|
||||
/**
|
||||
* Creates new storage service settings instance.
|
||||
*
|
||||
* @param {string} name The storage service name.
|
||||
* @param {string} key The storage service key.
|
||||
* @param {string} sasToken The storage service shared access signature token.
|
||||
* @param {string} blobEndpoint The storage service blob endpoint.
|
||||
* @param {string} queueEndpoint The storage service queue endpoint.
|
||||
* @param {string} tableEndpoint The storage service table endpoint.
|
||||
* @param {string} fileEndpoint The storage service file endpoint.
|
||||
* @param {bool} usePathStyleUri Boolean value indicating wether to use path style uri or not.
|
||||
*/
|
||||
function StorageServiceSettings(name, key, sasToken, blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, usePathStyleUri) {
|
||||
this._name = name;
|
||||
this._key = key;
|
||||
|
||||
if (sasToken && sasToken[0] === '?') {
|
||||
this._sasToken = sasToken.slice(1);
|
||||
} else {
|
||||
this._sasToken = sasToken;
|
||||
}
|
||||
|
||||
this._blobEndpoint = blobEndpoint;
|
||||
this._queueEndpoint = queueEndpoint;
|
||||
this._tableEndpoint = tableEndpoint;
|
||||
this._fileEndpoint = fileEndpoint;
|
||||
|
||||
if (usePathStyleUri) {
|
||||
this._usePathStyleUri = usePathStyleUri;
|
||||
} else {
|
||||
this._usePathStyleUri = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a StorageServiceSettings object from the given connection string.
|
||||
*
|
||||
* @param {string} connectionString The storage settings connection string.
|
||||
* @return {StorageServiceSettings}
|
||||
*/
|
||||
StorageServiceSettings.createFromConnectionString = function (connectionString) {
|
||||
var tokenizedSettings = ServiceSettings.parseAndValidateKeys(connectionString, validKeys);
|
||||
|
||||
try {
|
||||
return StorageServiceSettings.createFromSettings(tokenizedSettings);
|
||||
} catch (e) {
|
||||
if (e instanceof ServiceSettings.NoMatchError) {
|
||||
// Replace no match settings exception by no match connection string one.
|
||||
ServiceSettings.noMatchConnectionString(connectionString);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
StorageServiceSettings.createExplicitly = function (storageAccount, storageAccessKey, host, sasToken, endpointSuffix) {
|
||||
var settings = {};
|
||||
function addIfNotNullOrEmpty(key, value){
|
||||
if(typeof value === 'string' && !util.stringIsEmpty(value)){
|
||||
settings[key] = value;
|
||||
} else if (typeof value == 'object' && !util.objectIsNull(value)) {
|
||||
settings[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoints
|
||||
if (host) {
|
||||
addIfNotNullOrEmpty('blobendpoint', host);
|
||||
addIfNotNullOrEmpty('tableendpoint', host);
|
||||
addIfNotNullOrEmpty('queueendpoint', host);
|
||||
addIfNotNullOrEmpty('fileendpoint', host);
|
||||
} else {
|
||||
addIfNotNullOrEmpty('defaultendpointsprotocol', ServiceSettings.DEFAULT_PROTOCOL.split(':', 1)[0]);
|
||||
}
|
||||
|
||||
addIfNotNullOrEmpty('accountname', storageAccount);
|
||||
addIfNotNullOrEmpty('accountkey', storageAccessKey);
|
||||
addIfNotNullOrEmpty('sharedaccesssignature', sasToken);
|
||||
addIfNotNullOrEmpty('endpointsuffix', endpointSuffix);
|
||||
|
||||
return StorageServiceSettings.createFromSettings(settings);
|
||||
};
|
||||
|
||||
StorageServiceSettings.createFromEnvironment = function () {
|
||||
var emulated = process.env[StorageServiceClientConstants.EnvironmentVariables.EMULATED];
|
||||
if (emulated) {
|
||||
return StorageServiceSettings.getDevelopmentStorageAccountSettings();
|
||||
}
|
||||
|
||||
var connectionString = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_CONNECTION_STRING];
|
||||
if (connectionString) {
|
||||
return StorageServiceSettings.createFromConnectionString(connectionString);
|
||||
}
|
||||
|
||||
var storageAccount = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_ACCOUNT];
|
||||
var storageAccessKey = process.env[StorageServiceClientConstants.EnvironmentVariables.AZURE_STORAGE_ACCESS_KEY];
|
||||
if(storageAccount && storageAccessKey){
|
||||
return StorageServiceSettings.createExplicitly(storageAccount, storageAccessKey, null, null, null);
|
||||
}
|
||||
|
||||
throw new Error(SR.NO_CREDENTIALS_PROVIDED);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a StorageServiceSettings object from a set of settings.
|
||||
*
|
||||
* @param {object} settings The settings object.
|
||||
* @return {StorageServiceSettings}
|
||||
*/
|
||||
StorageServiceSettings.createFromSettings = function (settings) {
|
||||
// Devstore case
|
||||
var matchedSpecs = ServiceSettings.matchedSpecification(
|
||||
settings,
|
||||
ServiceSettings.allRequired(useDevelopmentStorageSetting),
|
||||
ServiceSettings.optional(developmentStorageProxyUriSetting)
|
||||
);
|
||||
|
||||
if (matchedSpecs) {
|
||||
var proxyUri = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
return this.getDevelopmentStorageAccountSettings(proxyUri);
|
||||
}
|
||||
|
||||
// Account/Key automatic case
|
||||
matchedSpecs = ServiceSettings.matchedSpecification(
|
||||
settings,
|
||||
ServiceSettings.allRequired(
|
||||
defaultEndpointsProtocolSetting,
|
||||
accountNameSetting,
|
||||
accountKeySetting
|
||||
),
|
||||
ServiceSettings.optional(
|
||||
blobEndpointSetting,
|
||||
queueEndpointSetting,
|
||||
tableEndpointSetting,
|
||||
fileEndpointSetting,
|
||||
endpointSuffixSetting
|
||||
)
|
||||
);
|
||||
|
||||
if (matchedSpecs) {
|
||||
return this._createStorageServiceSettings(settings);
|
||||
}
|
||||
|
||||
// Account/Key explicit case
|
||||
matchedSpecs = ServiceSettings.matchedSpecification(
|
||||
settings,
|
||||
ServiceSettings.allRequired(
|
||||
accountNameSetting,
|
||||
accountKeySetting
|
||||
),
|
||||
ServiceSettings.atLeastOne(
|
||||
blobEndpointSetting,
|
||||
queueEndpointSetting,
|
||||
tableEndpointSetting,
|
||||
fileEndpointSetting,
|
||||
endpointSuffixSetting
|
||||
)
|
||||
);
|
||||
|
||||
if (matchedSpecs) {
|
||||
return this._createStorageServiceSettings(settings);
|
||||
}
|
||||
|
||||
// SAS case
|
||||
matchedSpecs = ServiceSettings.matchedSpecification(
|
||||
settings,
|
||||
ServiceSettings.allRequired(
|
||||
sasSetting
|
||||
),
|
||||
ServiceSettings.atLeastOne(
|
||||
blobEndpointSetting,
|
||||
queueEndpointSetting,
|
||||
tableEndpointSetting,
|
||||
fileEndpointSetting,
|
||||
endpointSuffixSetting
|
||||
)
|
||||
);
|
||||
|
||||
if(matchedSpecs) {
|
||||
return this._createStorageServiceSettings(settings);
|
||||
}
|
||||
|
||||
// anonymous explicit case
|
||||
// Only blob anonymous access is valid.
|
||||
matchedSpecs = ServiceSettings.matchedSpecification(
|
||||
settings,
|
||||
ServiceSettings.allRequired(
|
||||
blobEndpointSetting
|
||||
),
|
||||
ServiceSettings.optional(
|
||||
fileEndpointSetting,
|
||||
queueEndpointSetting,
|
||||
tableEndpointSetting,
|
||||
endpointSuffixSetting
|
||||
)
|
||||
);
|
||||
|
||||
if(matchedSpecs) {
|
||||
return this._createStorageServiceSettings(settings);
|
||||
}
|
||||
|
||||
ServiceSettings.noMatchSettings(settings);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a StorageServiceSettings with development storage credentials using
|
||||
* the specified proxy Uri.
|
||||
*
|
||||
* @param {string} proxyUri The proxy endpoint to use.
|
||||
* @return {StorageServiceSettings}
|
||||
*/
|
||||
StorageServiceSettings.getDevelopmentStorageAccountSettings = function (proxyUri) {
|
||||
if (!proxyUri) {
|
||||
proxyUri = StorageServiceClientConstants.DEV_STORE_URI;
|
||||
}
|
||||
|
||||
var parsedUri = url.parse(proxyUri);
|
||||
var scheme = parsedUri.protocol;
|
||||
var host = parsedUri.host;
|
||||
var prefix = scheme + '//' + host;
|
||||
|
||||
var blobEndpoint = {
|
||||
primaryHost: prefix + ':10000' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT,
|
||||
secondaryHost: prefix + ':10000' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary'
|
||||
};
|
||||
|
||||
var queueEndpoint = {
|
||||
primaryHost: prefix + ':10001' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT,
|
||||
secondaryHost: prefix + ':10001' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary'
|
||||
};
|
||||
|
||||
var tableEndpoint = {
|
||||
primaryHost: prefix + ':10002' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT,
|
||||
secondaryHost: prefix + ':10002' + '/' + StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT + '-secondary'
|
||||
};
|
||||
|
||||
return new StorageServiceSettings(
|
||||
StorageServiceClientConstants.DEVSTORE_STORAGE_ACCOUNT,
|
||||
StorageServiceClientConstants.DEVSTORE_STORAGE_ACCESS_KEY,
|
||||
null,
|
||||
blobEndpoint,
|
||||
queueEndpoint,
|
||||
tableEndpoint,
|
||||
null,
|
||||
true
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates StorageServiceSettings object given endpoints uri.
|
||||
*
|
||||
* @ignore
|
||||
* @param {array} settings The service settings.
|
||||
* @param {string} blobEndpointUri The blob endpoint uri.
|
||||
* @param {string} queueEndpointUri The queue endpoint uri.
|
||||
* @param {string} tableEndpointUri The table endpoint uri.
|
||||
* @param {string} fileEndpointUri The file endpoint uri.
|
||||
* @return {StorageServiceSettings}
|
||||
*/
|
||||
StorageServiceSettings._createStorageServiceSettings = function (settings) {
|
||||
var standardizeHost = function (host, accountName, scheme, dns){
|
||||
var storageHost;
|
||||
if (host) {
|
||||
storageHost = {};
|
||||
storageHost.primaryHost = _.isString(host) ? host : host.primaryHost;
|
||||
storageHost.secondaryHost = _.isString(host) ? undefined : host.secondaryHost;
|
||||
}
|
||||
|
||||
if (scheme && accountName && dns) {
|
||||
storageHost = storageHost ? storageHost : {};
|
||||
storageHost.primaryHost = storageHost.primaryHost ? storageHost.primaryHost : url.format({ protocol: scheme, hostname: accountName + '.' + dns});
|
||||
storageHost.secondaryHost = storageHost.secondaryHost ? storageHost.secondaryHost : url.format({ protocol: scheme, hostname: accountName + '-secondary.' + dns});
|
||||
}
|
||||
|
||||
return storageHost;
|
||||
};
|
||||
|
||||
var scheme = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
var accountName = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.ACCOUNT_NAME_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
var accountKey = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.ACCOUNT_KEY_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
var sasToken = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.SHARED_ACCESS_SIGNATURE_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
var endpointSuffix = util.tryGetValueInsensitive(
|
||||
ConnectionStringKeys.ENDPOINT_SUFFIX_NAME,
|
||||
settings
|
||||
);
|
||||
|
||||
var blobEndpoint = standardizeHost(
|
||||
util.tryGetValueInsensitive(ConnectionStringKeys.BLOB_ENDPOINT_NAME, settings),
|
||||
accountName,
|
||||
scheme,
|
||||
endpointSuffix ? 'blob.' + endpointSuffix : StorageServiceClientConstants.CLOUD_BLOB_HOST);
|
||||
|
||||
var queueEndpoint = standardizeHost(
|
||||
util.tryGetValueInsensitive(ConnectionStringKeys.QUEUE_ENDPOINT_NAME, settings),
|
||||
accountName,
|
||||
scheme,
|
||||
endpointSuffix ? 'queue.' + endpointSuffix : StorageServiceClientConstants.CLOUD_QUEUE_HOST);
|
||||
|
||||
var tableEndpoint = standardizeHost(
|
||||
util.tryGetValueInsensitive(ConnectionStringKeys.TABLE_ENDPOINT_NAME, settings),
|
||||
accountName,
|
||||
scheme,
|
||||
endpointSuffix ? 'table.' + endpointSuffix : StorageServiceClientConstants.CLOUD_TABLE_HOST);
|
||||
|
||||
var fileEndpoint = standardizeHost(
|
||||
util.tryGetValueInsensitive(ConnectionStringKeys.FILE_ENDPOINT_NAME, settings),
|
||||
accountName,
|
||||
scheme,
|
||||
endpointSuffix ? 'file.' + endpointSuffix : StorageServiceClientConstants.CLOUD_FILE_HOST);
|
||||
|
||||
|
||||
return new StorageServiceSettings(
|
||||
accountName,
|
||||
accountKey,
|
||||
sasToken,
|
||||
blobEndpoint,
|
||||
queueEndpoint,
|
||||
tableEndpoint,
|
||||
fileEndpoint
|
||||
);
|
||||
};
|
||||
|
||||
StorageServiceSettings.validKeys = validKeys;
|
||||
|
||||
exports = module.exports = StorageServiceSettings;
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var crypto = require('crypto');
|
||||
|
||||
/**
|
||||
* Creates a new HmacSHA256Sign object.
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function HmacSha256Sign(accessKey) {
|
||||
this._accessKey = accessKey;
|
||||
this._decodedAccessKey = new Buffer(this._accessKey, 'base64');
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a signature for the specified string using the HMAC-SHA256 algorithm.
|
||||
*
|
||||
* @param {string} stringToSign The UTF-8-encoded string to sign.
|
||||
* @return A String that contains the HMAC-SHA256-encoded signature.
|
||||
*/
|
||||
HmacSha256Sign.prototype.sign = function (stringToSign) {
|
||||
// Encoding the Signature
|
||||
// Signature=Base64(HMAC-SHA256(UTF8(StringToSign)))
|
||||
|
||||
return crypto.createHmac('sha256', this._decodedAccessKey).update(stringToSign, 'utf-8').digest('base64');
|
||||
};
|
||||
|
||||
module.exports = HmacSha256Sign;
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
var HmacSha256Sign = require('./hmacsha256sign');
|
||||
var Constants = require('./../util/constants');
|
||||
var SR = require('./../util/sr');
|
||||
|
||||
/**
|
||||
* Creates a new SharedAccessSignature object.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} sasToken The sasToken.
|
||||
*/
|
||||
function SharedAccessSignature(sasToken) {
|
||||
this.sasToken = sasToken;
|
||||
this.signer = new HmacSha256Sign(sasToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a request with the signature header.
|
||||
*
|
||||
* @this {SharedAccessSignature}
|
||||
* @param {WebResource} The webresource to be signed.
|
||||
* @param {function(error)} callback The callback function.
|
||||
*/
|
||||
SharedAccessSignature.prototype.signRequest = function (webResource, callback) {
|
||||
if (webResource.uri.indexOf('?') === -1) {
|
||||
webResource.uri += '?';
|
||||
} else {
|
||||
webResource.uri += '&';
|
||||
}
|
||||
|
||||
webResource.uri += this.sasToken;
|
||||
|
||||
// Add the api-version
|
||||
if (this.sasToken.indexOf('api-version') == -1) {
|
||||
webResource.uri += '&' + Constants.QueryStringConstants.API_VERSION + '=' + Constants.HeaderConstants.TARGET_STORAGE_VERSION;
|
||||
} else {
|
||||
throw new SyntaxError(SR.INVALID_SAS_TOKEN);
|
||||
}
|
||||
callback(null);
|
||||
};
|
||||
|
||||
module.exports = SharedAccessSignature;
|
||||
+500
@@ -0,0 +1,500 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
var qs = require('querystring');
|
||||
|
||||
var azureutil = require('../util/util');
|
||||
var HmacSha256Sign = require('./hmacsha256sign');
|
||||
var SR = require('../util/sr');
|
||||
var errors = require('../errors/errors');
|
||||
var ArgumentError = errors.ArgumentError;
|
||||
|
||||
var Constants = require('../util/constants');
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
var QueryStringConstants = Constants.QueryStringConstants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
var CompatibleVersionConstants = Constants.CompatibleVersionConstants;
|
||||
|
||||
/**
|
||||
* Creates a new SharedKey object.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} storageAccount The storage account.
|
||||
* @param {string} storageAccessKey The storage account's access key.
|
||||
* @param {bool} usePathStyleUri Boolean value indicating if the path, or the hostname, should include the storage account.
|
||||
*/
|
||||
function SharedKey(storageAccount, storageAccessKey, usePathStyleUri) {
|
||||
this.storageAccount = storageAccount;
|
||||
this.storageAccessKey = storageAccessKey;
|
||||
this.usePathStyleUri = usePathStyleUri;
|
||||
this.signer = new HmacSha256Sign(storageAccessKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the shared access signature for a account.
|
||||
* For more detailed information, refer to https://msdn.microsoft.com/en-us/library/azure/mt584140.aspx
|
||||
*
|
||||
* @param {object} sharedAccessPolicy The shared access policy.
|
||||
* @param {SharedAccessServices} sharedAccessPolicy.AccessPolicy.Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.Services`.
|
||||
* @param {SharedAccessResourceTypes} sharedAccessPolicy.AccessPolicy.ResourceTypes The resource type for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.ResourceTypes`.
|
||||
* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permissions for a shared access signature.
|
||||
* Refer to `Constants.AccountSasConstants.Permissions`.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Start The time at which the Shared Access Signature becomes valid.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.IPAddressOrRange An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.Protocols The protocols permitted for a request made with the account SAS.
|
||||
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http.
|
||||
* Refer to `Constants.AccountSasConstants.Protocols`.
|
||||
* @return {string} The shared access signature.
|
||||
*/
|
||||
SharedKey.prototype.generateAccountSignedQueryString = function (sharedAccessPolicy) {
|
||||
var addIfNotNull = function (queryString, name, value) {
|
||||
if (!azureutil.objectIsNull(name) && !azureutil.objectIsNull(value)) {
|
||||
queryString[name] = value;
|
||||
}
|
||||
};
|
||||
|
||||
var formatAccessPolicyDates = function (accessPolicy) {
|
||||
if (!azureutil.objectIsNull(accessPolicy.Start)) {
|
||||
if (!_.isDate(accessPolicy.Start)) {
|
||||
accessPolicy.Start = new Date(accessPolicy.Start);
|
||||
}
|
||||
|
||||
accessPolicy.Start = azureutil.truncatedISO8061Date(accessPolicy.Start);
|
||||
}
|
||||
|
||||
if (!azureutil.objectIsNull(accessPolicy.Expiry)) {
|
||||
if (!_.isDate(accessPolicy.Expiry)) {
|
||||
accessPolicy.Expiry = new Date(accessPolicy.Expiry);
|
||||
}
|
||||
|
||||
accessPolicy.Expiry = azureutil.truncatedISO8061Date(accessPolicy.Expiry);
|
||||
}
|
||||
};
|
||||
|
||||
var queryString = {};
|
||||
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_VERSION, HeaderConstants.TARGET_STORAGE_VERSION);
|
||||
|
||||
// add shared access policy params
|
||||
if (sharedAccessPolicy.AccessPolicy) {
|
||||
formatAccessPolicyDates(sharedAccessPolicy.AccessPolicy);
|
||||
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_SERVICES, sharedAccessPolicy.AccessPolicy.Services);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_RESOURCE_TYPES, sharedAccessPolicy.AccessPolicy.ResourceTypes);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_PERMISSIONS, sharedAccessPolicy.AccessPolicy.Permissions);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_START, sharedAccessPolicy.AccessPolicy.Start);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_EXPIRY, sharedAccessPolicy.AccessPolicy.Expiry);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_IP, sharedAccessPolicy.AccessPolicy.IPAddressOrRange);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_PROTOCOL, sharedAccessPolicy.AccessPolicy.Protocols);
|
||||
}
|
||||
|
||||
// add signature
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNATURE, this._generateAccountSharedAccessSignature(sharedAccessPolicy));
|
||||
|
||||
return qs.stringify(queryString);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Generates the signature part of the shared access signature for a account.
|
||||
* For more detailed information, refer to https://msdn.microsoft.com/en-us/library/azure/mt584140.aspx
|
||||
*
|
||||
* @param {object} sharedAccessPolicy The shared access policy.
|
||||
* @param {SharedAccessServices} sharedAccessPolicy.AccessPolicy.Services The services (blob, file, queue, table) for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.Services`.
|
||||
* @param {SharedAccessResourceTypes} sharedAccessPolicy.AccessPolicy.ResourceTypes The resource type for a shared access signature associated with this shared access policy.
|
||||
* Refer to `Constants.AccountSasConstants.ResourceTypes`.
|
||||
* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permissions for a shared access signature.
|
||||
* Refer to `Constants.AccountSasConstants.Permissions`.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Start The time at which the Shared Access Signature becomes valid.
|
||||
* @param {date} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.IPAddressOrRange An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive.
|
||||
* @param {string} sharedAccessPolicy.AccessPolicy.Protocols The protocols permitted for a request made with the account SAS.
|
||||
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http.
|
||||
* Refer to `Constants.AccountSasConstants.Protocols`.
|
||||
* @return {string} The signature part of the shared access signature.
|
||||
*/
|
||||
SharedKey.prototype._generateAccountSharedAccessSignature = function(sharedAccessPolicy){
|
||||
var getvalueToAppend = function (value, noNewLine) {
|
||||
var returnValue = '';
|
||||
if (!azureutil.objectIsNull(value)) {
|
||||
returnValue = value;
|
||||
}
|
||||
|
||||
if (noNewLine !== true) {
|
||||
returnValue += '\n';
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
};
|
||||
|
||||
var stringToSign = getvalueToAppend(this.storageAccount) +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Permissions : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Services : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.ResourceTypes : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Start : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Expiry : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.IPAddressOrRange : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Protocols : '') +
|
||||
getvalueToAppend(HeaderConstants.TARGET_STORAGE_VERSION);
|
||||
|
||||
return this.signer.sign(stringToSign);
|
||||
};
|
||||
|
||||
/**
|
||||
* Signs a request with the Authentication header.
|
||||
*
|
||||
* @param {WebResource} The webresource to be signed.
|
||||
* @param {function(error)} callback The callback function.
|
||||
*/
|
||||
SharedKey.prototype.signRequest = function (webResource, callback) {
|
||||
var getvalueToAppend = function (value, headerName) {
|
||||
// Do not sign content-length 0 in 2014-08-16 and later
|
||||
if (headerName === HeaderConstants.CONTENT_LENGTH && (azureutil.objectIsNull(value[headerName]) || value[headerName].toString() === '0')) {
|
||||
return '\n';
|
||||
} else if (azureutil.objectIsNull(value) || azureutil.objectIsNull(value[headerName])) {
|
||||
return '\n';
|
||||
} else {
|
||||
return value[headerName] + '\n';
|
||||
}
|
||||
};
|
||||
|
||||
var stringToSign =
|
||||
webResource.method + '\n' +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_ENCODING) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_LANGUAGE) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_LENGTH) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_MD5) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.CONTENT_TYPE) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.DATE) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.IF_MODIFIED_SINCE) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.IF_MATCH) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.IF_NONE_MATCH) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.IF_UNMODIFIED_SINCE) +
|
||||
getvalueToAppend(webResource.headers, HeaderConstants.RANGE) +
|
||||
this._getCanonicalizedHeaders(webResource) +
|
||||
this._getCanonicalizedResource(webResource);
|
||||
|
||||
var signature = this.signer.sign(stringToSign);
|
||||
|
||||
webResource.withHeader(HeaderConstants.AUTHORIZATION, 'SharedKey ' + this.storageAccount + ':' + signature);
|
||||
callback(null);
|
||||
};
|
||||
|
||||
/*
|
||||
* Retrieves the webresource's canonicalized resource string.
|
||||
* @param {WebResource} webResource The webresource to get the canonicalized resource string from.
|
||||
* @return {string} The canonicalized resource string.
|
||||
*/
|
||||
SharedKey.prototype._getCanonicalizedResource = function (webResource) {
|
||||
var path = '/';
|
||||
if (webResource.path) {
|
||||
path = webResource.path;
|
||||
}
|
||||
|
||||
var canonicalizedResource = '/' + this.storageAccount + path;
|
||||
|
||||
// Get the raw query string values for signing
|
||||
var queryStringValues = webResource.queryString;
|
||||
|
||||
// Build the canonicalized resource by sorting the values by name.
|
||||
if (queryStringValues) {
|
||||
var paramNames = [];
|
||||
Object.keys(queryStringValues).forEach(function (n) {
|
||||
paramNames.push(n);
|
||||
});
|
||||
|
||||
paramNames = paramNames.sort();
|
||||
Object.keys(paramNames).forEach(function (name) {
|
||||
canonicalizedResource += '\n' + paramNames[name] + ':' + queryStringValues[paramNames[name]];
|
||||
});
|
||||
}
|
||||
|
||||
return canonicalizedResource;
|
||||
};
|
||||
|
||||
/*
|
||||
* Constructs the Canonicalized Headers string.
|
||||
*
|
||||
* To construct the CanonicalizedHeaders portion of the signature string,
|
||||
* follow these steps: 1. Retrieve all headers for the resource that begin
|
||||
* with x-ms-, including the x-ms-date header. 2. Convert each HTTP header
|
||||
* name to lowercase. 3. Sort the headers lexicographically by header name,
|
||||
* in ascending order. Each header may appear only once in the
|
||||
* string. 4. Unfold the string by replacing any breaking white space with a
|
||||
* single space. 5. Trim any white space around the colon in the header. 6.
|
||||
* Finally, append a new line character to each canonicalized header in the
|
||||
* resulting list. Construct the CanonicalizedHeaders string by
|
||||
* concatenating all headers in this list into a single string.
|
||||
*
|
||||
* @param {object} The webresource object.
|
||||
* @return {string} The canonicalized headers.
|
||||
*/
|
||||
SharedKey.prototype._getCanonicalizedHeaders = function (webResource) {
|
||||
// Build canonicalized headers
|
||||
var canonicalizedHeaders = '';
|
||||
if (webResource.headers) {
|
||||
var canonicalizedHeadersArray = [];
|
||||
for (var header in webResource.headers) {
|
||||
if (header.indexOf(HeaderConstants.PREFIX_FOR_STORAGE) === 0) {
|
||||
var headerItem = { canonicalized: header.toLowerCase(), original: header };
|
||||
canonicalizedHeadersArray.push(headerItem);
|
||||
}
|
||||
}
|
||||
|
||||
canonicalizedHeadersArray.sort(function(a, b) { return a.canonicalized.localeCompare(b.canonicalized); });
|
||||
|
||||
_.each(canonicalizedHeadersArray, function (currentHeaderItem) {
|
||||
var value = webResource.headers[currentHeaderItem.original];
|
||||
if (!azureutil.IsNullOrEmptyOrUndefinedOrWhiteSpace(value)) {
|
||||
canonicalizedHeaders += currentHeaderItem.canonicalized + ':' + value + '\n';
|
||||
} else {
|
||||
canonicalizedHeaders += currentHeaderItem.canonicalized + ':\n';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return canonicalizedHeaders;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates the query string for a shared access signature signing.
|
||||
*
|
||||
* @this {SharedAccessSignature}
|
||||
* @param {string} serviceType The service type.
|
||||
* @param {string} path The path to the resource.
|
||||
* @param {object} sharedAccessPolicy The shared access policy.
|
||||
* @param {string} [sharedAccessPolicy.Id] The signed identifier.
|
||||
* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type.
|
||||
* @param {date} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid.
|
||||
* @param {date} [sharedAccessPolicy.AccessPolicy.Expiry] The time at which the Shared Access Signature becomes expired.
|
||||
* @param {string} [sharedAccessPolicy.AccessPolicy.IPAddressOrRange] An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive.
|
||||
* @param {string} [sharedAccessPolicy.AccessPolicy.Protocols] The protocols permitted for a request made with the account SAS.
|
||||
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http.
|
||||
* @param {string} sasVersion A string indicating the desired SAS Version to use, in storage service version format. Value must be 2012-02-12 or later.
|
||||
* @parma {ResourceTypes} [args.resourceType] The resource type, if the resource is a blob or container. Null if the resource is a queue or table.
|
||||
* @parma {ResourceTypes} [args.tableName] The table name, if the resource is a table. Null if the resource is a blob orqueue.
|
||||
* @parma {ResourceTypes} [args.queryString] The query string, if additional parameters are desired.
|
||||
* @param {object} [args.headers] The optional header values to set for a blob returned wth this SAS.
|
||||
* @param {string} [args.headers.CacheControl] The value of the Cache-Control response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentType] The value of the Content-Type response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentEncoding] The value of the Content-Encoding response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentLanguage] The value of the Content-Language response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentDisposition] The value of the Content-Disposition response header to be returned when this SAS is used.
|
||||
* @return {object} The shared access signature query string.
|
||||
*/
|
||||
SharedKey.prototype.generateSignedQueryString = function (serviceType, path, sharedAccessPolicy, sasVersion, args) {
|
||||
var addIfNotNull = function (queryString, name, value) {
|
||||
if (!azureutil.objectIsNull(name) && !azureutil.objectIsNull(value)) {
|
||||
queryString[name] = value;
|
||||
}
|
||||
};
|
||||
|
||||
var validateVersion = function (sasVersion) {
|
||||
// validate and add version
|
||||
if (azureutil.objectIsNull(sasVersion)) {
|
||||
return HeaderConstants.TARGET_STORAGE_VERSION;
|
||||
} else {
|
||||
var values = _.values(CompatibleVersionConstants);
|
||||
if (values.some(function(version) {
|
||||
return version.toLowerCase() === sasVersion.toLowerCase();
|
||||
})) {
|
||||
return sasVersion;
|
||||
} else {
|
||||
throw new ArgumentError('sasVersion', azureutil.stringFormat(SR.INVALID_SAS_VERSION, sasVersion, values));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var formatAccessPolicyDates = function (accessPolicy) {
|
||||
if (!azureutil.objectIsNull(accessPolicy.Start)) {
|
||||
if (!_.isDate(accessPolicy.Start)) {
|
||||
accessPolicy.Start = new Date(accessPolicy.Start);
|
||||
}
|
||||
|
||||
accessPolicy.Start = azureutil.truncatedISO8061Date(accessPolicy.Start);
|
||||
}
|
||||
|
||||
if (!azureutil.objectIsNull(accessPolicy.Expiry)) {
|
||||
if (!_.isDate(accessPolicy.Expiry)) {
|
||||
accessPolicy.Expiry = new Date(accessPolicy.Expiry);
|
||||
}
|
||||
|
||||
accessPolicy.Expiry = azureutil.truncatedISO8061Date(accessPolicy.Expiry);
|
||||
}
|
||||
};
|
||||
|
||||
// set up optional args
|
||||
var queryString;
|
||||
var resourceType;
|
||||
var headers;
|
||||
var tableName;
|
||||
|
||||
if(args) {
|
||||
queryString = args.queryString;
|
||||
resourceType = args.resourceType;
|
||||
tableName = args.tableName;
|
||||
headers = args.headers;
|
||||
}
|
||||
|
||||
if(!queryString) {
|
||||
queryString = {};
|
||||
}
|
||||
|
||||
// add shared access policy params
|
||||
if (sharedAccessPolicy.AccessPolicy) {
|
||||
formatAccessPolicyDates(sharedAccessPolicy.AccessPolicy);
|
||||
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_START, sharedAccessPolicy.AccessPolicy.Start);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_EXPIRY, sharedAccessPolicy.AccessPolicy.Expiry);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_PERMISSIONS, sharedAccessPolicy.AccessPolicy.Permissions);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_IP, sharedAccessPolicy.AccessPolicy.IPAddressOrRange);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_PROTOCOL, sharedAccessPolicy.AccessPolicy.Protocols);
|
||||
|
||||
// tables only
|
||||
addIfNotNull(queryString, QueryStringConstants.STARTPK, sharedAccessPolicy.AccessPolicy.StartPk);
|
||||
addIfNotNull(queryString, QueryStringConstants.ENDPK, sharedAccessPolicy.AccessPolicy.EndPk);
|
||||
addIfNotNull(queryString, QueryStringConstants.STARTRK, sharedAccessPolicy.AccessPolicy.StartRk);
|
||||
addIfNotNull(queryString, QueryStringConstants.ENDRK, sharedAccessPolicy.AccessPolicy.EndRk);
|
||||
}
|
||||
|
||||
// validate and add version
|
||||
var validatedSASVersionString = validateVersion(sasVersion);
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_VERSION, validatedSASVersionString);
|
||||
|
||||
// add signed identifier
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_IDENTIFIER, sharedAccessPolicy.Id);
|
||||
|
||||
// blobs only
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNED_RESOURCE, resourceType);
|
||||
if (headers) {
|
||||
addIfNotNull(queryString, QueryStringConstants.CACHE_CONTROL, headers.cacheControl);
|
||||
addIfNotNull(queryString, QueryStringConstants.CONTENT_TYPE, headers.contentType);
|
||||
addIfNotNull(queryString, QueryStringConstants.CONTENT_ENCODING, headers.contentEncoding);
|
||||
addIfNotNull(queryString, QueryStringConstants.CONTENT_LANGUAGE, headers.contentLanguage);
|
||||
addIfNotNull(queryString, QueryStringConstants.CONTENT_DISPOSITION, headers.contentDisposition);
|
||||
}
|
||||
|
||||
// tables only
|
||||
addIfNotNull(queryString, QueryStringConstants.TABLENAME, tableName);
|
||||
|
||||
// add signature
|
||||
addIfNotNull(queryString, QueryStringConstants.SIGNATURE, this._generateSignature(serviceType, path, sharedAccessPolicy, validatedSASVersionString, {resourceType: resourceType, headers: headers, tableName: tableName}));
|
||||
|
||||
return qs.stringify(queryString);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates the shared access signature for a resource.
|
||||
*
|
||||
* @this {SharedAccessSignature}
|
||||
* @param {string} serviceType The service type.
|
||||
* @param {string} path The path to the resource.
|
||||
* @param {object} sharedAccessPolicy The shared access policy.
|
||||
* @param {string} [sharedAccessPolicy.Id] The signed identifier.
|
||||
* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type.
|
||||
* @param {date} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid.
|
||||
* @param {date} [sharedAccessPolicy.AccessPolicy.Expiry] The time at which the Shared Access Signature becomes expired.
|
||||
* @param {string} [sharedAccessPolicy.AccessPolicy.IPAddressOrRange] An IP address or a range of IP addresses from which to accept requests. When specifying a range, note that the range is inclusive.
|
||||
* @param {string} [sharedAccessPolicy.AccessPolicy.Protocols] The protocols permitted for a request made with the account SAS.
|
||||
* Possible values are both HTTPS and HTTP (https,http) or HTTPS only (https). The default value is https,http.
|
||||
* @param {string} sasVersion A string indicating the desired SAS Version to use, in storage service version format. Value must be 2012-02-12 or later.
|
||||
* @parma {ResourceTypes} [args.resourceType] The resource type, if the resource is a blob or container. Null if the resource is a queue or table.
|
||||
* @parma {ResourceTypes} [args.tableName] The table name, if the resource is a table. Null if the resource is a blob or queue.
|
||||
* @param {object} [args.headers] The optional header values to set for a blob returned wth this SAS.
|
||||
* @param {string} [args.headers.CacheControl] The value of the Cache-Control response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentType] The value of the Content-Type response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentEncoding] The value of the Content-Encoding response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentLanguage] The value of the Content-Language response header to be returned when this SAS is used.
|
||||
* @param {string} [args.headers.ContentDisposition] The value of the Content-Disposition response header to be returned when this SAS is used.
|
||||
* @return {string} The shared access signature.
|
||||
*/
|
||||
SharedKey.prototype._generateSignature = function (serviceType, path, sharedAccessPolicy, sasVersion, args) {
|
||||
var getvalueToAppend = function (value, noNewLine) {
|
||||
var returnValue = '';
|
||||
if (!azureutil.objectIsNull(value)) {
|
||||
returnValue = value;
|
||||
}
|
||||
|
||||
if (noNewLine !== true) {
|
||||
returnValue += '\n';
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
};
|
||||
|
||||
// set up optional args
|
||||
var resourceType;
|
||||
var tableName;
|
||||
var headers;
|
||||
if(args) {
|
||||
resourceType = args.resourceType;
|
||||
tableName = args.tableName;
|
||||
headers = args.headers;
|
||||
}
|
||||
|
||||
// Add leading slash to path
|
||||
if (path.substr(0, 1) !== '/') {
|
||||
path = '/' + path;
|
||||
}
|
||||
|
||||
var canonicalizedResource;
|
||||
if (sasVersion === CompatibleVersionConstants.FEBRUARY_2012 || sasVersion === CompatibleVersionConstants.AUGUST_2013) {
|
||||
// Do not prepend service name for older versions
|
||||
canonicalizedResource = '/' + this.storageAccount + path;
|
||||
} else {
|
||||
canonicalizedResource = '/' + serviceType + '/' + this.storageAccount + path;
|
||||
}
|
||||
|
||||
var stringToSign = getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Permissions : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Start : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Expiry : '') +
|
||||
getvalueToAppend(canonicalizedResource) +
|
||||
getvalueToAppend(sharedAccessPolicy.Id) +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.IPAddressOrRange : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.Protocols : '') +
|
||||
sasVersion;
|
||||
|
||||
if(sasVersion == CompatibleVersionConstants.FEBRUARY_2012) {
|
||||
if(headers) {
|
||||
throw new ArgumentError('args.headers', SR.INVALID_HEADERS);
|
||||
}
|
||||
} else if (resourceType) {
|
||||
stringToSign += '\n' +
|
||||
getvalueToAppend(headers ? headers.cacheControl : '') +
|
||||
getvalueToAppend(headers ? headers.contentDisposition : '') +
|
||||
getvalueToAppend(headers ? headers.contentEncoding : '') +
|
||||
getvalueToAppend(headers ? headers.contentLanguage : '') +
|
||||
getvalueToAppend(headers ? headers.contentType : '', true);
|
||||
}
|
||||
|
||||
if(tableName) {
|
||||
stringToSign += '\n' +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.StartPk : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.StartRk : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.EndPk : '') +
|
||||
getvalueToAppend(sharedAccessPolicy.AccessPolicy ? sharedAccessPolicy.AccessPolicy.EndRk : '', true);
|
||||
}
|
||||
|
||||
return this.signer.sign(stringToSign);
|
||||
};
|
||||
|
||||
module.exports = SharedKey;
|
||||
+425
@@ -0,0 +1,425 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var util = require('util');
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var os = require('os');
|
||||
|
||||
var azureutil = require('../util/util');
|
||||
var Logger = require('../diagnostics/logger');
|
||||
var Constants = require('../util/constants');
|
||||
var errors = require('../errors/errors');
|
||||
var ArgumentError = errors.ArgumentError;
|
||||
|
||||
var DEFAULT_OPERATION_MEMORY_USAGE = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
|
||||
var DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES = Constants.BlobConstants.DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES;
|
||||
var DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES = Constants.BlobConstants.DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES;
|
||||
var DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES = Constants.BlobConstants.DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES;
|
||||
var DEFAULT_GLOBAL_CONCURRENCY = 5; //Default http connection limitation for nodejs
|
||||
|
||||
var SystemTotalMemory = os.totalmem();
|
||||
var CriticalFreeMemory = 0.1 * SystemTotalMemory;
|
||||
var nodeVersion = azureutil.getNodeVersion();
|
||||
|
||||
/**
|
||||
* Concurrently execute batch operations and call operation callback randomly or in sequence.
|
||||
* Random mode is for uploading.
|
||||
* 1. Fire user callback when the operation is done.
|
||||
* Sequence mode is for downloading.
|
||||
* 1. Fire user callback when the operation is done and all previous operations and callback has finished.
|
||||
* 2. BatchOperation guarantees the user callback is fired one by one.
|
||||
* 3. The next user callback can't be fired until the current one is completed.
|
||||
*/
|
||||
function BatchOperation(name, options) {
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
this.logger = options.logger || new Logger(Logger.LogLevels.INFO);
|
||||
this.operationMemoryUsage = options.operationMemoryUsage || DEFAULT_OPERATION_MEMORY_USAGE;
|
||||
this.callbackInOrder = options.callbackInOrder === true;
|
||||
this.callInOrder = options.callInOrder === true;
|
||||
this._currentOperationId = this.callbackInOrder ? 1 : -1;
|
||||
this.concurrency = DEFAULT_GLOBAL_CONCURRENCY;
|
||||
this.enableReuseSocket = (nodeVersion.major > 0 || nodeVersion.minor >= 10) && options.enableReuseSocket;
|
||||
|
||||
this._emitter = new EventEmitter();
|
||||
this._enableComplete = false;
|
||||
this._ended = false;
|
||||
this._error = null;
|
||||
this._paused = false;
|
||||
|
||||
//Total operations count(queued and active and connected)
|
||||
this._totalOperation = 0;
|
||||
|
||||
//Action operations count(The operations which are connecting to remote or executing callback or queued for executing)
|
||||
this._activeOperation = 0;
|
||||
|
||||
//Queued operations count(The operations which are connecting to remote or queued for executing)
|
||||
this._queuedOperation = 0;
|
||||
|
||||
//finished operation should be removed from this array
|
||||
this._operations = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation state
|
||||
*/
|
||||
var OperationState = {
|
||||
INITED : 'inited',
|
||||
QUEUED : 'queued',
|
||||
RUNNING : 'running',
|
||||
COMPLETE : 'complete',
|
||||
CALLBACK : 'callback',
|
||||
ERROR : 'error'
|
||||
};
|
||||
|
||||
BatchOperation.OperationState = OperationState;
|
||||
|
||||
/**
|
||||
* Set batch operation concurrency
|
||||
*/
|
||||
BatchOperation.prototype.setConcurrency = function(concurrency) {
|
||||
if (concurrency) {
|
||||
this.concurrency = concurrency;
|
||||
http.Agent.maxSockets = this.concurrency;
|
||||
https.Agent.maxSockets = this.concurrency;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Is the workload heavy and It can be used to determine whether we could queue operations
|
||||
*/
|
||||
BatchOperation.prototype.IsWorkloadHeavy = function() {
|
||||
//Only support one batch operation for now.
|
||||
//In order to work with the multiple batch operation, we can use global operation track objects
|
||||
//BatchOperation acquire a bunch of operation ids from global and allocated ids to RestOperation
|
||||
//RestOperation start to run in order of id
|
||||
var sharedRequest = 1;
|
||||
if(this.enableReuseSocket && !this.callInOrder) {
|
||||
sharedRequest = 2;
|
||||
}
|
||||
return this._activeOperation >= sharedRequest * this.concurrency || this._isLowMemory();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the approximate memory usage for batch operation.
|
||||
*/
|
||||
BatchOperation.prototype._getApproximateMemoryUsage = function() {
|
||||
var currentUsage = azureutil.isBrowser() ? DEFAULT_MINIMUM_MEMORY_USAGE_BROWSER_IN_BYTES : process.memoryUsage().rss; // Currently, we cannot get memory usage in browsers
|
||||
var futureUsage = this._queuedOperation * this.operationMemoryUsage;
|
||||
return currentUsage + futureUsage;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return whether in a low memory situation.
|
||||
*/
|
||||
BatchOperation.prototype._isLowMemory = function() {
|
||||
var approximateMemoryUsage = this._getApproximateMemoryUsage();
|
||||
return os.freemem() < CriticalFreeMemory ||
|
||||
(this._activeOperation >= this.concurrency && approximateMemoryUsage > 0.5 * SystemTotalMemory) ||
|
||||
(azureutil.is32() && approximateMemoryUsage > DEFAULT_CRITICAL_MEMORY_LIMITATION_32_IN_BYTES) ||
|
||||
(azureutil.isBrowser() && approximateMemoryUsage > DEFAULT_CRITICAL_MEMORY_LIMITATION_BROWSER_IN_BYTES);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a operation into batch operation
|
||||
*/
|
||||
BatchOperation.prototype.addOperation = function(operation) {
|
||||
this._operations.push(operation);
|
||||
operation.status = OperationState.QUEUED;
|
||||
operation.operationId = ++this._totalOperation;
|
||||
this._queuedOperation++;
|
||||
this.logger.debug(util.format('Add operation %d into batch operation %s. Active: %s; Queued: %s', operation.operationId, this.name, this._activeOperation, this._queuedOperation));
|
||||
//Immediately start the idle operation if workload isn't heavy
|
||||
this._runOperation(operation);
|
||||
return this.IsWorkloadHeavy();
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable batch operation complete when there is no operation to run.
|
||||
*/
|
||||
BatchOperation.prototype.enableComplete = function() {
|
||||
this._enableComplete = true;
|
||||
this.logger.debug(util.format('Enable batch operation %s complete', this.name));
|
||||
this._tryEmitEndEvent();
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop firing user call back
|
||||
*/
|
||||
BatchOperation.prototype.pause = function () {
|
||||
this._paused = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Start firing user call back
|
||||
*/
|
||||
BatchOperation.prototype.resume = function () {
|
||||
if (this._paused) {
|
||||
this._paused = false;
|
||||
this._fireOperationUserCallback();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
BatchOperation.prototype.on = function(event, listener) {
|
||||
// only emit end if the batch has completed all operations
|
||||
if(this._ended && event === 'end') {
|
||||
listener();
|
||||
} else {
|
||||
this._emitter.on(event, listener);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Run operation
|
||||
*/
|
||||
BatchOperation.prototype._runOperation = function (operation) {
|
||||
this.logger.debug(util.format('Operation %d start to run', operation.operationId));
|
||||
var cb = this.getBatchOperationCallback(operation);
|
||||
|
||||
if(this._error) {
|
||||
cb(this._error);//Directly call the callback with previous error.
|
||||
} else {
|
||||
operation.run(cb);
|
||||
}
|
||||
|
||||
this._activeOperation++;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return an general operation call back.
|
||||
* This callback is used to update the internal status and fire user's callback when operation is complete.
|
||||
*/
|
||||
BatchOperation.prototype.getBatchOperationCallback = function (operation) {
|
||||
var self = this;
|
||||
return function (error) {
|
||||
self._queuedOperation--;
|
||||
if (error) {
|
||||
operation.status = OperationState.ERROR;
|
||||
self.logger.debug(util.format('Operation %d failed. Error %s', operation.operationId, error));
|
||||
self._error = error;
|
||||
} else {
|
||||
operation.status = OperationState.CALLBACK;
|
||||
self.logger.debug(util.format('Operation %d succeed', operation.operationId));
|
||||
}
|
||||
|
||||
operation._callbackArguments = arguments;
|
||||
if (self._paused) {
|
||||
operation.status = OperationState.CALLBACK;
|
||||
self.logger.debug(util.format('Batch operation paused and Operation %d wait for firing callback', operation.operationId));
|
||||
} else if (self.callbackInOrder) {
|
||||
operation.status = OperationState.CALLBACK;
|
||||
if (self._currentOperationId === operation.operationId) {
|
||||
self._fireOperationUserCallback(operation);
|
||||
} else if (self._currentOperationId > operation.operationId) {
|
||||
throw new Error('Debug error: current callback operation id cannot be larger than operation id');
|
||||
} else {
|
||||
self.logger.debug(util.format('Operation %d is waiting for firing callback %s', operation.operationId, self._currentOperationId));
|
||||
}
|
||||
} else {
|
||||
self._fireOperationUserCallback(operation);
|
||||
}
|
||||
|
||||
self._tryEmitDrainEvent();
|
||||
operation = null;
|
||||
self = null;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Fire user's call back
|
||||
*/
|
||||
BatchOperation.prototype._fireOperationUserCallback = function (operation) {
|
||||
var index = this._getCallbackOperationIndex();
|
||||
if (!operation && index != -1) {
|
||||
operation = this._operations[index];
|
||||
}
|
||||
|
||||
if (operation && !this._paused) {
|
||||
// fire the callback, if exists
|
||||
if (operation._userCallback) {
|
||||
this.logger.debug(util.format('Fire user call back for operation %d', operation.operationId));
|
||||
// make sure UserCallback is a sync operation in sequence mode.
|
||||
// both async and sync operations are available for random mode.
|
||||
operation._fireUserCallback();
|
||||
}
|
||||
|
||||
// remove the operation from the array and decrement the counter
|
||||
this._operations.splice(index, 1);
|
||||
this._activeOperation--;
|
||||
operation.status = OperationState.COMPLETE;
|
||||
index = operation = null;
|
||||
|
||||
if (this.callbackInOrder) {
|
||||
this._currentOperationId++;
|
||||
}
|
||||
|
||||
this._fireOperationUserCallback();
|
||||
} else if (this._paused) {
|
||||
this._tryEmitDrainEvent();
|
||||
} else {
|
||||
// check if batch has ended and if so emit end event
|
||||
this._tryEmitEndEvent();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Try to emit the BatchOperation end event
|
||||
* End event means all the operation and callback already finished.
|
||||
*/
|
||||
BatchOperation.prototype._tryEmitEndEvent = function () {
|
||||
if(this._enableComplete && this._activeOperation === 0 && this._operations.length === 0) {
|
||||
this._ended = true;
|
||||
this.logger.debug(util.format('Batch operation %s emits the end event', this.name));
|
||||
this._emitter.emit('end', this._error, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Workaround to recover from the 'hang' edge case. _tryEmitEndEvent function is not supposed to be called if the bacth is not really completed.
|
||||
this._tryEmitDrainEvent();
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Try to emit the drain event
|
||||
*/
|
||||
BatchOperation.prototype._tryEmitDrainEvent = function () {
|
||||
if (!this._emitter) return false;
|
||||
if(!this.IsWorkloadHeavy() || this._activeOperation < this.concurrency) {
|
||||
this._emitter.emit('drain');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current active operation index.
|
||||
* Only the active operation could call user's callback in sequence model.
|
||||
* The other finished but not active operations should wait for wake up.
|
||||
*/
|
||||
BatchOperation.prototype._getCallbackOperationIndex = function () {
|
||||
var operation = null;
|
||||
for (var i = 0; i < this._operations.length; i++) {
|
||||
operation = this._operations[i];
|
||||
if (this.callbackInOrder) {
|
||||
//Sequence mode
|
||||
if (operation.operationId == this._currentOperationId) {
|
||||
if (operation.status === OperationState.CALLBACK) {
|
||||
return i;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//Random mode
|
||||
if (operation.status === OperationState.CALLBACK) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Do nothing and directly call the callback.
|
||||
* In random mode, the user callback will be called immediately
|
||||
* In sequence mode, the user callback will be called after the previous callback has been called
|
||||
*/
|
||||
BatchOperation.noOperation = function (cb) {
|
||||
cb();
|
||||
};
|
||||
|
||||
/**
|
||||
* Rest operation in sdk
|
||||
*/
|
||||
function RestOperation(serviceClient, operation) {
|
||||
this.status = OperationState.Inited;
|
||||
this.operationId = -1;
|
||||
this._callbackArguments = null;
|
||||
|
||||
// setup callback and args
|
||||
this._userCallback = arguments[arguments.length - 1];
|
||||
var sliceEnd = arguments.length;
|
||||
if(azureutil.objectIsFunction(this._userCallback)) {
|
||||
sliceEnd--;
|
||||
} else {
|
||||
this._userCallback = null;
|
||||
}
|
||||
var operationArguments = Array.prototype.slice.call(arguments).slice(2, sliceEnd);
|
||||
|
||||
this.run = function(cb) {
|
||||
var func = serviceClient[operation];
|
||||
if(!func) {
|
||||
throw new ArgumentError('operation', util.format('Unknown operation %s in serviceclient', operation));
|
||||
} else {
|
||||
if(!cb) cb = this._userCallback;
|
||||
operationArguments.push(cb);
|
||||
this.status = OperationState.RUNNING;
|
||||
func.apply(serviceClient, operationArguments);
|
||||
operationArguments = operation = null;
|
||||
}
|
||||
};
|
||||
|
||||
this._fireUserCallback = function () {
|
||||
if(this._userCallback) {
|
||||
this._userCallback.apply(null, this._callbackArguments);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
BatchOperation.RestOperation = RestOperation;
|
||||
|
||||
/**
|
||||
* Common operation wrapper
|
||||
*/
|
||||
function CommonOperation(operationFunc, callback) {
|
||||
this.status = OperationState.Inited;
|
||||
this.operationId = -1;
|
||||
this._callbackArguments = null;
|
||||
var sliceStart = 2;
|
||||
if (azureutil.objectIsFunction(callback)) {
|
||||
this._userCallback = callback;
|
||||
} else {
|
||||
this._userCallback = null;
|
||||
sliceStart = 1;
|
||||
}
|
||||
var operationArguments = Array.prototype.slice.call(arguments).slice(sliceStart);
|
||||
this.run = function (cb) {
|
||||
if (!cb) cb = this._userCallback;
|
||||
operationArguments.push(cb);
|
||||
this.status = OperationState.RUNNING;
|
||||
operationFunc.apply(null, operationArguments);
|
||||
operationArguments = operationFunc = null;
|
||||
};
|
||||
|
||||
this._fireUserCallback = function () {
|
||||
if (this._userCallback) {
|
||||
this._userCallback.apply(null, this._callbackArguments);
|
||||
}
|
||||
this._userCallback = this._callbackArguments = null;
|
||||
};
|
||||
}
|
||||
|
||||
BatchOperation.CommonOperation = CommonOperation;
|
||||
|
||||
module.exports = BatchOperation;
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var buffer = require('buffer').Buffer;
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
var Constants = require('../util/constants');
|
||||
var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
|
||||
|
||||
function BrowserFileReadStream(file, options) {
|
||||
stream.Readable.call(this, options);
|
||||
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this._fileReader = new FileReader(file); // HTML FileReader
|
||||
this._file = file;
|
||||
this._size = file.size;
|
||||
this._highWaterMark = options.highWaterMark || bufferSize;
|
||||
this._offset = 0;
|
||||
var self = this;
|
||||
|
||||
this._fileReader.onloadend = function (event) {
|
||||
var data = event.target.result;
|
||||
var buf = buffer.from(data);
|
||||
self.push(buf);
|
||||
};
|
||||
|
||||
this._fileReader.onerror = function (error) {
|
||||
self.emit('error', error);
|
||||
};
|
||||
}
|
||||
util.inherits(BrowserFileReadStream, stream.Readable);
|
||||
|
||||
BrowserFileReadStream.prototype._read = function() {
|
||||
if (this._offset >= this._size) {
|
||||
this.push(null);
|
||||
} else {
|
||||
var end = this._offset + this._highWaterMark;
|
||||
var slice = this._file.slice(this._offset, end);
|
||||
this._fileReader.readAsArrayBuffer(slice);
|
||||
this._offset = end;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = BrowserFileReadStream;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
function BufferStream(buffer, options) {
|
||||
stream.Readable.call(this, options);
|
||||
|
||||
this._buffer = buffer;
|
||||
this._offset = 0;
|
||||
this._chunkSize = 4 * 1024 * 1024;
|
||||
this._bufferSize = buffer.length;
|
||||
}
|
||||
|
||||
util.inherits(BufferStream, stream.Readable);
|
||||
|
||||
BufferStream.prototype._read = function () {
|
||||
while (this.push(this._readNextChunk())) {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
BufferStream.prototype._readNextChunk = function () {
|
||||
var data = null;
|
||||
|
||||
if (this._offset < this._bufferSize) {
|
||||
var end = this._offset + this._chunkSize;
|
||||
end = end > this._bufferSize ? this._bufferSize : end;
|
||||
data = this._buffer.slice(this._offset, end);
|
||||
this._offset = end;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
module.exports = BufferStream;
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/**
|
||||
* Chunked memory pool allocator.
|
||||
* It could dramatically reduce the memory usage.
|
||||
* However, it can't dramatically reduce the CPU time since GC in v8 is very efficient.
|
||||
*/
|
||||
function ChunkAllocator(chunkSize, maxCount) {
|
||||
// Track the unused buffers and number of used buffers
|
||||
this._pool = [];
|
||||
this._inuse = 0;
|
||||
|
||||
// Buffer size
|
||||
this._chunkSize = chunkSize;
|
||||
|
||||
// If total memory is larger than this._chunkSize * this._maxCount, the buffer pool is not used.
|
||||
this._maxCount = maxCount || 10;
|
||||
|
||||
// Immediately add a buffer to the pool.
|
||||
this._extendMemoryPool();
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously require a buffer
|
||||
* Caller should be aware of that the content of buffer is random since the Buffer.fill is Time-consumed opreation.
|
||||
*/
|
||||
ChunkAllocator.prototype.getBuffer = function(size) {
|
||||
var buffer = this._getBufferFromPool(size);
|
||||
if (buffer === null) {
|
||||
// Either the total memory is larger than this._chunkSize * this._maxCount
|
||||
// Or, the size does not match the chunk size of the pool
|
||||
buffer = new Buffer(size);
|
||||
}
|
||||
|
||||
this._inuse++;
|
||||
return buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get buffer from the current memory pool.
|
||||
*/
|
||||
ChunkAllocator.prototype._getBufferFromPool = function(size) {
|
||||
// Return null if the given size does not match the chunk size of the buffer pool.
|
||||
if(size !== this._chunkSize) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extend the memory pool if it is empty.
|
||||
if(this._pool.length === 0) {
|
||||
this._extendMemoryPool();
|
||||
}
|
||||
|
||||
// If the pool is not empty, return a buffer.
|
||||
if(this._pool.length !== 0) {
|
||||
return this._pool.pop();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Extend the memory pool if the maximum size has not been reached.
|
||||
*/
|
||||
ChunkAllocator.prototype._extendMemoryPool = function() {
|
||||
var total = this._pool.length + this._inuse;
|
||||
|
||||
// If the total is larger than the max, do not allocate more memory.
|
||||
if(total >= this._maxCount) return;
|
||||
|
||||
// Calculate the new number of buffers, equal to the total*2 bounded by 1 and the maxCount
|
||||
var nextSize = Math.min(total * 2, this._maxCount) || 1;
|
||||
|
||||
// Add more buffers.
|
||||
var increment = nextSize - total;
|
||||
for(var i = 0; i < increment; i++) {
|
||||
var buffer = new Buffer(this._chunkSize);
|
||||
this._pool.push(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Release the buffer.
|
||||
*/
|
||||
ChunkAllocator.prototype.releaseBuffer = function(buffer) {
|
||||
if(buffer.length !== this._chunkSize) {
|
||||
// Directly delete the buffer if bufferSize is invalid and wait for GC.
|
||||
buffer = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the buffer to the pool if it is not full, otherwise delete it
|
||||
if (this._pool.length < this._maxCount) {
|
||||
this._pool.push(buffer);
|
||||
} else {
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
// Decrement _inuse
|
||||
this._inuse--;
|
||||
|
||||
// _inuse could be below zero if a buffer is released which was not returned by getBuffer
|
||||
if(this._inuse < 0) {
|
||||
this._inuse = 0;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroy ChunkAllocator.
|
||||
*/
|
||||
ChunkAllocator.prototype.destroy = function() {
|
||||
this._pool = [];
|
||||
this._inuse = 0;
|
||||
};
|
||||
|
||||
module.exports = ChunkAllocator;
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
var azureutil = require('../util/util');
|
||||
var Md5Wrapper = require('../md5-wrapper');
|
||||
var Constants = require('../util/constants');
|
||||
var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
|
||||
|
||||
/**
|
||||
* Chunk stream
|
||||
* 1. Calculate md5
|
||||
* 2. Track reading offset
|
||||
* 3. Work with customize memory allocator
|
||||
* 4. Buffer data from stream.
|
||||
* @param {object} options stream.Readable options
|
||||
*/
|
||||
function ChunkStream(options) {
|
||||
stream.Stream.call(this);
|
||||
this.writable = this.readable = true;
|
||||
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this._highWaterMark = options.highWaterMark || bufferSize;
|
||||
|
||||
this._paused = undefined; //True/false is the external status from users.
|
||||
|
||||
this._isStreamOpened = false;
|
||||
this._offset = 0;
|
||||
this._allocator = options.allocator;
|
||||
this._streamEnded = false;
|
||||
this._md5hash = null;
|
||||
this._buffer = null;
|
||||
this._internalBufferSize = 0;
|
||||
this._outputLengthLimit = 0;
|
||||
this._md5sum = undefined;
|
||||
|
||||
if (options.calcContentMd5) {
|
||||
this._md5hash = new Md5Wrapper().createMd5Hash();
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(ChunkStream, stream.Stream);
|
||||
|
||||
/**
|
||||
* Set the memory allocator.
|
||||
*/
|
||||
ChunkStream.prototype.setMemoryAllocator = function(allocator) {
|
||||
this._allocator = allocator;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the output length.
|
||||
*/
|
||||
ChunkStream.prototype.setOutputLength = function(length) {
|
||||
if (length) {
|
||||
this._outputLengthLimit = length;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal stream ended
|
||||
*/
|
||||
ChunkStream.prototype.end = function (chunk, encoding, cb) {
|
||||
if (typeof chunk === 'function') {
|
||||
cb = chunk;
|
||||
chunk = null;
|
||||
encoding = null;
|
||||
} else if (typeof encoding === 'function') {
|
||||
cb = encoding;
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
if (chunk) {
|
||||
this.write(chunk, encoding);
|
||||
}
|
||||
|
||||
this._streamEnded = true;
|
||||
this._flushInternalBuffer();
|
||||
|
||||
if (cb) {
|
||||
this.once('end', cb);
|
||||
}
|
||||
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
ChunkStream.prototype.finish = function () {
|
||||
this.emit('finish');
|
||||
|
||||
this.destroy();
|
||||
};
|
||||
|
||||
ChunkStream.prototype.error = function () {
|
||||
this.emit('error');
|
||||
|
||||
this.destroy();
|
||||
};
|
||||
|
||||
ChunkStream.prototype.destroy = function () {
|
||||
this.writable = this.readable = false;
|
||||
|
||||
if (this._allocator && azureutil.objectIsFunction(this._allocator.destroy)) {
|
||||
this._allocator.destroy();
|
||||
}
|
||||
|
||||
this.emit('close');
|
||||
};
|
||||
|
||||
ChunkStream.prototype.stop = function () {
|
||||
this.destroy();
|
||||
this._streamEnded = true;
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
ChunkStream.prototype.write = function (chunk, encoding) {
|
||||
if (!this._isStreamOpened) {
|
||||
this._isStreamOpened = true;
|
||||
}
|
||||
|
||||
this._buildChunk(chunk, encoding);
|
||||
|
||||
return !this._paused;
|
||||
};
|
||||
|
||||
/**
|
||||
* Buffer the data into a chunk and emit it
|
||||
*/
|
||||
ChunkStream.prototype._buildChunk = function (data) {
|
||||
if (typeof data == 'string') {
|
||||
data = new Buffer(data);
|
||||
}
|
||||
var dataSize = data.length;
|
||||
var dataOffset = 0;
|
||||
do {
|
||||
var buffer = null;
|
||||
var targetSize = this._internalBufferSize + dataSize;
|
||||
|
||||
if (targetSize < this._highWaterMark) {
|
||||
// add the data to the internal buffer and return as it is not yet full
|
||||
this._copyToInternalBuffer(data, dataOffset, data.length);
|
||||
return;
|
||||
} else if (targetSize == this._highWaterMark){
|
||||
var canReleaseInnerStreamBuffer = this._stream && this._stream._allocator && this._stream._allocator.releaseBuffer;
|
||||
if(this._internalBufferSize === 0 && data.length === this._highWaterMark && !canReleaseInnerStreamBuffer) {
|
||||
// set the buffer to the data passed in to avoid creating a new buffer
|
||||
buffer = data;
|
||||
} else {
|
||||
// add the data to the internal buffer and pop that buffer
|
||||
this._copyToInternalBuffer(data, dataOffset, data.length);
|
||||
buffer = this._popInternalBuffer();
|
||||
}
|
||||
dataSize = 0;
|
||||
} else {
|
||||
// add data to the internal buffer until its full, then return it
|
||||
// set the dataSize parameter so that additional data is not lost
|
||||
var copySize = this._highWaterMark - this._internalBufferSize;
|
||||
this._copyToInternalBuffer(data, dataOffset, dataOffset + copySize);
|
||||
dataSize -= copySize;
|
||||
dataOffset += copySize;
|
||||
buffer = this._popInternalBuffer();
|
||||
}
|
||||
this._emitBufferData(buffer);
|
||||
} while(dataSize > 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit the buffer
|
||||
*/
|
||||
ChunkStream.prototype._emitBufferData = function(buffer) {
|
||||
var newOffset = this._offset + buffer.length;
|
||||
var range = {
|
||||
start : this._offset,
|
||||
end : newOffset - 1,
|
||||
size : buffer.length
|
||||
};
|
||||
|
||||
this._offset = newOffset;
|
||||
|
||||
if (this._outputLengthLimit > 0) {
|
||||
// When the start postion is larger than the limit, no data will be consumed though there is an event to be emitted.
|
||||
// So the buffer should not be calculated.
|
||||
if (range.start <= this._outputLengthLimit) {
|
||||
if (this._offset > this._outputLengthLimit) {
|
||||
// Don't use negative end parameter which means the index starting from the end of the buffer
|
||||
// to be compatible with node 0.8.
|
||||
buffer = buffer.slice(0, buffer.length - (this._offset - this._outputLengthLimit));
|
||||
}
|
||||
if (this._md5hash) {
|
||||
this._md5hash.update(buffer);
|
||||
}
|
||||
}
|
||||
} else if (this._md5hash) {
|
||||
this._md5hash.update(buffer);
|
||||
}
|
||||
|
||||
this.emit('data', buffer, range);
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy data into internal buffer
|
||||
*/
|
||||
ChunkStream.prototype._copyToInternalBuffer = function(data, start, end) {
|
||||
if(start === undefined) start = 0;
|
||||
if(end === undefined) end = data.length;
|
||||
if (!this._buffer) {
|
||||
this._buffer = this._allocateNewBuffer();
|
||||
this._internalBufferSize = 0;
|
||||
}
|
||||
var copied = data.copy(this._buffer, this._internalBufferSize, start, end);
|
||||
this._internalBufferSize += copied;
|
||||
|
||||
if (this._stream && this._stream._allocator && this._stream._allocator.releaseBuffer) {
|
||||
this._stream._allocator.releaseBuffer(data);
|
||||
}
|
||||
|
||||
if(copied != (end - start)) {
|
||||
throw new Error('Can not copy entire data to buffer');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Flush internal buffer
|
||||
*/
|
||||
ChunkStream.prototype._flushInternalBuffer = function() {
|
||||
var buffer = this._popInternalBuffer();
|
||||
if (buffer) {
|
||||
this._emitBufferData(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Pop internal buffer
|
||||
*/
|
||||
ChunkStream.prototype._popInternalBuffer = function () {
|
||||
var buf = null;
|
||||
if (!this._buffer || this._internalBufferSize === 0) {
|
||||
buf = null;
|
||||
} else if(this._internalBufferSize == this._highWaterMark) {
|
||||
buf = this._buffer;
|
||||
} else {
|
||||
buf = this._buffer.slice(0, this._internalBufferSize);
|
||||
}
|
||||
|
||||
this._buffer = null;
|
||||
this._internalBufferSize = 0;
|
||||
|
||||
return buf;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allocate a buffer
|
||||
*/
|
||||
ChunkStream.prototype._allocateNewBuffer = function() {
|
||||
var size = this._highWaterMark;
|
||||
if(this._allocator && azureutil.objectIsFunction(this._allocator.getBuffer)) {
|
||||
return this._allocator.getBuffer(size);
|
||||
} else {
|
||||
var buffer = new Buffer(size);
|
||||
return buffer;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get file content md5 when read completely.
|
||||
*/
|
||||
ChunkStream.prototype.getContentMd5 = function(encoding) {
|
||||
if (!encoding) encoding = 'base64';
|
||||
if(!this._md5hash) {
|
||||
throw new Error('Can\'t get content md5, please set the calcContentMd5 option for FileReadStream.');
|
||||
} else {
|
||||
if (this._streamEnded) {
|
||||
if (!this._md5sum) {
|
||||
this._md5sum = this._md5hash.digest(encoding);
|
||||
}
|
||||
return this._md5sum;
|
||||
} else {
|
||||
throw new Error('Stream has not ended.');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Pause chunk stream
|
||||
*/
|
||||
ChunkStream.prototype.pause = function() {
|
||||
this._paused = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resume read stream
|
||||
*/
|
||||
ChunkStream.prototype.resume = function() {
|
||||
if (this._paused) {
|
||||
this._paused = false;
|
||||
|
||||
this.emit('drain');
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ChunkStream;
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var ChunkStream = require('./chunkstream');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var util = require('util');
|
||||
var azureutil = require('./../util/util');
|
||||
|
||||
/**
|
||||
* Chunk stream
|
||||
* 1. Calculate md5
|
||||
* 2. Track reading offset
|
||||
* 3. Work with customize memory allocator
|
||||
* 4. Buffer data from stream.
|
||||
* @param {object} options stream.Readable options
|
||||
*/
|
||||
function ChunkStreamWithStream(stream, options) {
|
||||
ChunkStream.call(this, options);
|
||||
|
||||
stream.pause(); // Pause stream and wait for data listener. It's useful for node v0.8
|
||||
this._stream = stream;
|
||||
this._stream.on('end', this.end.bind(this)); // Should catch the end event for node v0.8
|
||||
}
|
||||
|
||||
util.inherits(ChunkStreamWithStream, ChunkStream);
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
ChunkStreamWithStream.prototype.on = function(event, listener) {
|
||||
if(event === 'end' && this._streamEnded) {
|
||||
listener(); //Directly call the end event when stream already ended
|
||||
} else {
|
||||
EventEmitter.prototype.on.call(this, event, listener);
|
||||
}
|
||||
|
||||
if (event === 'data') {
|
||||
if (!this._isStreamOpened) {
|
||||
this._isStreamOpened = true;
|
||||
this._stream.on('data', this._buildChunk.bind(this));
|
||||
}
|
||||
if (this._paused === undefined) {
|
||||
this._stream.resume();
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop stream from external
|
||||
*/
|
||||
ChunkStreamWithStream.prototype.stop = function (chunk, encoding, cb) {
|
||||
if (azureutil.objectIsFunction(this._stream.destroy)) {
|
||||
this._stream.destroy();
|
||||
} else {
|
||||
this.pause();
|
||||
}
|
||||
ChunkStream.prototype.end.call(this, chunk, encoding, cb);
|
||||
};
|
||||
|
||||
/**
|
||||
* Pause chunk stream
|
||||
*/
|
||||
ChunkStreamWithStream.prototype.pause = function () {
|
||||
ChunkStream.prototype.pause.call(this);
|
||||
|
||||
this._stream.pause();
|
||||
};
|
||||
|
||||
/**
|
||||
* Resume read stream
|
||||
*/
|
||||
ChunkStreamWithStream.prototype.resume = function() {
|
||||
ChunkStream.prototype.resume.call(this);
|
||||
|
||||
this._stream.resume();
|
||||
};
|
||||
|
||||
ChunkStreamWithStream.prototype.finish = function () {
|
||||
ChunkStream.prototype.finish.call(this);
|
||||
this._stream.emit.call(this._stream, 'finish');
|
||||
};
|
||||
|
||||
ChunkStreamWithStream.prototype.destroy = function () {
|
||||
ChunkStream.prototype.destroy.call(this);
|
||||
this._stream.emit.call(this._stream, 'close');
|
||||
};
|
||||
|
||||
module.exports = ChunkStreamWithStream;
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
var fs = require('fs');
|
||||
var validator = require('validator');
|
||||
|
||||
var Md5Wrapper = require('../md5-wrapper');
|
||||
var Constants = require('../util/constants');
|
||||
var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/**
|
||||
* File read stream
|
||||
* 1. Calculate md5
|
||||
* 2. Track reading offset
|
||||
* 3. Work with customize memory allocator
|
||||
* 4. Buffer data from stream.
|
||||
* @param {object} options stream.Readable options
|
||||
*/
|
||||
function FileReadStream(path, options) {
|
||||
stream.Stream.call(this);
|
||||
this.readable = true;
|
||||
|
||||
if(!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this._destroyed = false;
|
||||
this._streamEnded = false;
|
||||
this._fd = null;
|
||||
this._fileName = undefined;
|
||||
this._highWaterMark = options.highWaterMark || bufferSize;
|
||||
this._offset = 0;
|
||||
this._paused = undefined;
|
||||
this._allocator = options.allocator;
|
||||
this._fileName = path;
|
||||
|
||||
this._md5hash = null;
|
||||
this._md5sum = undefined;
|
||||
|
||||
if (options.calcContentMd5) {
|
||||
this._md5hash = new Md5Wrapper().createMd5Hash();
|
||||
}
|
||||
|
||||
this._open();
|
||||
}
|
||||
|
||||
util.inherits(FileReadStream, stream.Stream);
|
||||
|
||||
/**
|
||||
* Open file
|
||||
*/
|
||||
FileReadStream.prototype._open = function () {
|
||||
var flags = 'r';
|
||||
var self = this;
|
||||
fs.open(this._fileName, flags, function(error, fd) {
|
||||
if (error) {
|
||||
self.emit('error', error);
|
||||
} else {
|
||||
self._fd = fd;
|
||||
self.emit('open', fd);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
FileReadStream.prototype.on = function(event, listener) {
|
||||
if (event === 'data' && this._paused === undefined) {
|
||||
this._paused = false;
|
||||
this._emitData();
|
||||
}
|
||||
|
||||
return EventEmitter.prototype.on.call(this, event, listener);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set memory allocator
|
||||
*/
|
||||
FileReadStream.prototype.setMemoryAllocator = function(allocator) {
|
||||
this._allocator = allocator;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get buffer
|
||||
*/
|
||||
FileReadStream.prototype._getBuffer = function(size) {
|
||||
if(this._allocator && this._allocator.getBuffer) {
|
||||
return this._allocator.getBuffer(size);
|
||||
} else {
|
||||
var buffer = new Buffer(size);
|
||||
return buffer;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Release buffer
|
||||
*/
|
||||
FileReadStream.prototype._releaseBuffer = function(buffer) {
|
||||
if(this._allocator && this._allocator.releaseBuffer) {
|
||||
this._allocator.releaseBuffer(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit the data from file
|
||||
*/
|
||||
FileReadStream.prototype._emitData = function() {
|
||||
var self = this;
|
||||
if(!this._fd) {
|
||||
this.once('open', function() {
|
||||
self._emitData();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._paused || this._streamEnded) {
|
||||
return;
|
||||
}
|
||||
var buffer = this._getBuffer(this._highWaterMark);
|
||||
fs.read(this._fd, buffer, 0, this._highWaterMark, this._offset, function(error, bytesRead, readBuffer) {
|
||||
if (error) {
|
||||
self.emit('error', error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(bytesRead === 0) {
|
||||
if(!self._streamEnded) {
|
||||
self._streamEnded = true;
|
||||
self.emit('end');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var range = {
|
||||
start : self._offset,
|
||||
end : self._offset + bytesRead - 1,
|
||||
size : bytesRead
|
||||
};
|
||||
|
||||
var data;
|
||||
if(bytesRead == self._highWaterMark) {
|
||||
data = readBuffer;
|
||||
} else {
|
||||
data = readBuffer.slice(0, bytesRead);
|
||||
//Release the current buffer since we created a new one
|
||||
self._releaseBuffer(readBuffer);
|
||||
}
|
||||
|
||||
if(self._md5hash) {
|
||||
self._md5hash.update(data);
|
||||
}
|
||||
|
||||
self.emit('data', data, range);
|
||||
|
||||
// cleanup
|
||||
self._offset += bytesRead;
|
||||
buffer = readBuffer = data = null;
|
||||
self._emitData();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get file content md5 when read completely.
|
||||
*/
|
||||
FileReadStream.prototype.getContentMd5 = function(encoding) {
|
||||
if (!encoding) encoding = 'base64';
|
||||
if(!this._md5hash) {
|
||||
throw new Error('Can\'t get content md5, please set the calcContentMd5 option for FileReadStream.');
|
||||
} else {
|
||||
if (this._streamEnded) {
|
||||
if (!this._md5sum) {
|
||||
this._md5sum = this._md5hash.digest(encoding);
|
||||
}
|
||||
return this._md5sum;
|
||||
} else {
|
||||
throw new Error('FileReadStream has not ended.');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Pause chunk stream
|
||||
*/
|
||||
FileReadStream.prototype.pause = function() {
|
||||
this._paused = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resume read stream
|
||||
*/
|
||||
FileReadStream.prototype.resume = function() {
|
||||
var previousState = this._paused;
|
||||
if (this._paused) {
|
||||
this._paused = false;
|
||||
|
||||
if(previousState === true) {
|
||||
//Only start to emit data when it's in pause state
|
||||
this._emitData();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
FileReadStream.prototype.finish = function () {
|
||||
this.destroy();
|
||||
};
|
||||
|
||||
FileReadStream.prototype.destroy = function () {
|
||||
if (this._destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
this.readable = false;
|
||||
|
||||
function close(fd) {
|
||||
fs.close(fd || self._fd, function(err) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
}
|
||||
else {
|
||||
self.emit('close');
|
||||
}
|
||||
});
|
||||
self._fd = null;
|
||||
self._destroyed = true;
|
||||
}
|
||||
|
||||
// when the stream is closed immediately after creating it
|
||||
if (!validator.isInt(this._fd)) {
|
||||
this.once('open', close);
|
||||
return;
|
||||
}
|
||||
|
||||
close();
|
||||
};
|
||||
|
||||
FileReadStream.prototype.stop = function () {
|
||||
this.destroy();
|
||||
this._streamEnded = true;
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
module.exports = FileReadStream;
|
||||
+293
@@ -0,0 +1,293 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var azureCommon = require('./../common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = require('./../util/constants');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/**
|
||||
* Range stream
|
||||
*/
|
||||
function RangeStream(serviceClient, container, blob, options) {
|
||||
this.serviceClient = serviceClient;
|
||||
this._emitter = new EventEmitter();
|
||||
this._paused = false;
|
||||
this._emittedAll = false;
|
||||
this._emittedRangeIndex = 0;
|
||||
this._rangelist = [];
|
||||
this._resourcePath = [];
|
||||
this._isEmitting = false;
|
||||
this._rangeStreamEnded = false;
|
||||
this._lengthHeader = Constants.HeaderConstants.CONTENT_LENGTH;
|
||||
this._minRangeSize = Constants.BlobConstants.MIN_WRITE_PAGE_SIZE_IN_BYTES;
|
||||
this._maxRangeSize = Constants.BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES;
|
||||
if (options.rangeStart) {
|
||||
this._startOffset = options.rangeStart;
|
||||
} else {
|
||||
this._startOffset = 0;
|
||||
}
|
||||
this._dataOffset = this._startOffset;
|
||||
if (options.rangeEnd) {
|
||||
this._endOffset = options.rangeEnd;
|
||||
} else {
|
||||
this._endOffset = Number.MAX_VALUE;
|
||||
}
|
||||
if (container) {
|
||||
this._resourcePath.push(container);
|
||||
}
|
||||
if (blob) {
|
||||
this._resourcePath.push(blob);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get range list
|
||||
*/
|
||||
RangeStream.prototype.list = function (options, callback) {
|
||||
var start = this._startOffset;
|
||||
var end;
|
||||
var singleRangeSize = Constants.BlobConstants.MAX_SINGLE_GET_PAGE_RANGE_SIZE;
|
||||
|
||||
if (this._listFunc === undefined) {
|
||||
// the default function puts the whole blob into a single list item
|
||||
this._listFunc = this._defaultListFunc;
|
||||
end = this._endOffset;
|
||||
} else {
|
||||
end = Math.min(this._startOffset + singleRangeSize - 1, this._endOffset);
|
||||
}
|
||||
options.rangeStart = start;
|
||||
if (end != Number.MAX_VALUE) {
|
||||
options.rangeEnd = end;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
var onList = function (error, ranges, response) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
if (self._rangeStreamEnded) {
|
||||
return;
|
||||
}
|
||||
|
||||
var totalSize = parseInt(response.headers[self._lengthHeader], 10);
|
||||
var endOffset = Math.min(totalSize - 1, self._endOffset);
|
||||
var rangeEnd = Math.min(end, endOffset);
|
||||
|
||||
if (!ranges.length) {
|
||||
// convert single object to range
|
||||
// start >= end means there is no valid regions
|
||||
ranges.push({ start : start, end : rangeEnd, dataSize: 0 });
|
||||
} else if (ranges[ranges.length - 1].end !== rangeEnd) {
|
||||
// don't forget the zero chunk at the end of range
|
||||
ranges.push({ start : ranges[ranges.length - 1].end + 1, end : rangeEnd, dataSize: 0 });
|
||||
}
|
||||
|
||||
if (end >= endOffset) {
|
||||
self._rangeStreamEnded = true;
|
||||
}
|
||||
self.resizeAndSaveRanges(ranges);
|
||||
self._startOffset += singleRangeSize;
|
||||
self._emitRange();
|
||||
|
||||
// This is only valid when listing pages because when listing with the default function, the "endOffset" will always equal to or greater than the "end".
|
||||
if (end < endOffset && !self._rangeStreamEnded) {
|
||||
process.nextTick(function () {
|
||||
ranges = null;
|
||||
self.list(options, callback);
|
||||
self = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var callArguments = Array.prototype.slice.call(this._resourcePath);
|
||||
callArguments.push(options);
|
||||
callArguments.push(onList);
|
||||
this._listFunc.apply(this.serviceClient, callArguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Resize regions:
|
||||
* 1. Merge small pieces into a range no less than this._minRangeSize
|
||||
* 2. Split large pieces into ranges no more than this._maxRangeSize
|
||||
*/
|
||||
RangeStream.prototype.resizeAndSaveRanges = function (ranges) {
|
||||
var rangeList = this._rangelist;
|
||||
var holdingRange = { type : 'range', size : 0, dataSize : 0, start : this._startOffset, end : -1 };
|
||||
var readingRange = null;
|
||||
var rangeSize = 0;
|
||||
|
||||
for (var index = 0; index < ranges.length; index++) {
|
||||
readingRange = ranges[index];
|
||||
rangeSize = readingRange.end - holdingRange.start + 1;
|
||||
|
||||
if (rangeSize < this._minRangeSize) {
|
||||
// merge fragment ranges
|
||||
this.mergeRanges(holdingRange, readingRange);
|
||||
} else {
|
||||
if (holdingRange.end != -1) {
|
||||
// save the holding range list and hold the reading range
|
||||
this.splitAndSaveRanges(holdingRange, rangeList);
|
||||
holdingRange = readingRange;
|
||||
}
|
||||
|
||||
if (this._dataOffset != readingRange.start) {
|
||||
// padding zero for empty range and hold the reading range
|
||||
this.putZeroRange(this._dataOffset, readingRange.start - 1, rangeList);
|
||||
holdingRange = readingRange;
|
||||
} else if (holdingRange.end == -1) {
|
||||
// if holdingRange is never set, it means readingRange exceeds MIN_WRITE_FILE_SIZE_IN_BYTES
|
||||
this.splitAndSaveRanges(readingRange, rangeList);
|
||||
// reading range has been saved, offset the holding start position for calculating the range size in next loop
|
||||
holdingRange.start = readingRange.end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// If it is the last range, put the holding range into list anyway
|
||||
if (index == ranges.length - 1 && holdingRange.end > holdingRange.start) {
|
||||
this.splitAndSaveRanges(holdingRange, rangeList);
|
||||
}
|
||||
|
||||
this._dataOffset = readingRange.end + 1;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Put a zero range into range list
|
||||
*/
|
||||
RangeStream.prototype.putZeroRange = function (startOffset, endOffset, rangeList) {
|
||||
var zeroDataRange = { type : 'range', size : -1, dataSize : 0, start : startOffset, end : endOffset };
|
||||
this.splitAndSaveRanges(zeroDataRange, rangeList);
|
||||
};
|
||||
|
||||
/**
|
||||
* Merge small ranges
|
||||
*/
|
||||
RangeStream.prototype.mergeRanges = function (holdingRange, readingRange) {
|
||||
holdingRange.size = readingRange.end - holdingRange.start + 1;
|
||||
holdingRange.dataSize += readingRange.dataSize;
|
||||
holdingRange.end = readingRange.end;
|
||||
return holdingRange;
|
||||
};
|
||||
|
||||
/**
|
||||
* Split range into small pieces with maximum _maxRangeSize and minimum _minRangeSize size.
|
||||
* For example, [0, 10G - 1] => [0, 4MB - 1], [4MB, 8MB - 1] ... [10GB - 4MB, 10GB - 1]
|
||||
*/
|
||||
RangeStream.prototype.splitAndSaveRanges = function (range, rangeList) {
|
||||
var rangeSize = range.end - range.start + 1;
|
||||
var offset = range.start;
|
||||
var limitedSize = 0;
|
||||
|
||||
while (rangeSize > 0) {
|
||||
var newRange = { type : 'range', size : 0, dataSize : 0, start : -1, end : -1 };
|
||||
limitedSize = Math.min(rangeSize, this._maxRangeSize);
|
||||
newRange.start = offset;
|
||||
newRange.size = limitedSize;
|
||||
if (range.dataSize === 0) {
|
||||
newRange.dataSize = 0;
|
||||
} else {
|
||||
newRange.dataSize = limitedSize;
|
||||
}
|
||||
offset += limitedSize;
|
||||
newRange.end = offset - 1;
|
||||
rangeList.push(newRange);
|
||||
rangeSize -= limitedSize;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit a range
|
||||
*/
|
||||
RangeStream.prototype._emitRange = function () {
|
||||
if (this._paused || this._emittedAll || this._isEmitting) return;
|
||||
this._isEmitting = true;
|
||||
try {
|
||||
for (; this._emittedRangeIndex < this._rangelist.length; this._emittedRangeIndex++) {
|
||||
if (this._paused) {
|
||||
return;
|
||||
}
|
||||
var range = this._rangelist[this._emittedRangeIndex];
|
||||
this._emitter.emit('range', range);
|
||||
this._rangelist[this._emittedRangeIndex] = null;
|
||||
}
|
||||
|
||||
if (this._rangeStreamEnded) {
|
||||
this._rangelist = null;
|
||||
this._emittedAll = true;
|
||||
this._emitter.emit('end');
|
||||
}
|
||||
} finally {
|
||||
this._isEmitting = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The Default list function which puts the whole blob into one range.
|
||||
*/
|
||||
RangeStream.prototype._defaultListFunc = function (container, blob, optionsOrCallback, callback) {
|
||||
var options;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
|
||||
|
||||
this.getBlobProperties(container, blob, options, function (error, result, response) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
var range = [{}];
|
||||
range[0].start = options.rangeStart ? Math.max(options.rangeStart, 0) : 0;
|
||||
range[0].end = options.rangeEnd ? Math.min(options.rangeEnd, result.contentLength - 1) : result.contentLength - 1;
|
||||
range[0].size = range[0].end - range[0].start + 1;
|
||||
range[0].dataSize = range[0].size;
|
||||
callback(error, range, response);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
RangeStream.prototype.on = function (event, listener) {
|
||||
this._emitter.on(event, listener);
|
||||
};
|
||||
|
||||
/**
|
||||
* Pause the stream
|
||||
*/
|
||||
RangeStream.prototype.pause = function () {
|
||||
this._paused = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resume the stream
|
||||
*/
|
||||
RangeStream.prototype.resume = function () {
|
||||
this._paused = false;
|
||||
if (!this._isEmitting) {
|
||||
this._emitRange();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop the stream
|
||||
*/
|
||||
RangeStream.prototype.stop = function () {
|
||||
this.pause();
|
||||
this._emittedAll = true;
|
||||
this._emitter.emit('end');
|
||||
};
|
||||
|
||||
module.exports = RangeStream;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var rs = require('readable-stream').Readable;
|
||||
var fs = require('fs');
|
||||
|
||||
/*
|
||||
* As far as streams support goes, we can get the node 0.10 stream API in node 0.8.
|
||||
* Use the readable-stream module (https://www.npmjs.org/package/readable-stream) which is
|
||||
* essentially a copy of the stream modules from core node 0.10 and it just works on both 0.8 and 0.10.
|
||||
*/
|
||||
|
||||
exports.createReadStream = function(path, options) {
|
||||
var stream = fs.createReadStream(path, options);
|
||||
if (/^v0\.8\./.test(process.version)) {
|
||||
stream = rs().wrap(stream);
|
||||
}
|
||||
return stream;
|
||||
};
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var util = require('util');
|
||||
var azureutil = require('../util/util');
|
||||
|
||||
/**
|
||||
* Blob upload/download speed summary
|
||||
*/
|
||||
function SpeedSummary (name) {
|
||||
this.name = name;
|
||||
this._startTime = Date.now();
|
||||
this._timeWindowInSeconds = 10;
|
||||
this._timeWindow = this._timeWindowInSeconds * 1000;
|
||||
this._totalWindowSize = 0;
|
||||
this._speedTracks = new Array(this._timeWindowInSeconds);
|
||||
this._speedTrackPtr = 0;
|
||||
this.totalSize = undefined;
|
||||
this.completeSize = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the size to human readable size
|
||||
*/
|
||||
function toHumanReadableSize(size, len) {
|
||||
if(!size) return '0B';
|
||||
if (!len || len <= 0) {
|
||||
len = 2;
|
||||
}
|
||||
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
var i = Math.floor( Math.log(size) / Math.log(1024));
|
||||
return (size/Math.pow(1024, i)).toFixed(len) + units[i];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get running seconds
|
||||
*/
|
||||
SpeedSummary.prototype.getElapsedSeconds = function(humanReadable) {
|
||||
var now = Date.now();
|
||||
var seconds = parseInt((now - this._startTime) / 1000, 10);
|
||||
if (humanReadable !== false) {
|
||||
var s = parseInt(seconds % 60, 10);
|
||||
seconds /= 60;
|
||||
var m = Math.floor(seconds % 60);
|
||||
seconds /= 60;
|
||||
var h = Math.floor(seconds);
|
||||
seconds = util.format('%s:%s:%s', azureutil.zeroPaddingString(h, 2), azureutil.zeroPaddingString(m, 2), azureutil.zeroPaddingString(s, 2));
|
||||
}
|
||||
return seconds;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get complete percentage
|
||||
* @param {int} len The number of digits after the decimal point.
|
||||
*/
|
||||
SpeedSummary.prototype.getCompletePercent = function(len) {
|
||||
if (this.totalSize) {
|
||||
if(!len || len <= 0) {
|
||||
len = 1;
|
||||
}
|
||||
return (this.completeSize * 100 / this.totalSize).toFixed(len);
|
||||
} else {
|
||||
if(this.totalSize === 0) {
|
||||
return 100;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get average upload/download speed
|
||||
*/
|
||||
SpeedSummary.prototype.getAverageSpeed = function(humanReadable) {
|
||||
var elapsedTime = this.getElapsedSeconds(false);
|
||||
return this._getInternalSpeed(this.completeSize, elapsedTime, humanReadable);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get instant speed
|
||||
*/
|
||||
SpeedSummary.prototype.getSpeed = function(humanReadable) {
|
||||
this._refreshSpeedTracks();
|
||||
var elapsedTime = this.getElapsedSeconds(false);
|
||||
elapsedTime = Math.min(elapsedTime, this._timeWindowInSeconds);
|
||||
return this._getInternalSpeed(this._totalWindowSize, elapsedTime, humanReadable);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get internal speed
|
||||
*/
|
||||
SpeedSummary.prototype._getInternalSpeed = function(totalSize, elapsedTime, humanReadable) {
|
||||
if (elapsedTime <= 0) {
|
||||
elapsedTime = 1;
|
||||
}
|
||||
var speed = totalSize / elapsedTime;
|
||||
if(humanReadable !== false) {
|
||||
speed = toHumanReadableSize(speed) + '/s';
|
||||
}
|
||||
return speed;
|
||||
};
|
||||
|
||||
/**
|
||||
* Refresh speed tracks
|
||||
*/
|
||||
SpeedSummary.prototype._refreshSpeedTracks = function() {
|
||||
var now = Date.now();
|
||||
var totalSize = 0;
|
||||
for(var i = 0; i < this._speedTracks.length; i++) {
|
||||
if(!this._speedTracks[i]) continue;
|
||||
if(now - this._speedTracks[i].timeStamp <= this._timeWindow) {
|
||||
totalSize += this._speedTracks[i].size;
|
||||
} else {
|
||||
this._speedTracks[i] = null;
|
||||
}
|
||||
}
|
||||
this._totalWindowSize = totalSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* Increment the complete data size
|
||||
*/
|
||||
SpeedSummary.prototype.increment = function(len) {
|
||||
this.completeSize += len;
|
||||
this._recordSpeed(len);
|
||||
return this.completeSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* record complete size into speed tracks
|
||||
*/
|
||||
SpeedSummary.prototype._recordSpeed = function(completeSize) {
|
||||
var now = Date.now();
|
||||
var track = this._speedTracks[this._speedTrackPtr];
|
||||
if(track) {
|
||||
var timeDiff = now - track.timeStamp;
|
||||
if(timeDiff > this._timeWindow) {
|
||||
track.timeStamp = now;
|
||||
track.size = completeSize;
|
||||
} else if(timeDiff <= 1000) { //1 seconds
|
||||
track.size += completeSize;
|
||||
} else {
|
||||
this._speedTrackPtr = (this._speedTrackPtr + 1) % this._timeWindowInSeconds;
|
||||
this._recordSpeed(completeSize);
|
||||
}
|
||||
} else {
|
||||
track = {timeStamp : now, size: completeSize};
|
||||
this._speedTracks[this._speedTrackPtr] = track;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get auto increment function
|
||||
*/
|
||||
SpeedSummary.prototype.getAutoIncrementFunction = function(size) {
|
||||
var self = this;
|
||||
return function(error, retValue) {
|
||||
if(!error) {
|
||||
var doneSize = 0;
|
||||
if((!retValue && retValue !== 0) || isNaN(retValue)) {
|
||||
doneSize = size;
|
||||
} else {
|
||||
doneSize = retValue;
|
||||
}
|
||||
self.increment(doneSize);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Get total size
|
||||
*/
|
||||
SpeedSummary.prototype.getTotalSize = function(humanReadable) {
|
||||
if (humanReadable !== false) {
|
||||
return toHumanReadableSize(this.totalSize);
|
||||
} else {
|
||||
return this.totalSize;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get completed data size
|
||||
*/
|
||||
SpeedSummary.prototype.getCompleteSize = function(humanReadable) {
|
||||
if (humanReadable !== false) {
|
||||
return toHumanReadableSize(this.completeSize);
|
||||
} else {
|
||||
return this.completeSize;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = SpeedSummary;
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Expose 'AccessCondition'.
|
||||
|
||||
/**
|
||||
* Defines constants, enums, and utility functions for use with storage access condition.
|
||||
* @namespace
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* Constructs an empty access condition.
|
||||
*
|
||||
* @return {object} An empty AccessCondition object
|
||||
*/
|
||||
exports.generateEmptyCondition = function () {
|
||||
return {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource does not exist on the service
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-None-Match conditional header
|
||||
|
||||
* @return {AccessConditions} An AccessCondition object that represents a condition that checks for nonexistence
|
||||
*/
|
||||
exports.generateIfNotExistsCondition = function () {
|
||||
var accessCondition = {};
|
||||
accessCondition.EtagNonMatch = '*';
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource exists on the service
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-Match conditional header
|
||||
|
||||
* @return {AccessConditions} An AccessCondition object that represents a condition that checks for existence
|
||||
*/
|
||||
exports.generateIfExistsCondition = function () {
|
||||
var accessCondition = {};
|
||||
accessCondition.EtagMatch = '*';
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource's ETag value
|
||||
* does not match the specified ETag value
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-None-Match conditional header
|
||||
*
|
||||
* @param {string} etag The ETag value to check against the resource's ETag
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-None-Match condition
|
||||
*/
|
||||
exports.generateIfNoneMatchCondition = function (etag) {
|
||||
var accessCondition = {};
|
||||
accessCondition.EtagNonMatch = etag;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource's ETag value
|
||||
* matches the specified ETag value
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-Match conditional header
|
||||
*
|
||||
* @param {string} etag The ETag value to check against the resource's ETag
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Match condition
|
||||
*/
|
||||
exports.generateIfMatchCondition = function (etag) {
|
||||
var accessCondition = {};
|
||||
accessCondition.EtagMatch = etag;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource has been
|
||||
* modified since the specified time
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-Modified-Since conditional header
|
||||
*
|
||||
* @param {Date|string} time A date object specifying the time since which the resource must have been modified
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Modified-Since condition
|
||||
*/
|
||||
exports.generateIfModifiedSinceCondition = function (time) {
|
||||
var accessCondition = {};
|
||||
accessCondition.DateModifedSince = time;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource has not been
|
||||
* modified since the specified time
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP If-Unmodified-Since conditional header
|
||||
*
|
||||
* @param {Date|string} time A date object specifying the time since which the resource must have not been modified
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition
|
||||
*/
|
||||
exports.generateIfNotModifiedSinceCondition = function (time) {
|
||||
var accessCondition = {};
|
||||
accessCondition.DateUnModifiedSince = time;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource's sequence number
|
||||
* is equal to the specified value
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-eq conditional header
|
||||
*
|
||||
* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition
|
||||
*/
|
||||
exports.generateSequenceNumberEqualCondition = function (sequenceNumber) {
|
||||
var accessCondition = {};
|
||||
accessCondition.SequenceNumberEqual = sequenceNumber;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource's sequence number
|
||||
* is less than the specified value
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-lt conditional header
|
||||
*
|
||||
* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition
|
||||
*/
|
||||
exports.generateSequenceNumberLessThanCondition = function (sequenceNumber) {
|
||||
var accessCondition = {};
|
||||
accessCondition.SequenceNumberLessThan = sequenceNumber;
|
||||
return accessCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs an access condition such that an operation will be performed only if the resource's sequence number
|
||||
* is less than or equal to the specified value
|
||||
*
|
||||
* Setting this access condition modifies the request to include the HTTP x-ms-if-sequence-number-le conditional header
|
||||
*
|
||||
* @param {Number|string} sequenceNumber A date object specifying the time since which the resource must have not been modified
|
||||
* @return {AccessConditions} An AccessCondition object that represents the If-Unmodified-Since condition
|
||||
*/
|
||||
exports.generateSequenceNumberLessThanOrEqualCondition = function (sequenceNumber) {
|
||||
var accessCondition = {};
|
||||
accessCondition.SequenceNumberLessThanOrEqual = sequenceNumber;
|
||||
return accessCondition;
|
||||
};
|
||||
+2536
File diff suppressed because it is too large.
Load diff
+69
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
/**
|
||||
* Date/time related helper functions
|
||||
* @module date
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generates a Date object which is in the given days from now.
|
||||
*
|
||||
* @param {int} days The days timespan.
|
||||
* @return {Date}
|
||||
*/
|
||||
exports.daysFromNow = function (days) {
|
||||
var date = new Date();
|
||||
date.setDate(date.getDate() + days);
|
||||
return date;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a Date object which is in the given hours from now.
|
||||
*
|
||||
* @param {int} hours The hours timespan.
|
||||
* @return {Date}
|
||||
*/
|
||||
exports.hoursFromNow = function (hours) {
|
||||
var date = new Date();
|
||||
date.setHours(date.getHours() + hours);
|
||||
return date;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a Date object which is in the given minutes from now.
|
||||
*
|
||||
* @param {int} minutes The minutes timespan.
|
||||
* @return {Date}
|
||||
*/
|
||||
exports.minutesFromNow = function (minutes) {
|
||||
var date = new Date();
|
||||
date.setMinutes(date.getMinutes() + minutes);
|
||||
return date;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a Date object which is in the given seconds from now.
|
||||
*
|
||||
* @param {int} seconds The seconds timespan.
|
||||
* @return {Date}
|
||||
*/
|
||||
exports.secondsFromNow = function (seconds) {
|
||||
var date = new Date();
|
||||
date.setSeconds(date.getSeconds() + seconds);
|
||||
return date;
|
||||
};
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var rightPad = function (n, number) {
|
||||
var currentN = '' + n;
|
||||
while (currentN.length < number) {
|
||||
currentN = currentN + '0';
|
||||
}
|
||||
|
||||
return currentN;
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats a date into an iso 8061 string.
|
||||
*
|
||||
* @param {date} date The date to format.
|
||||
* @param {bool} skipMilliseconds Boolean value indicating if the miliseconds part of the date should not be included.
|
||||
* @param {integer} millisecondsPading Number of digits to left pad the miliseconds.
|
||||
* @return {string} The date formated in the ISO 8061 date format.
|
||||
*/
|
||||
exports.format = function (date) {
|
||||
var dateString = date.toISOString();
|
||||
return dateString.substring(0, dateString.length - 1) + '0000Z';
|
||||
};
|
||||
|
||||
/**
|
||||
* Parses an ISO 8061 date string into a date object.
|
||||
*
|
||||
* @param {string} stringDateTime The string with the date to parse in the ISO 8061 format.
|
||||
* @return {date} The parsed date.
|
||||
*/
|
||||
exports.parse = function (stringDateTime) {
|
||||
var parts = stringDateTime.split('T');
|
||||
var ymd = parts[0].split('-');
|
||||
var time = parts[1].split('.');
|
||||
var hms = time[0].split(':');
|
||||
var ms = 0;
|
||||
if (time[1]) {
|
||||
ms = time[1].split('Z');
|
||||
}
|
||||
|
||||
var date = new Date(Date.UTC(
|
||||
parseInt(ymd[0], 10),
|
||||
parseInt(ymd[1], 10) - 1,
|
||||
parseInt(ymd[2], 10),
|
||||
parseInt(hms[0], 10),
|
||||
parseInt(hms[1], 10),
|
||||
parseInt(hms[2], 10),
|
||||
Math.round(parseInt(rightPad(ms[0], 7), 10) / 10000)
|
||||
));
|
||||
|
||||
return date;
|
||||
};
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
'use strict';
|
||||
|
||||
var XMLBuilder = require('xmlbuilder/lib/XMLBuilder');
|
||||
|
||||
// Patch xmlbuilder to allow Unicode surrogate pair code
|
||||
// points in XML bodies
|
||||
|
||||
XMLBuilder.prototype.assertLegalChar = function(str) {
|
||||
var chars, chr;
|
||||
chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
|
||||
chr = str.match(chars);
|
||||
if (chr) {
|
||||
throw new Error('Invalid character (' + chr + ') in string: ' + str);
|
||||
}
|
||||
};
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
var SR = {
|
||||
ANONYMOUS_ACCESS_BLOBSERVICE_ONLY: 'Anonymous access is only valid for the BlobService.',
|
||||
ARGUMENT_NULL_OR_EMPTY: 'The argument must not be null or an empty string. Argument name: %s.',
|
||||
ARGUMENT_NULL_OR_UNDEFINED: 'The argument must not be null or undefined. Argument name: %s.',
|
||||
ARGUMENT_OUT_OF_RANGE_ERROR: 'The argument is out of range. Argument name: %s, Value passed: %s.',
|
||||
BATCH_ONE_PARTITION_KEY: 'All entities in the batch must have the same PartitionKey value.',
|
||||
BATCH_ONE_RETRIEVE: 'If a retrieve operation is part of a batch, it must be the only operation in the batch.',
|
||||
BATCH_TOO_LARGE: 'Batches must not contain more than 100 operations.',
|
||||
BLOB_INVALID_SEQUENCE_NUMBER: 'The sequence number may not be specified for an increment operation.',
|
||||
BLOB_TYPE_MISMATCH: 'Blob type of the blob reference doesn\'t match blob type of the blob.',
|
||||
CANNOT_CREATE_SAS_WITHOUT_ACCOUNT_KEY: 'Cannot create Shared Access Signature unless the Account Name and Key are used to create the ServiceClient.',
|
||||
CONTENT_LENGTH_MISMATCH: 'An incorrect number of bytes was read from the connection. The connection may have been closed.',
|
||||
CONTENT_TYPE_MISSING: 'Content-Type response header is missing or invalid.',
|
||||
EMPTY_BATCH: 'Batch must not be empty.',
|
||||
EXCEEDED_SIZE_LIMITATION: 'Upload exceeds the size limitation. Max size is %s but the current size is %s',
|
||||
HASH_MISMATCH: 'Hash mismatch (integrity check failed), Expected value is %s, retrieved %s.',
|
||||
INCORRECT_ENTITY_KEYS: 'PartitionKey and RowKey must be specified as strings in the entity object.',
|
||||
INVALID_BLOB_LENGTH: 'createBlockBlobFromText requires the size of text to be less than 64MB. Please use createBlockBlobFromLocalFile or createBlockBlobFromStream to upload large blobs.',
|
||||
INVALID_CONNECTION_STRING: 'Connection strings must be of the form "key1=value1;key2=value2".',
|
||||
INVALID_CONNECTION_STRING_BAD_KEY: 'Connection string contains unrecognized key: "%s"',
|
||||
INVALID_CONNECTION_STRING_DUPLICATE_KEY: 'Connection string contains duplicate key: "%s"',
|
||||
INVALID_CONNECTION_STRING_EMPTY_KEY: 'Connection strings must not contain empty keys.',
|
||||
INVALID_DELETE_SNAPSHOT_OPTION: 'The deleteSnapshots option cannot be included when deleting a specific snapshot using the snapshotId option.',
|
||||
INVALID_EDM_TYPE: 'The value \'%s\' does not match the type \'%s\'.',
|
||||
INVALID_FILE_LENGTH: 'createFileFromText requires the size of text to be less than 4MB. Please use createFileFromLocalFile or createFileFromStream to upload large files.',
|
||||
INVALID_FILE_RANGE_FOR_UPDATE: 'Range size should be less than 4MB for a file range update operation.',
|
||||
INVALID_HEADERS: 'Headers are not supported in the 2012-02-12 version.',
|
||||
INVALID_MESSAGE_ID: 'Message ID cannot be null or undefined for deleteMessage and updateMessage operations.',
|
||||
INVALID_PAGE_BLOB_LENGTH: 'Page blob length must be multiple of 512.',
|
||||
INVALID_PAGE_END_OFFSET: 'Page end offset must be multiple of 512.',
|
||||
INVALID_PAGE_RANGE_FOR_UPDATE: 'Page range size should be less than 4MB for a page update operation.',
|
||||
INVALID_PAGE_START_OFFSET: 'Page start offset must be multiple of 512.',
|
||||
INVALID_POP_RECEIPT: 'Pop Receipt cannot be null or undefined for deleteMessage and updateMessage operations.',
|
||||
INVALID_PROPERTY_RESOLVER: 'The specified property resolver returned an invalid type. %s:{_:%s,$:%s }',
|
||||
INVALID_RANGE_FOR_MD5: 'The requested range should be less than 4MB when contentMD5 is expected from the server',
|
||||
INVALID_SAS_VERSION: 'SAS Version ? is invalid. Valid versions include: ?.',
|
||||
INVALID_SAS_TOKEN: 'The SAS token should not contain api-version.',
|
||||
INVALID_SIGNED_IDENTIFIERS: 'Signed identifiers need to be a hash object with key as the id and the value as the access policy.',
|
||||
INVALID_STREAM_LENGTH: 'The length of the provided stream is invalid.',
|
||||
INVALID_STRING_ERROR: 'Invalid string error.',
|
||||
INVALID_TABLE_OPERATION: 'Operation not found: %s',
|
||||
INVALID_TEXT_LENGTH: 'The length of the provided text is invalid.',
|
||||
MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION: 'The client could not finish the operation within specified maximum execution timeout.',
|
||||
MD5_NOT_POSSIBLE: 'MD5 cannot be calculated for an existing blob because it would require reading the existing data. Please disable storeBlobContentMD5.',
|
||||
MD5_NOT_PRESENT_ERROR: 'MD5 does not exist. If you do not want to force validation, please disable useTransactionalMD5.',
|
||||
METADATA_KEY_INVALID: 'The key for one of the metadata key-value pairs is null, empty, or whitespace.',
|
||||
METADATA_VALUE_INVALID: 'The value for one of the metadata key-value pairs is null, empty, or whitespace.',
|
||||
NO_CREDENTIALS_PROVIDED: 'Credentials must be provided when creating a service client.',
|
||||
PRIMARY_ONLY_COMMAND: 'This operation can only be executed against the primary storage location.',
|
||||
QUERY_OPERATOR_REQUIRES_WHERE: '%s operator needs to be used after where.',
|
||||
SECONDARY_ONLY_COMMAND: 'This operation can only be executed against the secondary storage location.',
|
||||
STORAGE_HOST_LOCATION_REQUIRED: 'The host for the storage service must be specified.',
|
||||
STORAGE_HOST_MISSING_LOCATION: 'The host for the target storage location is not specified. Please consider changing the request\'s location mode.',
|
||||
TYPE_NOT_SUPPORTED: 'Type not supported when sending data to the service: ',
|
||||
MAX_BLOB_SIZE_CONDITION_NOT_MEET: 'Append block data should not exceed the maximum blob size condition value.',
|
||||
};
|
||||
|
||||
module.exports = SR;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Expose 'StorageUtilities'.
|
||||
|
||||
/**
|
||||
* Defines constants, enums, and utility functions for use with storage.
|
||||
* @namespace
|
||||
*/
|
||||
var StorageUtilities = {
|
||||
/**
|
||||
* Specifies the location mode used to decide which location the request should be sent to.
|
||||
*
|
||||
* @const
|
||||
* @enum {number}
|
||||
*/
|
||||
LocationMode: {
|
||||
/** The primary location only */
|
||||
PRIMARY_ONLY: 0,
|
||||
/** The primary location first, then the secondary */
|
||||
PRIMARY_THEN_SECONDARY: 1,
|
||||
/** The secondary location only */
|
||||
SECONDARY_ONLY: 2,
|
||||
/** The secondary location first, then the primary */
|
||||
SECONDARY_THEN_PRIMARY: 3
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = StorageUtilities;
|
||||
+618
@@ -0,0 +1,618 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var _ = require('underscore');
|
||||
var util = require('util');
|
||||
var url = require('url');
|
||||
var stream = require('stream');
|
||||
var Constants = require('./constants');
|
||||
var Md5Wrapper = require('../md5-wrapper');
|
||||
var StorageUtilities = require('./storageutilities');
|
||||
var SR = require('./sr');
|
||||
|
||||
/**
|
||||
* Trim the default port in the url.
|
||||
*
|
||||
* @param {string} uri The URI to be encoded.
|
||||
* @return {string} The URI without defualt port.
|
||||
*/
|
||||
exports.trimPortFromUri = function (uri) {
|
||||
var uri = url.parse(uri);
|
||||
if ((uri.protocol === Constants.HTTPS && uri.port == Constants.DEFAULT_HTTPS_PORT) || (uri.protocol === Constants.HTTP && uri.port == Constants.DEFAULT_HTTP_PORT)) {
|
||||
uri.host = uri.hostname;
|
||||
}
|
||||
return url.format(uri);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the number of keys (properties) in an object.
|
||||
*
|
||||
* @param {object} value The object which keys are to be counted.
|
||||
* @return {number} The number of keys in the object.
|
||||
*/
|
||||
exports.objectKeysLength = function (value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _.keys(value).length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if in a browser environment.
|
||||
*
|
||||
* @return {bool} True if in a browser environment, false otherwise.
|
||||
*/
|
||||
exports.isBrowser = function () {
|
||||
return typeof window !== 'undefined';
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if in IE.
|
||||
*
|
||||
* @return {bool} True if in IE, false otherwise.
|
||||
*/
|
||||
exports.isIE = function () {
|
||||
if (!exports.isBrowser()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var ua = window.navigator.userAgent;
|
||||
var msie = ua.indexOf('MSIE ');
|
||||
var trident = ua.indexOf('Trident/');
|
||||
return msie > 0 || trident > 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if in a 32bit Node.js environment.
|
||||
*
|
||||
* @return {bool} True if in a 32bit Node.js environment, false otherwise.
|
||||
*/
|
||||
exports.is32 = function () {
|
||||
return !exports.isBrowser() && process.arch === 'ia32';
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a value is null or undefined.
|
||||
*
|
||||
* @param {object} value The value to check for null or undefined.
|
||||
* @return {bool} True if the value is null or undefined, false otherwise.
|
||||
*/
|
||||
exports.objectIsNull = function (value) {
|
||||
return _.isNull(value) || _.isUndefined(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if an object is empty.
|
||||
*
|
||||
* @param {object} object The object to check if it is null.
|
||||
* @return {bool} True if the object is empty, false otherwise.
|
||||
*/
|
||||
exports.objectIsEmpty = function (object) {
|
||||
return _.isEmpty(object);
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if an object contains an integer number.
|
||||
*
|
||||
* @param {object} value The object to assert.
|
||||
* @return {bool} True if the object contains an integer number; false otherwise.
|
||||
*/
|
||||
exports.objectIsInt = function (value) {
|
||||
return typeof value === 'number' && parseFloat(value) == parseInt(value, 10) && !isNaN(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if an object is a NaN.
|
||||
*
|
||||
* @param {object} value The object to assert.
|
||||
* @return {bool} True if the object is a NaN; false otherwise.
|
||||
*/
|
||||
exports.objectIsNaN = function (value) {
|
||||
return typeof(value) === 'number' && isNaN(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if an object is a string.
|
||||
*
|
||||
* @param {object} object The object to check if it is a string.
|
||||
* @return {bool} True if the object is a string, false otherwise.
|
||||
*/
|
||||
exports.objectIsString = function (object) {
|
||||
return _.isString(object);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if an object is a function
|
||||
* @param {object} object The object to check whether it is function
|
||||
* @return {bool} True if the specified object is function, otherwise false
|
||||
*/
|
||||
exports.objectIsFunction = function (object) {
|
||||
return _.isFunction(object);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Front zero padding of string to sepcified length
|
||||
*/
|
||||
exports.zeroPaddingString = function(str, len) {
|
||||
var paddingStr = '0000000000' + str;
|
||||
if(paddingStr.length < len) {
|
||||
return exports.zeroPaddingString(paddingStr, len);
|
||||
} else {
|
||||
return paddingStr.substr(-1 * len);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a value is an empty string, null or undefined.
|
||||
*
|
||||
* @param {object} value The value to check for an empty string, null or undefined.
|
||||
* @return {bool} True if the value is an empty string, null or undefined, false otherwise.
|
||||
*/
|
||||
exports.stringIsEmpty = function (value) {
|
||||
return _.isNull(value) || _.isUndefined(value) || value === '';
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a value is null, empty, undefined or consists only of white-space characters.
|
||||
*
|
||||
* @param {object} value The value to check for null, empty, undefined and white-space only characters.
|
||||
* @return {bool} True if the value is an empty string, null, undefined, or consists only of white-space characters, false otherwise.
|
||||
*/
|
||||
exports.IsNullOrEmptyOrUndefinedOrWhiteSpace = function (value) {
|
||||
if(_.isNull(value) || _.isUndefined(value) || value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(_.isString(value) && value.trim().length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats a text replacing '?' by the arguments.
|
||||
*
|
||||
* @param {string} text The string where the ? should be replaced.
|
||||
* @param {array} arguments Value(s) to insert in question mark (?) parameters.
|
||||
* @return {string}
|
||||
*/
|
||||
exports.stringFormat = function (text) {
|
||||
if (arguments.length > 1) {
|
||||
for (var i = 1; text.indexOf('?') !== -1; i++) {
|
||||
text = text.replace('?', arguments[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if a string starts with another.
|
||||
*
|
||||
* @param {string} text The string to assert.
|
||||
* @param {string} prefix The string prefix.
|
||||
* @return {Bool} True if the string starts with the prefix; false otherwise.
|
||||
*/
|
||||
exports.stringStartsWith = function (text, prefix) {
|
||||
if (_.isNull(prefix)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return text.substr(0, prefix.length) === prefix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if a string ends with another.
|
||||
*
|
||||
* @param {string} text The string to assert.
|
||||
* @param {string} suffix The string suffix.
|
||||
* @return {Bool} True if the string ends with the suffix; false otherwise.
|
||||
*/
|
||||
exports.stringEndsWith = function (text, suffix) {
|
||||
if (_.isNull(suffix)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return text.substr(text.length - suffix.length) === suffix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the BOM from a string.
|
||||
*
|
||||
* @param {string} str The string from where the BOM is to be removed
|
||||
* @return {string} The string without the BOM.
|
||||
*/
|
||||
exports.removeBOM = function (str) {
|
||||
if (str.charCodeAt(0) === 0xfeff || str.charCodeAt(0) === 0xffef) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
/**
|
||||
* Merges multiple objects.
|
||||
*
|
||||
* @param {object} object The objects to be merged
|
||||
* @return {object} The merged object.
|
||||
*/
|
||||
exports.merge = function () {
|
||||
return _.extend.apply(this, arguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a value exists in an array. The comparison is done in a case
|
||||
* insensitive manner.
|
||||
*
|
||||
* @param {string} needle The searched value.
|
||||
* @param {array} haystack The array.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @return {boolean}
|
||||
*/
|
||||
exports.inArrayInsensitive = function (needle, haystack) {
|
||||
return _.contains(_.map(haystack, function (h) { return h.toLowerCase(); }), needle.toLowerCase());
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the specified value of the key passed from object and in case that
|
||||
* this key doesn't exist, the default value is returned. The key matching is
|
||||
* done in a case insensitive manner.
|
||||
*
|
||||
* @param {string} key The array key.
|
||||
* @param {object} haystack The object to be used.
|
||||
* @param {mix} default The value to return if $key is not found in $array.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @return mix
|
||||
*/
|
||||
exports.tryGetValueInsensitive = function (key, haystack, defaultValue) {
|
||||
if (haystack) {
|
||||
for (var i in haystack) {
|
||||
if (haystack.hasOwnProperty(i) && i.toString().toLowerCase() === key.toString().toLowerCase()) {
|
||||
return haystack[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the value in a chained object.
|
||||
*
|
||||
* @param {object} object The object with the values.
|
||||
* @param {array} keys The keys.
|
||||
* @param {mix} default The value to return if $key is not found in $array.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @return mix
|
||||
*/
|
||||
exports.tryGetValueChain = function (object, keys, defaultValue) {
|
||||
if (keys.length === 0) {
|
||||
return object;
|
||||
}
|
||||
|
||||
var currentKey = keys.shift();
|
||||
if (object && object[currentKey] !== undefined) {
|
||||
return exports.tryGetValueChain(object[currentKey], keys, defaultValue);
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the value of an inner property of an object.
|
||||
*
|
||||
* @param {object} object The target object.
|
||||
* @param {array} keys The property chain keys.
|
||||
* @param {mix} object The value to be set.
|
||||
*
|
||||
* @static
|
||||
|
||||
* @example
|
||||
* // Set targetObject.propA.propB to 'testValue'
|
||||
* var targetObject = {};
|
||||
* util.setObjectInnerPropertyValue(targetObject, ['propA', 'propB'], 'testValue');
|
||||
*/
|
||||
exports.setObjectInnerPropertyValue = function(object, propertyChainKeys, value){
|
||||
if(!object || propertyChainKeys.length < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var currentKey = propertyChainKeys.shift();
|
||||
if(propertyChainKeys.length === 0) {
|
||||
object[currentKey] = value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!object[currentKey]) {
|
||||
object[currentKey] = {};
|
||||
}
|
||||
|
||||
exports.setObjectInnerPropertyValue(object[currentKey], propertyChainKeys, value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rounds a date off to seconds.
|
||||
*
|
||||
* @param {Date} a date
|
||||
* @return {string} the date in ISO8061 format, with no milliseconds component
|
||||
*/
|
||||
exports.truncatedISO8061Date = function (date) {
|
||||
var dateString = date.toISOString();
|
||||
return dateString.substring(0, dateString.length - 5) + 'Z';
|
||||
};
|
||||
|
||||
exports.normalizeArgs = function (optionsOrCallback, callback, result) {
|
||||
var options = {};
|
||||
if(_.isFunction(optionsOrCallback) && !callback) {
|
||||
callback = optionsOrCallback;
|
||||
} else if (optionsOrCallback) {
|
||||
options = optionsOrCallback;
|
||||
}
|
||||
|
||||
result(options, callback);
|
||||
};
|
||||
|
||||
exports.getNodeVersion = function () {
|
||||
var parsedVersion = process.version.split('.');
|
||||
return {
|
||||
major: parseInt(parsedVersion[0].substr(1), 10),
|
||||
minor: parseInt(parsedVersion[1], 10),
|
||||
patch: parseInt(parsedVersion[2], 10)
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate md5sum for the stream
|
||||
* @ignore
|
||||
*/
|
||||
exports.calculateMD5 = function(readStream, bufferLength, options, callback) {
|
||||
var internalBuff = new Buffer(bufferLength);
|
||||
var index = 0;
|
||||
var internalHash = new Md5Wrapper().createMd5Hash();
|
||||
readStream.on('data', function(data) {
|
||||
if (index + data.length > bufferLength) {
|
||||
var copyLength = bufferLength - index;
|
||||
if (copyLength > 0) {
|
||||
data = data.slice(0, copyLength);
|
||||
data.copy(internalBuff, index);
|
||||
internalHash.update(data);
|
||||
index += copyLength;
|
||||
}
|
||||
readStream.emit('end');
|
||||
} else {
|
||||
data.copy(internalBuff, index);
|
||||
internalHash.update(data);
|
||||
index += data.length;
|
||||
}
|
||||
}).on('end', function() {
|
||||
if (!readStream.endEmitted) {
|
||||
internalBuff = internalBuff.slice(0, index);
|
||||
var contentMD5 = internalHash.digest('base64');
|
||||
// Set the flag to be compatible with Nodejs 0.10 which will keep emitting data from
|
||||
// the file stream when the read stream has emitted the end event from its listner.
|
||||
readStream.endEmitted = true;
|
||||
callback(internalBuff, contentMD5);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the content of buffer is all zero
|
||||
*/
|
||||
exports.isBufferAllZero = function (buffer) {
|
||||
for(var i = 0, len = buffer.length; i < len; i++) {
|
||||
if (buffer[i] !== 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Write zero to stream
|
||||
*/
|
||||
var zeroBuffer = null;
|
||||
exports.writeZerosToStream = function (stream, length, md5Hash, progressCallback, callback) {
|
||||
var defaultBufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
|
||||
var bufferSize = Math.min(defaultBufferSize, length);
|
||||
var remaining = length - bufferSize;
|
||||
var buffer = null;
|
||||
if (bufferSize == defaultBufferSize) {
|
||||
if (!zeroBuffer) {
|
||||
zeroBuffer = new Buffer(defaultBufferSize);
|
||||
zeroBuffer.fill(0);
|
||||
}
|
||||
buffer = zeroBuffer;
|
||||
} else {
|
||||
buffer = new Buffer(bufferSize);
|
||||
buffer.fill(0);
|
||||
}
|
||||
if (md5Hash) {
|
||||
md5Hash.update(buffer);
|
||||
}
|
||||
//We can only write the entire buffer to stream instead of part of buffer.
|
||||
return stream.write(buffer, function () {
|
||||
if (exports.objectIsFunction(progressCallback)) {
|
||||
progressCallback(null, buffer.length);
|
||||
}
|
||||
buffer = null;
|
||||
if (remaining > 0) {
|
||||
exports.writeZerosToStream(stream, remaining, md5Hash, progressCallback, callback);
|
||||
} else if (exports.objectIsFunction(callback)) {
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate md5sum for the content
|
||||
*/
|
||||
exports.getContentMd5 = function (content, encoding) {
|
||||
if (!encoding) encoding = 'base64';
|
||||
var internalHash = new Md5Wrapper().createMd5Hash();
|
||||
internalHash.update(content, 'utf8');
|
||||
return internalHash.digest(encoding);
|
||||
};
|
||||
|
||||
exports.getNextLocation = function(lastLocation, locationMode) {
|
||||
switch(locationMode) {
|
||||
case StorageUtilities.LocationMode.PRIMARY_ONLY:
|
||||
return Constants.StorageLocation.PRIMARY;
|
||||
case StorageUtilities.LocationMode.SECONDARY_ONLY:
|
||||
return Constants.StorageLocation.SECONDARY;
|
||||
case StorageUtilities.LocationMode.PRIMARY_THEN_SECONDARY:
|
||||
case StorageUtilities.LocationMode.SECONDARY_THEN_PRIMARY:
|
||||
return (lastLocation === Constants.StorageLocation.PRIMARY) ? Constants.StorageLocation.SECONDARY : Constants.StorageLocation.PRIMARY;
|
||||
default:
|
||||
throw new RangeError(util.format(SR.ARGUMENT_OUT_OF_RANGE_ERROR, 'locationMode', locationMode));
|
||||
}
|
||||
};
|
||||
|
||||
exports.getNextListingLocationMode = function (token) {
|
||||
if(_.isNull(token) || _.isUndefined(token)) {
|
||||
return Constants.RequestLocationMode.PRIMARY_OR_SECONDARY;
|
||||
}
|
||||
else {
|
||||
switch (token.targetLocation) {
|
||||
case Constants.StorageLocation.PRIMARY:
|
||||
return Constants.RequestLocationMode.PRIMARY_ONLY;
|
||||
case Constants.StorageLocation.SECONDARY:
|
||||
return Constants.RequestLocationMode.SECONDARY_ONLY;
|
||||
default:
|
||||
throw new RangeError(util.format(SR.ARGUMENT_OUT_OF_RANGE_ERROR, 'targetLocation', token.targetLocation));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.isStreamPaused = function (object) {
|
||||
if (object instanceof stream) {
|
||||
return object._paused === true || (object._readableState && object._readableState.flowing === false);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse copy progress string in the format of bytesCopied/totalBytes
|
||||
*/
|
||||
exports.parseCopyProgress = function (progress) {
|
||||
if (typeof progress != 'string' || progress.indexOf('/') === -1) {
|
||||
return {};
|
||||
}
|
||||
|
||||
var progressInfo = progress.split('/');
|
||||
return { bytesCopied: progressInfo[0], totalBytes: progressInfo[1] };
|
||||
};
|
||||
|
||||
/**
|
||||
* The list of the properties should be normalized with explicit mapping
|
||||
*/
|
||||
var normalizePropertyNameExceptionList = {
|
||||
'x-ms-blob-sequence-number': 'sequenceNumber',
|
||||
'content-Type': 'contentSettings.contentType',
|
||||
'content-Encoding': 'contentSettings.contentEncoding',
|
||||
'content-Language': 'contentSettings.contentLanguage',
|
||||
'cache-Control': 'contentSettings.cacheControl',
|
||||
'content-Disposition': 'contentSettings.contentDisposition',
|
||||
'content-MD5': 'contentSettings.contentMD5',
|
||||
'leaseId': 'lease.id',
|
||||
'leaseStatus': 'lease.status',
|
||||
'leaseDuration': 'lease.duration',
|
||||
'leaseState': 'lease.state',
|
||||
'copyId': 'copy.id',
|
||||
'copyStatus': 'copy.status',
|
||||
'copySource': 'copy.source',
|
||||
'copyProgress': 'copy.progress',
|
||||
'copyCompletionTime': 'copy.completionTime',
|
||||
'copyStatusDescription': 'copy.statusDescription',
|
||||
'copyDestinationSnapshot': 'copy.destinationSnapshot',
|
||||
'publicAccess': 'publicAccessLevel',
|
||||
'incrementalCopy': 'isIncrementalCopy'
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalize the property name from XML to keep consistent with
|
||||
* the name defined in the property headers
|
||||
*/
|
||||
exports.normalizePropertyNameFromXML = function (propertyName) {
|
||||
if (this.IsNullOrEmptyOrUndefinedOrWhiteSpace(propertyName)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
propertyName = propertyName.trim();
|
||||
propertyName = propertyName[0].toLowerCase() + propertyName.substring(1);
|
||||
// So far the cases are:
|
||||
// for the 'last-modified' property in listing resources
|
||||
// for the 'content-*' properties in listing resources
|
||||
// for the 'cache-control' property in listing blobs
|
||||
// for the 'x-ms-blob-sequence-number' in listing blobs
|
||||
if (propertyName in normalizePropertyNameExceptionList) {
|
||||
return normalizePropertyNameExceptionList[propertyName];
|
||||
} else if (propertyName.toLowerCase().indexOf('-') != -1) {
|
||||
return propertyName.replace('-', '');
|
||||
} else {
|
||||
return propertyName;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the property value from XML
|
||||
*/
|
||||
exports.setPropertyValueFromXML = function (result, xmlNode, toNormalize) {
|
||||
for (var subPropertyName in xmlNode) {
|
||||
if (xmlNode.hasOwnProperty(subPropertyName)) {
|
||||
if (toNormalize) {
|
||||
var propertyChain = this.normalizePropertyNameFromXML(subPropertyName).split('.');
|
||||
exports.setObjectInnerPropertyValue(result, propertyChain, xmlNode[subPropertyName]);
|
||||
} else {
|
||||
result[subPropertyName.toLowerCase()] = xmlNode[subPropertyName];
|
||||
}
|
||||
|
||||
if (subPropertyName.toLowerCase() === 'copyprogress') {
|
||||
var info = this.parseCopyProgress(xmlNode[subPropertyName]);
|
||||
exports.setObjectInnerPropertyValue(result, ['copy', 'bytesCopied'], parseInt(info.bytesCopied));
|
||||
exports.setObjectInnerPropertyValue(result, ['copy', 'totalBytes'], parseInt(info.totalBytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Filter out non-reserved properties from options
|
||||
*/
|
||||
exports.filterOutNonReservedProperties = function (reserved, options) {
|
||||
var nonReservedProperties = {};
|
||||
if (options) {
|
||||
for (var prop in options) {
|
||||
if (options.hasOwnProperty(prop)) {
|
||||
var isReserved = reserved.hasOwnProperty(prop);
|
||||
var isFunction = typeof options[prop] === 'function';
|
||||
if (!isReserved && !isFunction) {
|
||||
nonReservedProperties[prop] = options[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nonReservedProperties;
|
||||
};
|
||||
+560
@@ -0,0 +1,560 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var _ = require('underscore');
|
||||
var util = require('util');
|
||||
|
||||
var constants = require('./../util/constants');
|
||||
var blobConstants = constants.BlobConstants;
|
||||
var BlobUtilities = require('./../../services/blob/blobutilities');
|
||||
var FileUtilities = require('./../../services/file/fileutilities');
|
||||
var azureutil = require('./util');
|
||||
var SR = require('./sr');
|
||||
var check = require('validator');
|
||||
var errors = require('../errors/errors');
|
||||
var ArgumentError = errors.ArgumentError;
|
||||
var ArgumentNullError = errors.ArgumentNullError;
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
function initCallback(callbackParam, resultsCb) {
|
||||
var fail;
|
||||
if (callbackParam) {
|
||||
fail = function (err) {
|
||||
callbackParam(err);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
fail = function (err) {
|
||||
throw err;
|
||||
};
|
||||
callbackParam = function () {};
|
||||
}
|
||||
|
||||
resultsCb(fail, callbackParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value is a valid enumeration or not.
|
||||
*
|
||||
* @param {object} value The value to validate.
|
||||
* @param {object} list The enumeration values.
|
||||
* @return {boolean}
|
||||
*/
|
||||
exports.isValidEnumValue = function (value, list, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (!list.some(function (current) {
|
||||
return current.toLowerCase() === value.toLowerCase();
|
||||
})) {
|
||||
return fail(new RangeError(util.format('Invalid value: %s. Options are: %s.', value, list)));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a anonymous function that check if the given uri is valid or not.
|
||||
*
|
||||
* @param {string} uri The uri to validate.
|
||||
* @return {boolean}
|
||||
*/
|
||||
exports.isValidUri = function (uri) {
|
||||
if (!check.isURL(uri)){
|
||||
throw new URIError('The provided URI "' + uri + '" is invalid.');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given host is valid or not.
|
||||
*
|
||||
* @param {string|object} host The host to validate.
|
||||
* @return {boolean}
|
||||
*/
|
||||
exports.isValidHost= function (host) {
|
||||
if (azureutil.objectIsNull(host)) {
|
||||
throw new ArgumentNullError('host', SR.STORAGE_HOST_LOCATION_REQUIRED);
|
||||
} else {
|
||||
var storageHost = {};
|
||||
storageHost.primaryHost = _.isString(host) ? host : host.primaryHost;
|
||||
if (storageHost.primaryHost && !check.isURL(storageHost.primaryHost)){
|
||||
throw new URIError('The provided URI "' + storageHost.primaryHost + '" is invalid.');
|
||||
}
|
||||
|
||||
storageHost.secondaryHost = _.isString(host) ? undefined : host.secondaryHost;
|
||||
if (storageHost.secondaryHost && !check.isURL(storageHost.secondaryHost)){
|
||||
throw new URIError('The provided URI "' + storageHost.secondaryHost + '" is invalid.');
|
||||
}
|
||||
|
||||
if (!storageHost.primaryHost && !storageHost.secondaryHost) {
|
||||
throw new ArgumentNullError('host', SR.STORAGE_HOST_LOCATION_REQUIRED);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given value is a valid UUID or not.
|
||||
*
|
||||
* @param {string|object} uuid The uuid to validate.
|
||||
* @return {boolean}
|
||||
*/
|
||||
exports.isValidUuid = function(uuid, callback) {
|
||||
var validUuidRegex = /^[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}$/;
|
||||
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (!validUuidRegex.test(uuid)) {
|
||||
return fail(new SyntaxError('The value is not a valid UUID format.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a anonymous function that check if a given key is base 64 encoded.
|
||||
*
|
||||
* @param {string} key The key to validate.
|
||||
* @return {function}
|
||||
*/
|
||||
exports.isBase64Encoded = function (key) {
|
||||
var isValidBase64String = key.match(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/);
|
||||
|
||||
if (isValidBase64String) {
|
||||
return true;
|
||||
} else {
|
||||
throw new SyntaxError('The provided account key ' + key + ' is not a valid base64 string.');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a function.
|
||||
*
|
||||
* @param {object} function The function to validate.
|
||||
* @return {function}
|
||||
*/
|
||||
exports.isValidFunction = function (functionObject, functionName) {
|
||||
if (!functionObject) {
|
||||
throw new ArgumentNullError('functionObject', functionName + ' must be specified.');
|
||||
}
|
||||
if(!_.isFunction(functionObject)){
|
||||
throw new TypeError(functionName + ' specified should be a function.');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
var getNameError = function(name, typeName) {
|
||||
// checks if name is null, undefined or empty
|
||||
if (azureutil.stringIsEmpty(name)) {
|
||||
return new ArgumentNullError('name', util.format('%s name must be a non empty string.', typeName));
|
||||
}
|
||||
|
||||
// check if name is between 3 and 63 characters
|
||||
if (name.length < 3 || name.length > 63) {
|
||||
return new ArgumentError('name', util.format('%s name must be between 3 and 63 characters long.', typeName));
|
||||
}
|
||||
|
||||
// check if name follows naming rules
|
||||
if (name.match(/^([a-z0-9]+(-[a-z0-9]+)*)$/) === null) {
|
||||
return new SyntaxError(util.format('%s name format is incorrect.', typeName));
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a container name.
|
||||
*
|
||||
* @param {string} containerName The container name.
|
||||
*/
|
||||
exports.containerNameIsValid = function (containerName, callback) {
|
||||
var fail;
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
var nameError = getNameError(containerName, 'Container');
|
||||
|
||||
if (!nameError || containerName.match(/^(\$root|\$logs)$/)) {
|
||||
callback();
|
||||
return true;
|
||||
} else {
|
||||
return fail(nameError);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a blob name.
|
||||
*
|
||||
* @param {string} containerName The container name.
|
||||
* @param {string} blobname The blob name.
|
||||
*/
|
||||
exports.blobNameIsValid = function (containerName, blobName, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (!blobName) {
|
||||
return fail(new ArgumentNullError('blobName', 'Blob name is not specified.'));
|
||||
}
|
||||
|
||||
if (containerName === '$root' && blobName.indexOf('/') !== -1) {
|
||||
return fail(new SyntaxError('Blob name format is incorrect.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a blob tier name.
|
||||
*
|
||||
* @param {string} blobTier The blob tier name.
|
||||
*/
|
||||
exports.blobTierNameIsValid = function (blobTier, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (!blobTier) {
|
||||
return fail(new ArgumentNullError('blobTier', 'Blob tier is not specified.'));
|
||||
}
|
||||
|
||||
if (!_.chain(_.union(
|
||||
_.values(BlobUtilities.BlobTier.PremiumPageBlobTier),
|
||||
_.values(BlobUtilities.BlobTier.StandardBlobTier)
|
||||
))
|
||||
.map(function (val) { return val.toString().toUpperCase(); })
|
||||
.contains(blobTier.toString().toUpperCase())
|
||||
.value()) {
|
||||
return fail(new SyntaxError('Blob tier is incorrect. Refer to BlobUtilities.BlobTier for possible values.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a share name.
|
||||
*
|
||||
* @param {string} shareName The share name.
|
||||
*/
|
||||
exports.shareNameIsValid = function (shareName, callback) {
|
||||
var fail;
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
var nameError = getNameError(shareName, 'Share');
|
||||
|
||||
if (!nameError) {
|
||||
callback();
|
||||
return true;
|
||||
} else {
|
||||
return fail(nameError);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a queue name.
|
||||
*
|
||||
* @param {string} queueName The queue name.
|
||||
*/
|
||||
exports.queueNameIsValid = function (queueName, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
var nameError = getNameError(queueName, 'Queue');
|
||||
|
||||
if (!nameError) {
|
||||
callback();
|
||||
return true;
|
||||
} else {
|
||||
return fail(nameError);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a table name.
|
||||
*
|
||||
* @param {string} table The table name.
|
||||
*/
|
||||
exports.tableNameIsValid = function (table, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (azureutil.stringIsEmpty(table)) {
|
||||
return fail(new ArgumentNullError('table', 'Table name must be a non empty string.'));
|
||||
}
|
||||
|
||||
if (table.length < 3 || table.length > 63) {
|
||||
return fail(new ArgumentError('table', 'Table name must be between 3 and 63 characters long.'));
|
||||
}
|
||||
|
||||
if(table.toLowerCase() === 'tables') {
|
||||
return fail(new RangeError('Table name cannot be \'Tables\'.'));
|
||||
}
|
||||
|
||||
if (table.match(/^([A-Za-z][A-Za-z0-9]{2,62})$/) !== null || table === '$MetricsCapacityBlob' || table.match(/^(\$Metrics(HourPrimary|MinutePrimary|HourSecondary|MinuteSecondary)?(Transactions)(Blob|Queue|Table|File))$/) !== null)
|
||||
{
|
||||
callback();
|
||||
return true;
|
||||
} else {
|
||||
return fail(new SyntaxError('Table name format is incorrect.'));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates an HTML File object.
|
||||
*
|
||||
* @param {File} browserFile The HTML File object.
|
||||
*/
|
||||
exports.browserFileIsValid = function (browserFile, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
// IE doesn't support File.constructor.name
|
||||
if (!azureutil.isBrowser() ||
|
||||
!browserFile ||
|
||||
!browserFile.constructor ||
|
||||
(!azureutil.isIE() && !browserFile.constructor.name) ||
|
||||
(!azureutil.isIE() && browserFile.constructor.name !== 'File' && browserFile.constructor.name !== 'Blob') ||
|
||||
!azureutil.objectIsInt(browserFile.size)) {
|
||||
return fail(new ArgumentError('type', 'Invalid HTML File object.'));
|
||||
} else {
|
||||
callback();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates page ranges.
|
||||
*
|
||||
* @param {int} rangeStart The range starting position.
|
||||
* @param {int} rangeEnd The range ending position.
|
||||
* @param {int} writeBlockSizeInBytes The block size.
|
||||
*/
|
||||
exports.pageRangesAreValid = function (rangeStart, rangeEnd, writeBlockSizeInBytes, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (rangeStart % 512 !== 0) {
|
||||
return fail(new RangeError('Start byte offset must be a multiple of 512.'));
|
||||
}
|
||||
|
||||
var size = null;
|
||||
if (!azureutil.objectIsNull(rangeEnd)) {
|
||||
if ((rangeEnd + 1) % 512 !== 0) {
|
||||
return fail(new RangeError('End byte offset must be a multiple of 512 minus 1.'));
|
||||
}
|
||||
|
||||
size = (rangeEnd - rangeStart) + 1;
|
||||
if (size > writeBlockSizeInBytes) {
|
||||
return fail(new RangeError('Page blob size cannot be larger than ' + writeBlockSizeInBytes + ' bytes.'));
|
||||
}
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a blob type.
|
||||
*
|
||||
* @param {string} type The type name.
|
||||
*/
|
||||
exports.blobTypeIsValid = function (type, callback) {
|
||||
var getEnumValues = function (obj) {
|
||||
var values = [];
|
||||
for (var prop in obj) {
|
||||
if (obj.hasOwnProperty(prop)) {
|
||||
values.push(obj[prop]);
|
||||
}
|
||||
}
|
||||
return values;
|
||||
};
|
||||
|
||||
return this.isValidEnumValue(type, getEnumValues(blobConstants.BlobTypes), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates share ACL type.
|
||||
*
|
||||
* @param {string} type The type name.
|
||||
*/
|
||||
exports.shareACLIsValid = function (type, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (type != FileUtilities.SharePublicAccessType.OFF) {
|
||||
fail(new ArgumentError('type', 'The access type is not supported.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates share quota value.
|
||||
*
|
||||
* @param {int} type The quota value.
|
||||
*/
|
||||
exports.shareQuotaIsValid = function (quota, callback) {
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (quota && quota <= 0) {
|
||||
fail(new RangeError('The share quota value, in GB, must be greater than 0.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
return true;
|
||||
};
|
||||
|
||||
// common functions for validating arguments
|
||||
|
||||
function throwMissingArgument(name, func) {
|
||||
throw new ArgumentNullError(name, 'Required argument ' + name + ' for function ' + func + ' is not defined');
|
||||
}
|
||||
|
||||
function ArgumentValidator(functionName) {
|
||||
this.func = functionName;
|
||||
}
|
||||
|
||||
_.extend(ArgumentValidator.prototype, {
|
||||
string: function (val, name) {
|
||||
this.exists(val, name);
|
||||
if (typeof val !== 'string') {
|
||||
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a non-empty string');
|
||||
}
|
||||
},
|
||||
|
||||
stringAllowEmpty: function (val, name) {
|
||||
if (typeof val !== 'string') {
|
||||
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a string');
|
||||
}
|
||||
},
|
||||
|
||||
object: function (val, name) {
|
||||
this.exists(val, name);
|
||||
if (typeof val !== 'object') {
|
||||
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be an object');
|
||||
}
|
||||
},
|
||||
|
||||
exists: function (val, name) {
|
||||
if (!val) {
|
||||
throwMissingArgument(name, this.func);
|
||||
}
|
||||
},
|
||||
|
||||
function: function (val, name) {
|
||||
this.exists(val, name);
|
||||
if (typeof val !== 'function') {
|
||||
throw new TypeError('Parameter ' + name + ' for function ' + this.func + ' should be a function');
|
||||
}
|
||||
},
|
||||
|
||||
value: function (val, name) {
|
||||
if (!val && val !== 0) {
|
||||
throwMissingArgument(name, this.func);
|
||||
}
|
||||
},
|
||||
|
||||
nonEmptyArray: function (val, name) {
|
||||
if (!val || val.length === 0) {
|
||||
throw new TypeError('Required array argument ' + name + ' for function ' + this.func + ' is either not defined or empty');
|
||||
}
|
||||
},
|
||||
|
||||
callback: function (val) {
|
||||
this.exists(val, 'callback');
|
||||
this.function(val, 'callback');
|
||||
},
|
||||
|
||||
test: function (predicate, message) {
|
||||
if (!predicate()) {
|
||||
throw new Error(message + ' in function ' + this.func);
|
||||
}
|
||||
},
|
||||
|
||||
tableNameIsValid: exports.tableNameIsValid,
|
||||
browserFileIsValid: exports.browserFileIsValid,
|
||||
containerNameIsValid: exports.containerNameIsValid,
|
||||
shareNameIsValid: exports.shareNameIsValid,
|
||||
blobNameIsValid: exports.blobNameIsValid,
|
||||
blobTierNameIsValid: exports.blobTierNameIsValid,
|
||||
pageRangesAreValid: exports.pageRangesAreValid,
|
||||
queueNameIsValid: exports.queueNameIsValid,
|
||||
blobTypeIsValid: exports.blobTypeIsValid,
|
||||
shareACLIsValid: exports.shareACLIsValid,
|
||||
shareQuotaIsValid: exports.shareQuotaIsValid,
|
||||
isValidEnumValue: exports.isValidEnumValue
|
||||
});
|
||||
|
||||
function validateArgs(functionName, validationRules) {
|
||||
var validator = new ArgumentValidator(functionName);
|
||||
validationRules(validator);
|
||||
}
|
||||
|
||||
exports.ArgumentValidator = ArgumentValidator;
|
||||
exports.validateArgs = validateArgs;
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var BlobService = require('./blobservice.core');
|
||||
var azureCommon = require('./../../common/common.browser');
|
||||
var extend = require('extend');
|
||||
var mime = require('browserify-mime');
|
||||
|
||||
var Constants = azureCommon.Constants;
|
||||
var azureutil = azureCommon.util;
|
||||
var BlobConstants = Constants.BlobConstants;
|
||||
var BrowserFileReadStream = azureCommon.BrowserFileReadStream;
|
||||
var SpeedSummary = azureCommon.SpeedSummary;
|
||||
var validate = azureCommon.validate;
|
||||
|
||||
/**
|
||||
* Creates a new block blob. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB.
|
||||
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createBlockBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) {
|
||||
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.BLOCK, browserFile, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Uploads a page blob from an HTML file. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads.
|
||||
* The default value is false for page blobs.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createPageBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) {
|
||||
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.PAGE, browserFile, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new append blob from an HTML File object. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks.
|
||||
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you.
|
||||
* If you want to append data to an already existing blob, please look at appendFromBrowserFile.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 ahash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createAppendBlobFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) {
|
||||
return this._createBlobFromBrowserFile(container, blob, BlobConstants.BlobTypes.APPEND, browserFile, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends to an append blob from an HTML File object. Assumes the blob already exists on the service.
|
||||
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks.
|
||||
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.appendFromBrowserFile = function (container, blob, browserFile, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('appendFromBrowserFile', function (v) {
|
||||
v.string(container, 'container');
|
||||
v.string(blob, 'blob');
|
||||
v.containerNameIsValid(container);
|
||||
v.browserFileIsValid(browserFile);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(blob);
|
||||
|
||||
var stream = new BrowserFileReadStream(browserFile);
|
||||
var streamCallback = function (appendError, blob, response) {
|
||||
if (azureutil.objectIsFunction(stream.destroy)) {
|
||||
stream.destroy();
|
||||
}
|
||||
callback(appendError, blob, response);
|
||||
};
|
||||
this._uploadBlobFromStream(false, container, blob, BlobConstants.BlobTypes.APPEND, stream, browserFile.size, options, streamCallback);
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* Creates a new blob (Block/Page/Append). If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @ignore
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {BlobType} blobType The blob type.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only)
|
||||
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB.
|
||||
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only)
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The MD5 hash of the blob content.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback The callback function.
|
||||
*
|
||||
* @return {SpeedSummary}
|
||||
*
|
||||
*/
|
||||
BlobService.prototype._createBlobFromBrowserFile = function (container, blob, blobType, browserFile, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('_createBlobFromBrowserFile', function (v) {
|
||||
v.string(container, 'container');
|
||||
v.string(blob, 'blob');
|
||||
v.containerNameIsValid(container);
|
||||
v.blobTypeIsValid(blobType);
|
||||
v.browserFileIsValid(browserFile);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(blob);
|
||||
|
||||
var self = this;
|
||||
var creationCallback = function (createError, createBlob, createResponse) {
|
||||
if (createError) {
|
||||
callback(createError, createBlob, createResponse);
|
||||
} else {
|
||||
// Automatically detect the mime type
|
||||
if(azureutil.tryGetValueChain(options, ['contentSettings','contentType'], undefined) === undefined) {
|
||||
azureutil.setObjectInnerPropertyValue(options, ['contentSettings','contentType'], mime.lookup(browserFile.name));
|
||||
}
|
||||
|
||||
var stream = new BrowserFileReadStream(browserFile);
|
||||
var streamCallback = function (createError, createBlob, createResponse) {
|
||||
if (azureutil.objectIsFunction(stream.destroy)) {
|
||||
stream.destroy();
|
||||
}
|
||||
callback(createError, createBlob, createResponse);
|
||||
};
|
||||
self._uploadBlobFromStream(true, container, blob, blobType, stream, browserFile.size, options, streamCallback);
|
||||
}
|
||||
};
|
||||
|
||||
this._createBlob(container, blob, blobType, browserFile.size, options, creationCallback);
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
module.exports = BlobService;
|
||||
+5733
File diff suppressed because it is too large.
Load diff
+461
@@ -0,0 +1,461 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureCommon = require('./../../common/common.node');
|
||||
var BlobService = require('./blobservice.core');
|
||||
var extend = require('extend');
|
||||
var fs = require('fs');
|
||||
var mime = require('browserify-mime');
|
||||
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = azureCommon.Constants;
|
||||
var FileReadStream = azureCommon.FileReadStream;
|
||||
var SpeedSummary = azureCommon.SpeedSummary;
|
||||
var validate = azureCommon.validate;
|
||||
var BlobConstants = Constants.BlobConstants;
|
||||
|
||||
/**
|
||||
* Downloads a blob into a file.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be downloaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {boolean} [options.skipSizeCheck] Skip the size check to perform direct download.
|
||||
* Set the option to true for small blobs.
|
||||
* Parallel download and speed summary won't work with this option on.
|
||||
* @param {SpeedSummary} [options.speedSummary] The download tracker objects.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {string} [options.snapshotId] The snapshot identifier.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range.
|
||||
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions.
|
||||
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information if an error occurs;
|
||||
* otherwise `[result]{@link BlobResult}` will contain the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var blobService = azure.createBlobService();
|
||||
* blobService.getBlobToLocalFile('taskcontainer', 'task1', 'task1-download.txt', function(error, serverBlob) {
|
||||
* if(!error) {
|
||||
* // Blob available in serverBlob.blob variable
|
||||
* }
|
||||
*/
|
||||
BlobService.prototype.getBlobToLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
var options;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
|
||||
|
||||
validate.validateArgs('getBlobToLocalFile', function (v) {
|
||||
v.string(container, 'container');
|
||||
v.string(blob, 'blob');
|
||||
v.string(localFileName, 'localFileName');
|
||||
v.containerNameIsValid(container);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
return this._getBlobToLocalFile(container, blob, localFileName, options, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Uploads a page blob from file. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {SpeedSummary} [options.speedSummary] The upload tracker objects.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads.
|
||||
* The default value is false for page blobs.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createPageBlobFromLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
return this._createBlobFromLocalFile(container, blob, BlobConstants.BlobTypes.PAGE, localFileName, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new block blob. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB.
|
||||
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] The MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createBlockBlobFromLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
return this._createBlobFromLocalFile(container, blob, BlobConstants.BlobTypes.BLOCK, localFileName, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new append blob from a local file. If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks.
|
||||
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you.
|
||||
* If you want to append data to an already existing blob, please look at appendFromLocalFile.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 ahash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.createAppendBlobFromLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
return this._createBlobFromLocalFile(container, blob, BlobConstants.BlobTypes.APPEND, localFileName, optionsOrCallback, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends to an append blob from a local file. Assumes the blob already exists on the service.
|
||||
* This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks.
|
||||
* If you are guaranteed to have a single writer scenario, please look at options.absorbConditionalErrorsOnRetry and see if setting this flag to true is acceptable for you.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The blob's MD5 hash.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information
|
||||
* if an error occurs; otherwise `[result]{@link BlobResult}` will contain
|
||||
* the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
BlobService.prototype.appendFromLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('appendFromLocalFile', function (v) {
|
||||
v.string(container, 'container');
|
||||
v.string(blob, 'blob');
|
||||
v.containerNameIsValid(container);
|
||||
v.string(localFileName, 'localFileName');
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(blob);
|
||||
|
||||
var self = this;
|
||||
fs.stat(localFileName, function (error, stat) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
var stream = new FileReadStream(localFileName, { calcContentMd5: options.storeBlobContentMD5 });
|
||||
var streamCallback = function (appendError, blob, response) {
|
||||
if (azureutil.objectIsFunction(stream.destroy)) {
|
||||
stream.destroy();
|
||||
}
|
||||
callback(appendError, blob, response);
|
||||
};
|
||||
self._uploadBlobFromStream(false, container, blob, BlobConstants.BlobTypes.APPEND, stream, stat.size, options, streamCallback);
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* Creates a new blob (Block/Page/Append). If the blob already exists on the service, it will be overwritten.
|
||||
* To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
|
||||
*
|
||||
* @ignore
|
||||
*
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {BlobType} blobType The blob type.
|
||||
* @param {string} localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {bool} [options.absorbConditionalErrorsOnRetry] Specifies whether to absorb the conditional error on retry. (For append blob only)
|
||||
* @param {int} [options.blockSize] The size of each block. Maximum is 100MB.
|
||||
* @param {string} [options.blockIdPrefix] The prefix to be used to generate the block id. (For block blob only)
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.transactionalContentMD5] An MD5 hash of the blob content. This hash is used to verify the integrity of the blob during transport.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {bool} [options.storeBlobContentMD5] Specifies whether the blob's ContentMD5 header should be set on uploads. The default value is true for block blobs.
|
||||
* @param {string} [options.blobTier] For page blobs on premium accounts only. Set the tier of the target blob. Refer to BlobUtilities.BlobTier.PremiumPageBlobTier.
|
||||
* @param {object} [options.contentSettings] The content settings of the blob.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the blob. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the blob.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The Blob service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The blob's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The MD5 hash of the blob content.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback The callback function.
|
||||
*
|
||||
* @return {SpeedSummary}
|
||||
*
|
||||
*/
|
||||
BlobService.prototype._createBlobFromLocalFile = function (container, blob, blobType, localFileName, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('_createBlobFromLocalFile', function (v) {
|
||||
v.string(container, 'container');
|
||||
v.string(blob, 'blob');
|
||||
v.containerNameIsValid(container);
|
||||
v.blobTypeIsValid(blobType);
|
||||
v.string(localFileName, 'localFileName');
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(blob);
|
||||
|
||||
var self = this;
|
||||
var size = 0;
|
||||
|
||||
var creationCallback = function (createError, createBlob, createResponse) {
|
||||
if (createError) {
|
||||
callback(createError, createBlob, createResponse);
|
||||
} else {
|
||||
// Automatically detect the mime type
|
||||
if(azureutil.tryGetValueChain(options, ['contentSettings','contentType'], undefined) === undefined) {
|
||||
azureutil.setObjectInnerPropertyValue(options, ['contentSettings','contentType'], mime.lookup(localFileName));
|
||||
}
|
||||
|
||||
var stream = new FileReadStream(localFileName, { calcContentMd5: options.storeBlobContentMD5 });
|
||||
var streamCallback = function (createError, createBlob, createResponse) {
|
||||
if (azureutil.objectIsFunction(stream.destroy)) {
|
||||
stream.destroy();
|
||||
}
|
||||
callback(createError, createBlob, createResponse);
|
||||
};
|
||||
self._uploadBlobFromStream(true, container, blob, blobType, stream, size, options, streamCallback);
|
||||
}
|
||||
};
|
||||
|
||||
// Check the file size to determine the upload method: single request or chunks
|
||||
fs.stat(localFileName, function (error, stat) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
size = stat.size;
|
||||
self._createBlob(container, blob, blobType, size, options, creationCallback);
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads a blob into a file.
|
||||
* @ignore
|
||||
* @this {BlobService}
|
||||
* @param {string} container The container name.
|
||||
* @param {string} blob The blob name.
|
||||
* @param {string} localFileName The local path to the file to be downloaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {boolean} [options.skipSizeCheck] Skip the size check to perform direct download.
|
||||
* Set the option to true for small blobs.
|
||||
* Parallel download and speed summary won't work with this option on.
|
||||
* @param {SpeedSummary} [options.speedSummary] The download tracker objects.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {string} [options.snapshotId] The snapshot identifier.
|
||||
* @param {string} [options.leaseId] The lease identifier.
|
||||
* @param {string} [options.rangeStart] Return only the bytes of the blob in the specified range.
|
||||
* @param {string} [options.rangeEnd] Return only the bytes of the blob in the specified range.
|
||||
* @param {AccessConditions} [options.accessConditions] The access conditions.
|
||||
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions.
|
||||
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading blobs.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information if an error occurs;
|
||||
* otherwise `result` will contain the blob information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*
|
||||
*/
|
||||
BlobService.prototype._getBlobToLocalFile = function (container, blob, localFileName, optionsOrCallback, callback) {
|
||||
var options;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(blob);
|
||||
|
||||
var writeStream = fs.createWriteStream(localFileName, { 'highWaterMark': BlobConstants.MAX_QUEUED_WRITE_DISK_BUFFER_SIZE });
|
||||
writeStream.on('error', function (error) {
|
||||
callback(error);
|
||||
});
|
||||
|
||||
this.getBlobToStream(container, blob, writeStream, options, function (error, responseBlob, response) {
|
||||
if (error) {
|
||||
writeStream.end(function () {
|
||||
// If the download failed from the beginning, remove the file.
|
||||
if (fs.existsSync(localFileName) && writeStream.bytesWritten === 0) {
|
||||
fs.unlinkSync(localFileName);
|
||||
}
|
||||
callback(error, responseBlob, response);
|
||||
});
|
||||
} else {
|
||||
callback(error, responseBlob, response);
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
module.exports = BlobService;
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Expose 'BlobUtilities'.
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* Defines constants, enums, and utility functions for use with the Blob service.
|
||||
* @namespace BlobUtilities
|
||||
*/
|
||||
var BlobUtilities = {
|
||||
/**
|
||||
* Permission types
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SharedAccessPermissions: {
|
||||
READ: 'r',
|
||||
ADD: 'a',
|
||||
CREATE: 'c',
|
||||
WRITE: 'w',
|
||||
DELETE: 'd',
|
||||
LIST: 'l'
|
||||
},
|
||||
|
||||
/**
|
||||
* Blob listing details.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
BlobListingDetails: {
|
||||
SNAPSHOTS: 'snapshots',
|
||||
METADATA: 'metadata',
|
||||
UNCOMMITTED_BLOBS: 'uncommittedblobs',
|
||||
COPY: 'copy',
|
||||
DELETED: 'deleted'
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletion options for blob snapshots
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SnapshotDeleteOptions: {
|
||||
SNAPSHOTS_ONLY: 'only',
|
||||
BLOB_AND_SNAPSHOTS: 'include'
|
||||
},
|
||||
|
||||
/**
|
||||
* Type of block list to retrieve
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
BlockListFilter: {
|
||||
ALL: 'all',
|
||||
COMMITTED: 'committed',
|
||||
UNCOMMITTED: 'uncommitted'
|
||||
},
|
||||
|
||||
/**
|
||||
* Blobs and container public access types.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
BlobContainerPublicAccessType: {
|
||||
OFF: null,
|
||||
CONTAINER: 'container',
|
||||
BLOB: 'blob'
|
||||
},
|
||||
|
||||
/**
|
||||
* Describes actions that can be performed on a page blob sequence number.
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SequenceNumberAction: {
|
||||
MAX: 'max',
|
||||
UPDATE: 'update',
|
||||
INCREMENT: 'increment'
|
||||
},
|
||||
|
||||
/**
|
||||
* Candidate values for blob tiers.
|
||||
*
|
||||
* @property {object} PremiumPageBlobTier Candidate values for premium pageblob tiers.
|
||||
* @property {string} PremiumPageBlobTier.P4
|
||||
* @property {string} PremiumPageBlobTier.P6
|
||||
* @property {string} PremiumPageBlobTier.P10
|
||||
* @property {string} PremiumPageBlobTier.P20
|
||||
* @property {string} PremiumPageBlobTier.P30
|
||||
* @property {string} PremiumPageBlobTier.P40
|
||||
* @property {string} PremiumPageBlobTier.P50
|
||||
* @property {string} PremiumPageBlobTier.P60
|
||||
* @property {object} StandardBlobTier Candidate values for standard blobs tiers.
|
||||
* @property {string} StandardBlobTier.HOT
|
||||
* @property {string} StandardBlobTier.COOL
|
||||
* @property {string} StandardBlobTier.ARCHIVE
|
||||
*/
|
||||
BlobTier: {
|
||||
PremiumPageBlobTier: {
|
||||
P4: 'P4',
|
||||
P6: 'P6',
|
||||
P10: 'P10',
|
||||
P20: 'P20',
|
||||
P30: 'P30',
|
||||
P40: 'P40',
|
||||
P50: 'P50',
|
||||
P60: 'P60'
|
||||
},
|
||||
StandardBlobTier: {
|
||||
HOT: 'Hot',
|
||||
COOL: 'Cool',
|
||||
ARCHIVE: 'Archive'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = BlobUtilities;
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var Constants = require('./../../../common/util/constants');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var BlobUtilities = require('./../blobutilities');
|
||||
|
||||
/**
|
||||
* BlockBlob block range stream
|
||||
*/
|
||||
function BlockRangeStream(blobServiceClient, container, blob, options) {
|
||||
this.blobServiceClient = blobServiceClient;
|
||||
this.container = container;
|
||||
this.blob = blob;
|
||||
this._emitter = new EventEmitter();
|
||||
this._paused = false;
|
||||
this._emittedAll = false;
|
||||
this._emittedRangeType = null;
|
||||
this._emittedRangeIndex = null;
|
||||
this._offset = 0;
|
||||
this._rangelist = [];
|
||||
this._isEmitting = false;
|
||||
if (options.rangeStart) {
|
||||
this._startOffset = options.rangeStart;
|
||||
} else {
|
||||
this._startOffset = 0;
|
||||
}
|
||||
if (options.rangeEnd) {
|
||||
this._endOffset = options.rangeEnd;
|
||||
} else {
|
||||
this._endOffset = Number.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add event listener
|
||||
*/
|
||||
BlockRangeStream.prototype.on = function (event, listener) {
|
||||
this._emitter.on(event, listener);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get block list
|
||||
*/
|
||||
BlockRangeStream.prototype.list = function (options, callback) {
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (!options.blockListFilter) {
|
||||
options.blockListFilter = BlobUtilities.BlockListFilter.ALL;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
this.blobServiceClient.listBlocks(this.container, this.blob, options.blockListFilter, options, function (error, blocklist, response) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
var totalSize = parseInt(response.headers[Constants.HeaderConstants.BLOB_CONTENT_LENGTH], 10);
|
||||
if (!blocklist.CommittedBlocks) {
|
||||
//Convert single block blob to block blob range
|
||||
var name = 'NODESDK_BLOCKBLOB_RANGESTREAM';
|
||||
blocklist.CommittedBlocks = [{ Name : name, Size : totalSize }];
|
||||
}
|
||||
|
||||
self._rangelist = blocklist;
|
||||
self._emitBlockList();
|
||||
self = blocklist = null;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit block ranges
|
||||
*/
|
||||
BlockRangeStream.prototype._emitBlockList = function () {
|
||||
if (this._paused || this._emittedAll || this._isEmitting) return;
|
||||
|
||||
var self = this;
|
||||
this._getTypeList(function () {
|
||||
self._rangelist = null;
|
||||
self._emittedAll = true;
|
||||
self._emitter.emit('end');
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the block type list
|
||||
*/
|
||||
BlockRangeStream.prototype._getTypeList = function (callback) {
|
||||
this._isEmitting = true;
|
||||
try {
|
||||
var typeStart = false;
|
||||
if (this._rangelist) {
|
||||
for (var blockType in this._rangelist) {
|
||||
if (this._rangelist.hasOwnProperty(blockType)) {
|
||||
if (this._emittedRangeType === null || typeStart || this._emittedRangeType == blockType) {
|
||||
this._emittedRangeType = blockType;
|
||||
typeStart = true;
|
||||
} else if (this._emittedRangeType !== blockType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this._paused) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._emitBlockRange (blockType, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this._isEmitting = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the block list
|
||||
*/
|
||||
BlockRangeStream.prototype._emitBlockRange = function (blockType, callback) {
|
||||
var blockList = this._rangelist[blockType];
|
||||
var indexStart = false;
|
||||
for (var blockIndex = 0; blockIndex < blockList.length; blockIndex++) {
|
||||
if (this._emittedRangeIndex === null || indexStart || this._emittedRangeIndex === blockIndex) {
|
||||
this._emittedRangeIndex = blockIndex;
|
||||
indexStart = true;
|
||||
} else if (this._emittedRangeIndex !== blockIndex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this._paused) {
|
||||
return;
|
||||
}
|
||||
|
||||
var range = blockList[blockIndex];
|
||||
// follow the same naming convention of page ranges and json
|
||||
range.name = range.Name;
|
||||
range.type = blockType;
|
||||
range.start = this._offset;
|
||||
this._offset += parseInt(range.Size, 10);
|
||||
range.end = this._offset - 1;
|
||||
delete range.Name;
|
||||
delete range.Size;
|
||||
|
||||
if (range.start > this._endOffset) {
|
||||
break;
|
||||
} else if (range.end < this._startOffset) {
|
||||
continue;
|
||||
} else {
|
||||
range.start = Math.max(range.start, this._startOffset);
|
||||
range.end = Math.min(range.end, this._endOffset);
|
||||
range.size = range.end - range.start + 1;
|
||||
range.dataSize = range.size;
|
||||
this._emitter.emit('range', range);
|
||||
}
|
||||
}
|
||||
|
||||
// remove the used range and avoid memory leak
|
||||
this._rangelist[blockType] = null;
|
||||
|
||||
callback();
|
||||
};
|
||||
|
||||
/**
|
||||
* Pause the stream
|
||||
*/
|
||||
BlockRangeStream.prototype.pause = function () {
|
||||
this._paused = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resume the stream
|
||||
*/
|
||||
BlockRangeStream.prototype.resume = function () {
|
||||
this._paused = false;
|
||||
if (!this._isEmitting) {
|
||||
this._emitBlockList();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop the stream
|
||||
*/
|
||||
BlockRangeStream.prototype.stop = function () {
|
||||
this.pause();
|
||||
this._emittedAll = true;
|
||||
this._emitter.emit('end');
|
||||
};
|
||||
|
||||
module.exports = BlockRangeStream;
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var util = require('util');
|
||||
var RangeStream = require('./../../../common/streams/rangestream');
|
||||
var Constants = require('./../../../common/util/constants');
|
||||
|
||||
/**
|
||||
* PageBlob page range stream
|
||||
*/
|
||||
function PageRangeStream(blobServiceClient, container, blob, options) {
|
||||
PageRangeStream['super_'].call(this, blobServiceClient, container, blob, options);
|
||||
|
||||
if (options.minRangeSize) {
|
||||
this._minRangeSize = options.minRangeSize;
|
||||
} else {
|
||||
this._minRangeSize = Constants.BlobConstants.MIN_WRITE_PAGE_SIZE_IN_BYTES;
|
||||
}
|
||||
if (options.maxRangeSize) {
|
||||
this._maxRangeSize = options.maxRangeSize;
|
||||
} else {
|
||||
this._maxRangeSize = Constants.BlobConstants.DEFAULT_WRITE_PAGE_SIZE_IN_BYTES;
|
||||
}
|
||||
this._lengthHeader = Constants.HeaderConstants.BLOB_CONTENT_LENGTH;
|
||||
this._listFunc = blobServiceClient.listPageRanges;
|
||||
}
|
||||
|
||||
util.inherits(PageRangeStream, RangeStream);
|
||||
|
||||
module.exports = PageRangeStream;
|
||||
+304
@@ -0,0 +1,304 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = azureCommon.Constants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new BlobResult object.
|
||||
* @class
|
||||
* The BlobResult class is used to store the blob information.
|
||||
*
|
||||
* @property {string} container The container name.
|
||||
* @property {string} name The blob name.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the blob was last modified.
|
||||
* @property {string} contentLength The size of the blob in bytes.
|
||||
* @property {string} blobType The blob type.
|
||||
* @property {boolean} isIncrementalCopy If the blob is incremental copy blob.
|
||||
* @property {string} requestId The request id.
|
||||
* @property {string} sequenceNumber The current sequence number for a page blob.
|
||||
* @property {string} contentRange The content range.
|
||||
* @property {string} committedBlockCount The committed block count.
|
||||
* @property {string} serverEncrypted If the blob data and application metadata are completely encrypted using the specified algorithm. true/false.
|
||||
* @property {object} contentSettings The content settings.
|
||||
* @property {string} contentSettings.contentType The content type.
|
||||
* @property {string} contentSettings.contentEncoding The content encoding.
|
||||
* @property {string} contentSettings.contentLanguage The content language.
|
||||
* @property {string} contentSettings.cacheControl The cache control.
|
||||
* @property {string} contentSettings.contentDisposition The content disposition.
|
||||
* @property {string} contentSettings.contentMD5 The content MD5 hash.
|
||||
* @property {object} lease The lease information.
|
||||
* @property {string} lease.id The lease id.
|
||||
* @property {string} lease.status The lease status.
|
||||
* @property {string} lease.state The lease state.
|
||||
* @property {string} lease.duration The lease duration.
|
||||
* @property {object} copy The copy information.
|
||||
* @property {string} copy.id The copy id.
|
||||
* @property {string} copy.status The copy status.
|
||||
* @property {string} copy.completionTime The copy completion time.
|
||||
* @property {string} copy.statusDescription The copy status description.
|
||||
* @property {string} copy.destinationSnapshot The snapshot time of the last successful incremental copy snapshot for this blob.
|
||||
* @property {string} copy.progress The copy progress.
|
||||
* @property {string} copy.source The copy source.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [container] The container name.
|
||||
* @param {string} [name] The blob name.
|
||||
*/
|
||||
function BlobResult(container, name) {
|
||||
if (container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
if (name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
BlobResult.parse = function (blobXml) {
|
||||
var blobResult = new BlobResult();
|
||||
|
||||
for (var propertyName in blobXml) {
|
||||
if (blobXml.hasOwnProperty(propertyName)) {
|
||||
if (propertyName === 'Properties') {
|
||||
// Lift out the properties onto the main object to keep consistent across all APIs like: getBlobProperties
|
||||
azureutil.setPropertyValueFromXML(blobResult, blobXml[propertyName], true);
|
||||
} else if (propertyName === 'Metadata') {
|
||||
var resultPropertyName = azureutil.normalizePropertyNameFromXML(propertyName);
|
||||
blobResult[resultPropertyName] = {};
|
||||
azureutil.setPropertyValueFromXML(blobResult[resultPropertyName], blobXml[propertyName], false);
|
||||
} else {
|
||||
blobResult[propertyName.toLowerCase()] = blobXml[propertyName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (blobResult.isIncrementalCopy !== undefined) {
|
||||
blobResult.isIncrementalCopy = (blobResult.isIncrementalCopy === 'true');
|
||||
}
|
||||
|
||||
// convert accessTierInferred to boolean type
|
||||
if (blobResult.accessTierInferred !== undefined) {
|
||||
blobResult.accessTierInferred = (blobResult.accessTierInferred === 'true');
|
||||
}
|
||||
|
||||
if (blobResult.deleted !== undefined) {
|
||||
blobResult.deleted = (blobResult.deleted == 'true');
|
||||
}
|
||||
|
||||
if (blobResult.remainingRetentionDays !== undefined) {
|
||||
blobResult.remainingRetentionDays = parseInt(blobResult.remainingRetentionDays);
|
||||
}
|
||||
|
||||
return blobResult;
|
||||
};
|
||||
|
||||
var headersForProperties = {
|
||||
'lastModified': 'LAST_MODIFIED',
|
||||
'etag': 'ETAG',
|
||||
'sequenceNumber': 'SEQUENCE_NUMBER',
|
||||
'blobType': 'BLOB_TYPE',
|
||||
'contentLength': 'CONTENT_LENGTH',
|
||||
'blobContentLength': 'BLOB_CONTENT_LENGTH',
|
||||
'contentRange': 'CONTENT_RANGE',
|
||||
'committedBlockCount': 'BLOB_COMMITTED_BLOCK_COUNT',
|
||||
'serverEncrypted': 'SERVER_ENCRYPTED',
|
||||
'requestId': 'REQUEST_ID',
|
||||
|
||||
'range': 'RANGE',
|
||||
'blobRange': 'STORAGE_RANGE',
|
||||
'getContentMd5': 'RANGE_GET_CONTENT_MD5',
|
||||
'acceptRanges': 'ACCEPT_RANGES',
|
||||
'appendOffset': 'BLOB_APPEND_OFFSET',
|
||||
|
||||
'accessTier': 'ACCESS_TIER',
|
||||
'accessTierChangeTime': 'ACCESS_TIER_CHANGE_TIME',
|
||||
'accessTierInferred': 'ACCESS_TIER_INFERRED',
|
||||
'archiveStatus': 'ARCHIVE_STATUS',
|
||||
|
||||
'isIncrementalCopy': 'INCREMENTAL_COPY',
|
||||
|
||||
// ContentSettings
|
||||
'contentSettings.contentType': 'CONTENT_TYPE',
|
||||
'contentSettings.contentEncoding': 'CONTENT_ENCODING',
|
||||
'contentSettings.contentLanguage': 'CONTENT_LANGUAGE',
|
||||
'contentSettings.cacheControl': 'CACHE_CONTROL',
|
||||
'contentSettings.contentDisposition': 'CONTENT_DISPOSITION',
|
||||
'contentSettings.contentMD5': 'CONTENT_MD5',
|
||||
|
||||
// Lease
|
||||
'lease.id': 'LEASE_ID',
|
||||
'lease.status': 'LEASE_STATUS',
|
||||
'lease.duration': 'LEASE_DURATION',
|
||||
'lease.state': 'LEASE_STATE',
|
||||
|
||||
// Copy
|
||||
'copy.id': 'COPY_ID',
|
||||
'copy.status': 'COPY_STATUS',
|
||||
'copy.source': 'COPY_SOURCE',
|
||||
'copy.progress': 'COPY_PROGRESS',
|
||||
'copy.completionTime': 'COPY_COMPLETION_TIME',
|
||||
'copy.statusDescription': 'COPY_STATUS_DESCRIPTION',
|
||||
'copy.destinationSnapshot': 'COPY_DESTINATION_SNAPSHOT'
|
||||
};
|
||||
|
||||
BlobResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setBlobPropertyFromHeaders = function (blobProperty, headerProperty) {
|
||||
if (!azureutil.tryGetValueChain(self, blobProperty.split('.'), null) && headers[headerProperty.toLowerCase()]) {
|
||||
azureutil.setObjectInnerPropertyValue(self, blobProperty.split('.'), headers[headerProperty.toLowerCase()]);
|
||||
|
||||
if (blobProperty === 'copy.progress') {
|
||||
var info = azureutil.parseCopyProgress(self.copy.progress);
|
||||
self.copy.bytesCopied = parseInt(info.bytesCopied);
|
||||
self.copy.totalBytes = parseInt(info.totalBytes);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// For range get, 'x-ms-blob-content-md5' indicate the overall MD5 of the blob. Try to set the contentMD5 using this header if it presents
|
||||
setBlobPropertyFromHeaders('contentSettings.contentMD5', HeaderConstants.BLOB_CONTENT_MD5);
|
||||
|
||||
_.chain(headersForProperties).pairs().each(function (pair) {
|
||||
var property = pair[0];
|
||||
var header = HeaderConstants[pair[1]];
|
||||
setBlobPropertyFromHeaders(property, header);
|
||||
});
|
||||
|
||||
// convert isIncrementalCopy to boolean type
|
||||
if (self.isIncrementalCopy !== undefined) {
|
||||
self.isIncrementalCopy = (self.isIncrementalCopy === 'true');
|
||||
}
|
||||
|
||||
// convert accessTierInferred to boolean type
|
||||
if (self.accessTierInferred !== undefined) {
|
||||
self.accessTierInferred = (self.accessTierInferred == 'true');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This method sets the HTTP headers and is used by all methods except setBlobProperties and commitBlocks. Those 2 methods will set the x-ms-* headers using setPropertiesFromBlob.
|
||||
* @ignore
|
||||
*/
|
||||
BlobResult.setHeadersFromBlob = function (webResource, blob) {
|
||||
var setHeaderPropertyFromBlob = function (headerProperty, blobProperty) {
|
||||
var blobPropertyValue = azureutil.tryGetValueChain(blob, blobProperty.split('.'), null);
|
||||
if (blobPropertyValue) {
|
||||
webResource.withHeader(headerProperty, blobPropertyValue);
|
||||
}
|
||||
};
|
||||
|
||||
if (blob) {
|
||||
// Content-Type
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_TYPE, 'contentSettings.contentType');
|
||||
|
||||
// Content-Encoding
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_ENCODING, 'contentSettings.contentEncoding');
|
||||
|
||||
// Content-Language
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_LANGUAGE, 'contentSettings.contentLanguage');
|
||||
|
||||
// Content-Disposition
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_DISPOSITION, 'contentSettings.contentDisposition');
|
||||
|
||||
// Cache-Control
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CACHE_CONTROL, 'contentSettings.cacheControl');
|
||||
|
||||
// Blob's Content-MD5
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_MD5, 'contentSettings.contentMD5');
|
||||
|
||||
// Content-Length
|
||||
setHeaderPropertyFromBlob(HeaderConstants.CONTENT_LENGTH, 'contentLength');
|
||||
|
||||
// transactional Content-MD5
|
||||
setHeaderPropertyFromBlob(HeaderConstants.CONTENT_MD5, 'transactionalContentMD5');
|
||||
|
||||
// Range
|
||||
if (!azureutil.objectIsNull(blob.rangeStart)) {
|
||||
var range = 'bytes=' + blob.rangeStart + '-';
|
||||
|
||||
if (!azureutil.objectIsNull(blob.rangeEnd)) {
|
||||
range += blob.rangeEnd;
|
||||
}
|
||||
|
||||
webResource.withHeader(HeaderConstants.RANGE, range);
|
||||
}
|
||||
|
||||
// Blob Type
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_TYPE, 'blobType');
|
||||
|
||||
// Lease id
|
||||
setHeaderPropertyFromBlob(HeaderConstants.LEASE_ID, 'leaseId');
|
||||
|
||||
// Sequence number
|
||||
setHeaderPropertyFromBlob(HeaderConstants.SEQUENCE_NUMBER, 'sequenceNumber');
|
||||
setHeaderPropertyFromBlob(HeaderConstants.SEQUENCE_NUMBER_ACTION, 'sequenceNumberAction');
|
||||
|
||||
if (blob.metadata) {
|
||||
webResource.addOptionalMetadataHeaders(blob.metadata);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This method sets the x-ms-* headers and is used by setBlobProperties and commitBlocks. All other methods will set the regular HTTP headers using setHeadersFromBlob.
|
||||
* @ignore
|
||||
*/
|
||||
BlobResult.setPropertiesFromBlob = function (webResource, blob) {
|
||||
var setHeaderPropertyFromBlob = function (headerProperty, blobProperty) {
|
||||
var propertyValue = azureutil.tryGetValueChain(blob, blobProperty.split('.'), null);
|
||||
if (propertyValue) {
|
||||
webResource.withHeader(headerProperty, propertyValue);
|
||||
}
|
||||
};
|
||||
|
||||
if (blob) {
|
||||
// Content-Type
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_TYPE, 'contentSettings.contentType');
|
||||
|
||||
// Content-Encoding
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_ENCODING, 'contentSettings.contentEncoding');
|
||||
|
||||
// Content-Language
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_LANGUAGE, 'contentSettings.contentLanguage');
|
||||
|
||||
// Content-Disposition
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_DISPOSITION, 'contentSettings.contentDisposition');
|
||||
|
||||
// Cache-Control
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CACHE_CONTROL, 'contentSettings.cacheControl');
|
||||
|
||||
// Content-MD5
|
||||
setHeaderPropertyFromBlob(HeaderConstants.BLOB_CONTENT_MD5, 'contentSettings.contentMD5');
|
||||
|
||||
// Lease id
|
||||
setHeaderPropertyFromBlob(HeaderConstants.LEASE_ID, 'leaseId');
|
||||
|
||||
if (blob.metadata) {
|
||||
webResource.addOptionalMetadataHeaders(blob.metadata);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = BlobResult;
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var xmlbuilder = azureCommon.xmlbuilder;
|
||||
var Constants = azureCommon.Constants;
|
||||
|
||||
/**
|
||||
* Builds an XML representation for a block list.
|
||||
*
|
||||
* @param {array} The block list.
|
||||
* @return {string} The XML block list.
|
||||
*/
|
||||
exports.serialize = function (blockListJs) {
|
||||
var blockListDoc = xmlbuilder.create();
|
||||
blockListDoc = blockListDoc.begin(Constants.BlobConstants.BLOCK_LIST_ELEMENT, { version: '1.0', encoding: 'utf-8' });
|
||||
|
||||
if (_.isArray(blockListJs.LatestBlocks)) {
|
||||
blockListJs.LatestBlocks.forEach(function (block) {
|
||||
blockListDoc = blockListDoc.ele(Constants.BlobConstants.LATEST_ELEMENT)
|
||||
.txt(new Buffer(block).toString('base64'))
|
||||
.up();
|
||||
});
|
||||
}
|
||||
|
||||
if (_.isArray(blockListJs.CommittedBlocks)) {
|
||||
blockListJs.CommittedBlocks.forEach(function (block) {
|
||||
blockListDoc = blockListDoc.ele(Constants.BlobConstants.COMMITTED_ELEMENT)
|
||||
.txt(new Buffer(block).toString('base64'))
|
||||
.up();
|
||||
});
|
||||
}
|
||||
|
||||
if (_.isArray(blockListJs.UncommittedBlocks)) {
|
||||
blockListJs.UncommittedBlocks.forEach(function (block) {
|
||||
blockListDoc = blockListDoc.ele(Constants.BlobConstants.UNCOMMITTED_ELEMENT)
|
||||
.txt(new Buffer(block).toString('base64'))
|
||||
.up();
|
||||
});
|
||||
}
|
||||
|
||||
return blockListDoc.doc().toString();
|
||||
};
|
||||
|
||||
exports.parse = function (blockListXml) {
|
||||
var blockListResult = {};
|
||||
|
||||
if (blockListXml.CommittedBlocks && blockListXml.CommittedBlocks.Block) {
|
||||
blockListResult.CommittedBlocks = blockListXml.CommittedBlocks.Block;
|
||||
if (!_.isArray(blockListResult.CommittedBlocks)) {
|
||||
blockListResult.CommittedBlocks = [blockListResult.CommittedBlocks];
|
||||
}
|
||||
blockListResult.CommittedBlocks.forEach(function(block) {
|
||||
block.Name = new Buffer(block.Name, 'base64').toString();
|
||||
});
|
||||
}
|
||||
|
||||
if (blockListXml.UncommittedBlocks && blockListXml.UncommittedBlocks.Block) {
|
||||
blockListResult.UncommittedBlocks = blockListXml.UncommittedBlocks.Block;
|
||||
if (!_.isArray(blockListResult.UncommittedBlocks)) {
|
||||
blockListResult.UncommittedBlocks = [blockListResult.UncommittedBlocks];
|
||||
}
|
||||
blockListResult.UncommittedBlocks.forEach(function(block) {
|
||||
block.Name = new Buffer(block.Name, 'base64').toString();
|
||||
});
|
||||
}
|
||||
|
||||
return blockListResult;
|
||||
};
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = azureCommon.Constants;
|
||||
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
var BlobUtilities = require('../blobutilities');
|
||||
|
||||
/**
|
||||
* Creates a new ContainerResult object.
|
||||
* @class
|
||||
* The ContainerResult class is used to store the container information.
|
||||
*
|
||||
* @property {string} name The container name.
|
||||
* @property {string} publicAccessLevel The public access level.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the container was last modified.
|
||||
* @property {string} requestId The request id.
|
||||
* @property {object} lease The lease information.
|
||||
* @property {string} lease.status The lease status.
|
||||
* @property {string} lease.state The lease state.
|
||||
* @property {string} lease.duration The lease duration.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [container] The container name.
|
||||
* @param {string} [publicAccessLevel] The public access level.
|
||||
*/
|
||||
function ContainerResult(name, publicAccessLevel) {
|
||||
if (name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
if (publicAccessLevel) {
|
||||
this.publicAccessLevel = publicAccessLevel;
|
||||
}
|
||||
}
|
||||
|
||||
ContainerResult.parse = function (containerXml) {
|
||||
var containerResult = new ContainerResult();
|
||||
|
||||
for (var propertyName in containerXml) {
|
||||
if (containerXml.hasOwnProperty(propertyName)) {
|
||||
if (propertyName === 'Properties') {
|
||||
// Lift out the properties onto the main object to keep consistent across all APIs like: getContainerProperties
|
||||
azureutil.setPropertyValueFromXML(containerResult, containerXml[propertyName], true);
|
||||
} else if (propertyName === 'Metadata') {
|
||||
var resultPropertyName = azureutil.normalizePropertyNameFromXML(propertyName);
|
||||
containerResult[resultPropertyName] = {};
|
||||
azureutil.setPropertyValueFromXML(containerResult[resultPropertyName], containerXml[propertyName], false);
|
||||
} else {
|
||||
containerResult[propertyName.toLowerCase()] = containerXml[propertyName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!containerResult.publicAccessLevel) {
|
||||
containerResult.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.OFF;
|
||||
}
|
||||
|
||||
return containerResult;
|
||||
};
|
||||
|
||||
ContainerResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setContainerPropertyFromHeaders = function (containerProperty, headerProperty) {
|
||||
if (!azureutil.tryGetValueChain(self, containerProperty.split('.'), null) && headers[headerProperty.toLowerCase()]) {
|
||||
azureutil.setObjectInnerPropertyValue(self, containerProperty.split('.'), headers[headerProperty.toLowerCase()]);
|
||||
}
|
||||
};
|
||||
|
||||
setContainerPropertyFromHeaders('etag', HeaderConstants.ETAG);
|
||||
setContainerPropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED);
|
||||
setContainerPropertyFromHeaders('lease.status', HeaderConstants.LEASE_STATUS);
|
||||
setContainerPropertyFromHeaders('lease.state', HeaderConstants.LEASE_STATE);
|
||||
setContainerPropertyFromHeaders('lease.duration', HeaderConstants.LEASE_DURATION);
|
||||
setContainerPropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID);
|
||||
|
||||
if (!self.publicAccessLevel) {
|
||||
self.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.OFF;
|
||||
if (headers[HeaderConstants.BLOB_PUBLIC_ACCESS]) {
|
||||
self.publicAccessLevel = headers[HeaderConstants.BLOB_PUBLIC_ACCESS];
|
||||
}
|
||||
}
|
||||
|
||||
if (self.publicAccessLevel === 'true') {
|
||||
// The container was marked for full public read access using a version prior to 2009-09-19.
|
||||
self.publicAccessLevel = BlobUtilities.BlobContainerPublicAccessType.CONTAINER;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The container ACL settings.
|
||||
* @typedef {object} ContainerAclResult
|
||||
* @extends {ContainerResult}
|
||||
* @property {Object.<string, AccessPolicy>} signedIdentifiers The container ACL settings. See `[AccessPolicy]{@link AccessPolicy}` for detailed information.
|
||||
*/
|
||||
|
||||
module.exports = ContainerResult;
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var Constants = require('./../../../common/common.core').Constants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new LeaseResult object.
|
||||
* @class
|
||||
* The LeaseResult class is used to store the lease information.
|
||||
*
|
||||
* @property {string} container The container name.
|
||||
* @property {string} blob The blob name.
|
||||
* @property {string} id The lease id.
|
||||
* @property {string} time Approximate time remaining in the lease period, in seconds.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the lease was last modified.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [container] The container name.
|
||||
* @param {string} [blob] The blob name.
|
||||
* @param {string} [id] The lease id.
|
||||
* @param {string} [time] Approximate time remaining in the lease period, in seconds.
|
||||
*/
|
||||
function LeaseResult(container, blob, id, time) {
|
||||
if (container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
if (blob) {
|
||||
this.blob = blob;
|
||||
}
|
||||
|
||||
if (id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
if (time) {
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
|
||||
LeaseResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
if (!self['id'] && headers[HeaderConstants.LEASE_ID]) {
|
||||
self['id'] = headers[HeaderConstants.LEASE_ID];
|
||||
}
|
||||
|
||||
if (!self['time'] && headers[HeaderConstants.LEASE_TIME]) {
|
||||
self['time'] = parseInt(headers[HeaderConstants.LEASE_TIME], 10);
|
||||
}
|
||||
|
||||
self['etag'] = headers[HeaderConstants.ETAG];
|
||||
self['lastModified'] = headers[HeaderConstants.LAST_MODIFIED.toLowerCase()];
|
||||
};
|
||||
|
||||
module.exports = LeaseResult;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var FileService = require('./fileservice.core');
|
||||
var azureCommon = require('./../../common/common.browser');
|
||||
var extend = require('extend');
|
||||
|
||||
var azureutil = azureCommon.util;
|
||||
var BrowserFileReadStream = azureCommon.BrowserFileReadStream;
|
||||
var SpeedSummary = azureCommon.SpeedSummary;
|
||||
var validate = azureCommon.validate;
|
||||
var ChunkStreamWithStream = azureCommon.ChunkStreamWithStream;
|
||||
|
||||
/**
|
||||
* Uploads a file to storage from an HTML File object. If the file already exists on the service, it will be overwritten.
|
||||
* (Only available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {FileService}
|
||||
* @param {string} share The share name.
|
||||
* @param {string} directory The directory name. Use '' to refer to the base directory.
|
||||
* @param {string} file The file name. File names may not start or end with the delimiter '/'.
|
||||
* @param {File} browserFile The File object to be uploaded created by HTML File API.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {SpeedSummary} [options.speedSummary] The download tracker objects;
|
||||
* @param {bool} [options.storeFileContentMD5] Specifies whether the file's ContentMD5 header should be set on uploads.
|
||||
* The default value is false for files.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {object} [options.contentSettings] The file's content settings.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the file. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the file.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The file service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The file's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The file's MD5 hash.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information if an error occurs;
|
||||
* otherwise `[result]{@link FileResult}` will contain the file information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
FileService.prototype.createFileFromBrowserFile = function (share, directory, file, browserFile, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('createFileFromBrowserFile', function (v) {
|
||||
v.string(share, 'share');
|
||||
v.stringAllowEmpty(directory, 'directory');
|
||||
v.string(file, 'file');
|
||||
v.browserFileIsValid(browserFile);
|
||||
v.shareNameIsValid(share);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(file);
|
||||
|
||||
var self = this;
|
||||
this.createFile(share, directory, file, browserFile.size, options, function (error) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
var stream = new BrowserFileReadStream(browserFile);
|
||||
var chunkStream = new ChunkStreamWithStream(stream, { calcContentMd5: options.storeFileContentMD5 });
|
||||
self._createFileFromChunkStream(share, directory, file, chunkStream, browserFile.size, options, callback);
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
module.exports = FileService;
|
||||
+3937
File diff suppressed because it is too large.
Load diff
+182
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureCommon = require('./../../common/common.node');
|
||||
var extend = require('extend');
|
||||
var fs = require('fs');
|
||||
var FileService = require('./fileservice.core');
|
||||
|
||||
var azureutil = azureCommon.util;
|
||||
var FileReadStream = azureCommon.FileReadStream;
|
||||
var SpeedSummary = azureCommon.SpeedSummary;
|
||||
var validate = azureCommon.validate;
|
||||
|
||||
/**
|
||||
* Downloads an Azure file into a file.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {FileService}
|
||||
* @param {string} share The share name.
|
||||
* @param {string} directory The directory name. Use '' to refer to the base directory.
|
||||
* @param {string} file The file name. File names may not start or end with the delimiter '/'.
|
||||
* @param {string} localFileName The local path to the file to be downloaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {string} [options.shareSnapshotId] The snapshot identifier of the share.
|
||||
* @param {boolean} [options.skipSizeCheck] Skip the size check to perform direct download.
|
||||
* Set the option to true for small files.
|
||||
* Parallel download and speed summary won't work with this option on.
|
||||
* @param {SpeedSummary} [options.speedSummary] The download tracker objects.
|
||||
* @param {int} [options.parallelOperationThreadCount] The number of parallel operations that may be performed when uploading.
|
||||
* @param {string} [options.rangeStart] Return only the bytes of the file in the specified range.
|
||||
* @param {string} [options.rangeEnd] Return only the bytes of the file in the specified range.
|
||||
* @param {boolean} [options.useTransactionalMD5] When set to true, Calculate and send/validate content MD5 for transactions.
|
||||
* @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading files.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information if an error occurs;
|
||||
* otherwise `[result]{@link FileResult}` will contain the file information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*
|
||||
* @example
|
||||
* var azure = require('azure-storage');
|
||||
* var FileService = azure.createFileService();
|
||||
* FileService.getFileToLocalFile('taskshare', taskdirectory', 'task1', 'task1-download.txt', function(error, serverFile) {
|
||||
* if(!error) {
|
||||
* // file available in serverFile.file variable
|
||||
* }
|
||||
*/
|
||||
FileService.prototype.getFileToLocalFile = function (share, directory, file, localFileName, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
userOptions.speedSummary = userOptions.speedSummary || new SpeedSummary(file);
|
||||
|
||||
validate.validateArgs('getFileToLocalFile', function (v) {
|
||||
v.string(share, 'share');
|
||||
v.stringAllowEmpty(directory, 'directory');
|
||||
v.string(file, 'file');
|
||||
v.string(localFileName, 'localFileName');
|
||||
v.shareNameIsValid(share);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
|
||||
var writeStream = fs.createWriteStream(localFileName);
|
||||
writeStream.on('error', function (error) {
|
||||
callback(error);
|
||||
});
|
||||
|
||||
this.getFileToStream(share, directory, file, writeStream, options, function (error, responseFile, response) {
|
||||
if (error) {
|
||||
writeStream.end(function () {
|
||||
// If the download failed from the beginning, remove the file.
|
||||
if (fs.existsSync(localFileName) && writeStream.bytesWritten === 0) {
|
||||
fs.unlinkSync(localFileName);
|
||||
}
|
||||
callback(error, responseFile, response);
|
||||
});
|
||||
} else {
|
||||
callback(error, responseFile, response);
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Uploads a file to storage from a local file. If the file already exists on the service, it will be overwritten.
|
||||
* (Not available in the JavaScript Client Library for Browsers)
|
||||
*
|
||||
* @this {FileService}
|
||||
* @param {string} share The share name.
|
||||
* @param {string} directory The directory name. Use '' to refer to the base directory.
|
||||
* @param {string} file The file name. File names may not start or end with the delimiter '/'.
|
||||
* @param (string) localFileName The local path to the file to be uploaded.
|
||||
* @param {object} [options] The request options.
|
||||
* @param {SpeedSummary} [options.speedSummary] The download tracker objects;
|
||||
* @param {bool} [options.storeFileContentMD5] Specifies whether the file's ContentMD5 header should be set on uploads.
|
||||
* The default value is false for files.
|
||||
* @param {bool} [options.useTransactionalMD5] Calculate and send/validate content MD5 for transactions.
|
||||
* @param {object} [options.contentSettings] The file's content settings.
|
||||
* @param {string} [options.contentSettings.contentType] The MIME content type of the file. The default type is application/octet-stream.
|
||||
* @param {string} [options.contentSettings.contentEncoding] The content encodings that have been applied to the file.
|
||||
* @param {string} [options.contentSettings.contentLanguage] The natural languages used by this resource.
|
||||
* @param {string} [options.contentSettings.cacheControl] The file service stores this value but does not use or modify it.
|
||||
* @param {string} [options.contentSettings.contentDisposition] The file's content disposition.
|
||||
* @param {string} [options.contentSettings.contentMD5] The file's MD5 hash.
|
||||
* @param {object} [options.metadata] The metadata key/value pairs.
|
||||
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
|
||||
* Please see StorageUtilities.LocationMode for the possible values.
|
||||
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
|
||||
* @param {int} [options.clientRequestTimeoutInMs] The timeout of client requests, in milliseconds, to use for the request.
|
||||
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
|
||||
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
|
||||
* execution time is checked intermittently while performing requests, and before executing retries.
|
||||
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
|
||||
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
|
||||
* The default value is false.
|
||||
* @param {errorOrResult} callback `error` will contain information if an error occurs;
|
||||
* otherwise `[result]{@link FileResult}` will contain the file information.
|
||||
* `response` will contain information related to this operation.
|
||||
* @return {SpeedSummary}
|
||||
*/
|
||||
FileService.prototype.createFileFromLocalFile = function (share, directory, file, localFileName, optionsOrCallback, callback) {
|
||||
var userOptions;
|
||||
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });
|
||||
|
||||
validate.validateArgs('createFileFromLocalFile', function (v) {
|
||||
v.string(share, 'share');
|
||||
v.stringAllowEmpty(directory, 'directory');
|
||||
v.string(file, 'file');
|
||||
v.string(localFileName, 'localFileName');
|
||||
v.shareNameIsValid(share);
|
||||
v.callback(callback);
|
||||
});
|
||||
|
||||
var options = extend(true, {}, userOptions);
|
||||
options.speedSummary = options.speedSummary || new SpeedSummary(file);
|
||||
|
||||
var self = this;
|
||||
fs.stat(localFileName, function(error, stat) {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
self.createFile(share, directory, file, stat.size, options, function(error) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
} else {
|
||||
var stream = new FileReadStream(localFileName, {calcContentMd5: options.storeFileContentMD5});
|
||||
self._createFileFromChunkStream(share, directory, file, stream, stat.size, options, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return options.speedSummary;
|
||||
};
|
||||
|
||||
module.exports = FileService;
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Expose 'FileUtilities'.
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* Defines constants, enums, and utility functions for use with the File service.
|
||||
* @namespace FileUtilities
|
||||
*/
|
||||
var FileUtilities = {
|
||||
/**
|
||||
* Permission types
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SharedAccessPermissions: {
|
||||
READ: 'r',
|
||||
CREATE: 'c',
|
||||
WRITE: 'w',
|
||||
DELETE: 'd',
|
||||
LIST: 'l'
|
||||
},
|
||||
|
||||
/**
|
||||
* Listing details.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
ListingDetails: {
|
||||
METADATA: 'metadata'
|
||||
},
|
||||
|
||||
/**
|
||||
* File and share public access types.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SharePublicAccessType: {
|
||||
OFF: null,
|
||||
SHARE: 'share',
|
||||
FILE: 'file'
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletion options for share snapshots
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
ShareSnapshotDeleteOptions: {
|
||||
SHARE_AND_SNAPSHOTS: 'include'
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = FileUtilities;
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var util = require('util');
|
||||
var RangeStream = require('./../../../common/streams/rangestream');
|
||||
var Constants = require('./../../../common/util/constants');
|
||||
|
||||
/**
|
||||
* File range stream
|
||||
*/
|
||||
function FileRangeStream(fileServiceClient, share, directory, file, options) {
|
||||
FileRangeStream['super_'].call(this, fileServiceClient, null, null, options);
|
||||
|
||||
this._lengthHeader = Constants.HeaderConstants.FILE_CONTENT_LENGTH;
|
||||
if (options.minRangeSize) {
|
||||
this._minRangeSize = options.minRangeSize;
|
||||
} else {
|
||||
this._minRangeSize = Constants.FileConstants.MIN_WRITE_FILE_SIZE_IN_BYTES;
|
||||
}
|
||||
if (options.maxRangeSize) {
|
||||
this._maxRangeSize = options.maxRangeSize;
|
||||
} else {
|
||||
this._maxRangeSize = Constants.FileConstants.DEFAULT_WRITE_SIZE_IN_BYTES;
|
||||
}
|
||||
this._listFunc = fileServiceClient.listRanges;
|
||||
this._resourcePath.push(share);
|
||||
this._resourcePath.push(directory);
|
||||
this._resourcePath.push(file);
|
||||
}
|
||||
|
||||
util.inherits(FileRangeStream, RangeStream);
|
||||
|
||||
module.exports = FileRangeStream;
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var HeaderConstants = require('./../../../common/common.core').Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new DirectoryResult object.
|
||||
* @class
|
||||
* The DirectoryResult class is used to store the directory information.
|
||||
*
|
||||
* @property {string} name The container name.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the directory was last modified.
|
||||
* @property {string} requestId The request id.
|
||||
* @property {string} serverEncrypted If the directory metadata is completely encrypted using the specified algorithm. true/false.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [name] The directory name.
|
||||
*/
|
||||
function DirectoryResult(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
DirectoryResult.parse = function (dirXml) {
|
||||
return new DirectoryResult(dirXml.Name);
|
||||
};
|
||||
|
||||
DirectoryResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setDirectoryPropertyFromHeaders = function (directoryProperty, headerProperty) {
|
||||
if (!self[directoryProperty] && headers[headerProperty.toLowerCase()]) {
|
||||
self[directoryProperty] = headers[headerProperty.toLowerCase()];
|
||||
}
|
||||
};
|
||||
|
||||
setDirectoryPropertyFromHeaders('etag', HeaderConstants.ETAG);
|
||||
setDirectoryPropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED);
|
||||
setDirectoryPropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID);
|
||||
setDirectoryPropertyFromHeaders('serverEncrypted', HeaderConstants.SERVER_ENCRYPTED);
|
||||
};
|
||||
|
||||
module.exports = DirectoryResult;
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var _ = require('underscore');
|
||||
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = azureCommon.Constants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new FileResult object.
|
||||
* @class
|
||||
* The FileResult class is used to store the file information.
|
||||
*
|
||||
* @property {string} share The share name.
|
||||
* @property {string} directory The directory name.
|
||||
* @property {string} name The file name.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the file was last modified.
|
||||
* @property {string} requestId The request id.
|
||||
* @property {string} acceptRanges The accept ranges.
|
||||
* @property {string} serverEncrypted If the file data and application metadata are completely encrypted using the specified algorithm. true/false.
|
||||
* @property {string} contentRange The content range
|
||||
* @property {string} contentLength The size of the file in bytes.
|
||||
* @property {object} contentSettings The content settings.
|
||||
* @property {string} contentSettings.contentType The content type.
|
||||
* @property {string} contentSettings.contentEncoding The content encoding.
|
||||
* @property {string} contentSettings.contentLanguage The content language.
|
||||
* @property {string} contentSettings.cacheControl The cache control.
|
||||
* @property {string} contentSettings.contentDisposition The content disposition.
|
||||
* @property {string} contentSettings.contentMD5 The content MD5 hash.
|
||||
* @property {object} copy The copy information.
|
||||
* @property {string} copy.id The copy id.
|
||||
* @property {string} copy.status The copy status.
|
||||
* @property {string} copy.completionTime The copy completion time.
|
||||
* @property {string} copy.statusDescription The copy status description.
|
||||
* @property {string} copy.progress The copy progress.
|
||||
* @property {string} copy.source The copy source.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [share] The share name.
|
||||
* @param {string} [directory] The directory name.
|
||||
* @param {string} [name] The file name.
|
||||
*/
|
||||
function FileResult(share, directory, name) {
|
||||
this.share = share;
|
||||
this.directory = directory;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
FileResult.parse = function (entryXml) {
|
||||
var listResult = new FileResult();
|
||||
for (var propertyName in entryXml) {
|
||||
if (propertyName === 'Properties') {
|
||||
// Lift out the properties onto the main object to keep consistent across all APIs like: getFileProperties
|
||||
azureutil.setPropertyValueFromXML(listResult, entryXml[propertyName], true);
|
||||
} else {
|
||||
listResult[propertyName.toLowerCase()] = entryXml[propertyName];
|
||||
}
|
||||
}
|
||||
|
||||
return listResult;
|
||||
};
|
||||
|
||||
var responseHeaders = {
|
||||
'acceptRanges': 'ACCEPT_RANGES',
|
||||
'contentLength': 'CONTENT_LENGTH',
|
||||
'contentRange': 'CONTENT_RANGE',
|
||||
|
||||
'contentSettings.contentType': 'CONTENT_TYPE',
|
||||
'contentSettings.contentEncoding': 'CONTENT_ENCODING',
|
||||
'contentSettings.contentLanguage': 'CONTENT_LANGUAGE',
|
||||
'contentSettings.cacheControl': 'CACHE_CONTROL',
|
||||
'contentSettings.contentDisposition': 'CONTENT_DISPOSITION',
|
||||
'contentSettings.contentMD5': 'CONTENT_MD5',
|
||||
'contentSettings.fileContentMD5': 'FILE_CONTENT_MD5',
|
||||
|
||||
'copy.id': 'COPY_ID',
|
||||
'copy.status': 'COPY_STATUS',
|
||||
'copy.source': 'COPY_SOURCE',
|
||||
'copy.progress': 'COPY_PROGRESS',
|
||||
'copy.completionTime': 'COPY_COMPLETION_TIME',
|
||||
'copy.statusDescription': 'COPY_STATUS_DESCRIPTION'
|
||||
};
|
||||
|
||||
FileResult.prototype.getPropertiesFromHeaders = function (headers, content) {
|
||||
var self = this;
|
||||
|
||||
var setFilePropertyFromHeaders = function (fileProperty, headerProperty) {
|
||||
if (!azureutil.tryGetValueChain(self, fileProperty.split('.'), null) && headers[headerProperty.toLowerCase()]) {
|
||||
azureutil.setObjectInnerPropertyValue(self, fileProperty.split('.'), headers[headerProperty.toLowerCase()]);
|
||||
|
||||
if (fileProperty === 'copy.progress') {
|
||||
var info = azureutil.parseCopyProgress(self.copy.progress);
|
||||
self.copy.bytesCopied = parseInt(info.bytesCopied);
|
||||
self.copy.totalBytes = parseInt(info.totalBytes);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// For range get, 'x-ms-content-md5' indicate the overall MD5 of the file. Try to set the contentMD5 using this header if it presents
|
||||
setFilePropertyFromHeaders('contentSettings.contentMD5', HeaderConstants.FILE_CONTENT_MD5);
|
||||
|
||||
setFilePropertyFromHeaders('etag', HeaderConstants.ETAG);
|
||||
setFilePropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED);
|
||||
setFilePropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID);
|
||||
setFilePropertyFromHeaders('serverEncrypted', HeaderConstants.SERVER_ENCRYPTED);
|
||||
|
||||
if (content) {
|
||||
_.chain(responseHeaders).pairs().each(function (pair) {
|
||||
var property = pair[0];
|
||||
var header = HeaderConstants[pair[1]];
|
||||
setFilePropertyFromHeaders(property, header);
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This method sets the HTTP headers and is used by all methods except setFileProperties and createFile.
|
||||
* Those methods will set the x-ms-* headers using setProperties.
|
||||
*/
|
||||
FileResult.setHeaders = function (webResource, options) {
|
||||
var setHeaderProperty = function (headerProperty, fileProperty) {
|
||||
var propertyValue = azureutil.tryGetValueChain(options, fileProperty.split('.'), null);
|
||||
if (propertyValue) {
|
||||
webResource.withHeader(headerProperty, propertyValue);
|
||||
}
|
||||
};
|
||||
|
||||
if (options) {
|
||||
// Content-MD5
|
||||
setHeaderProperty(HeaderConstants.CONTENT_MD5, 'transactionalContentMD5');
|
||||
|
||||
// Content-Length
|
||||
setHeaderProperty(HeaderConstants.CONTENT_LENGTH, 'contentLength');
|
||||
|
||||
// Range
|
||||
if (!azureutil.objectIsNull(options.rangeStart)) {
|
||||
var range = 'bytes=' + options.rangeStart + '-';
|
||||
|
||||
if (!azureutil.objectIsNull(options.rangeEnd)) {
|
||||
range += options.rangeEnd;
|
||||
}
|
||||
|
||||
webResource.withHeader(HeaderConstants.STORAGE_RANGE, range);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This method sets the x-ms-* headers and is used by setFileProperties and createFile.
|
||||
* All other methods will set the regular HTTP headers using setHeaders.
|
||||
*/
|
||||
FileResult.setProperties = function (webResource, options) {
|
||||
var setHeaderProperty = function (headerProperty, fileProperty) {
|
||||
var propertyValue = azureutil.tryGetValueChain(options, fileProperty.split('.'), null);
|
||||
if (propertyValue) {
|
||||
webResource.withHeader(headerProperty, propertyValue);
|
||||
}
|
||||
};
|
||||
|
||||
if (options) {
|
||||
// Content-Length
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_LENGTH, 'contentLength');
|
||||
|
||||
// Content-Type
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_TYPE, 'contentSettings.contentType');
|
||||
|
||||
// Content-Encoding
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_ENCODING, 'contentSettings.contentEncoding');
|
||||
|
||||
// Content-Language
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_LANGUAGE, 'contentSettings.contentLanguage');
|
||||
|
||||
// Content-Disposition
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_DISPOSITION, 'contentSettings.contentDisposition');
|
||||
|
||||
// Cache-Control
|
||||
setHeaderProperty(HeaderConstants.FILE_CACHE_CONTROL, 'contentSettings.cacheControl');
|
||||
|
||||
// Content-MD5
|
||||
setHeaderProperty(HeaderConstants.FILE_CONTENT_MD5, 'contentSettings.contentMD5');
|
||||
|
||||
if (options.metadata) {
|
||||
webResource.addOptionalMetadataHeaders(options.metadata);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = FileResult;
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var Constants = azureCommon.Constants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new ShareResult object.
|
||||
* @class
|
||||
* The ShareResult class is used to store the share information.
|
||||
*
|
||||
* @property {string} name The share name.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {string} etag The etag.
|
||||
* @property {string} lastModified The date/time that the share was last modified.
|
||||
* @property {string} requestId The request id.
|
||||
* @property {string} quota The share quota.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [name] The share name.
|
||||
*/
|
||||
function ShareResult(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
ShareResult.parse = function (shareXml, name) {
|
||||
var shareResult = new ShareResult(name);
|
||||
for (var propertyName in shareXml) {
|
||||
if (shareXml.hasOwnProperty(propertyName)) {
|
||||
if (propertyName === 'Properties') {
|
||||
// Lift out the properties onto the main object to keep consistent across all APIs like: getShareProperties
|
||||
azureutil.setPropertyValueFromXML(shareResult, shareXml[propertyName], true);
|
||||
} else if (propertyName === 'Metadata' || propertyName === 'ShareStats') {
|
||||
var resultPropertyName = azureutil.normalizePropertyNameFromXML(propertyName);
|
||||
shareResult[resultPropertyName] = {};
|
||||
azureutil.setPropertyValueFromXML(shareResult[resultPropertyName], shareXml[propertyName], propertyName === 'ShareStats');
|
||||
} else {
|
||||
shareResult[propertyName.toLowerCase()] = shareXml[propertyName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shareResult;
|
||||
};
|
||||
|
||||
ShareResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setSharePropertyFromHeaders = function (shareProperty, headerProperty) {
|
||||
if (!self[shareProperty] && headers[headerProperty.toLowerCase()]) {
|
||||
self[shareProperty] = headers[headerProperty.toLowerCase()];
|
||||
}
|
||||
};
|
||||
|
||||
setSharePropertyFromHeaders('etag', HeaderConstants.ETAG);
|
||||
setSharePropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED);
|
||||
setSharePropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID);
|
||||
setSharePropertyFromHeaders('quota', HeaderConstants.SHARE_QUOTA);
|
||||
};
|
||||
|
||||
/**
|
||||
* The share ACL settings.
|
||||
* @typedef {object} ShareAclResult
|
||||
* @extends {ShareAclResult}
|
||||
* @property {Object.<string, AccessPolicy>} signedIdentifiers The container ACL settings. See `[AccessPolicy]{@link AccessPolicy}` for detailed information.
|
||||
*/
|
||||
|
||||
module.exports = ShareResult;
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var xmlbuilder = azureCommon.xmlbuilder;
|
||||
var Constants = azureCommon.Constants;
|
||||
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new QueueMessageResult object.
|
||||
* @class
|
||||
* The QueueMessageResult class is used to store the queue message information.
|
||||
*
|
||||
* @property {string} queue The queue name.
|
||||
* @property {string} messageId The message id.
|
||||
* @property {string} popReceipt The pop receipt.
|
||||
* @property {string} messageText The message text.
|
||||
* @property {string} timeNextVisible The time next visible.
|
||||
* @property {string} insertionTime The insertion time.
|
||||
* @property {string} expirationTime The expiration time.
|
||||
* @property {number} dequeueCount The dequeue count.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [queue] The queue name.
|
||||
* @param {string} [messageId] The message id.
|
||||
* @param {string} [popReceipt] The pop receipt.
|
||||
*/
|
||||
function QueueMessageResult(queue, messageId, popReceipt) {
|
||||
if (queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
if (messageId) {
|
||||
this.messageId = messageId;
|
||||
}
|
||||
|
||||
if (popReceipt) {
|
||||
this.popReceipt = popReceipt;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an XML representation for a queue message
|
||||
*
|
||||
* @param {string} messageJs The queue message.
|
||||
* @param {QueueMessageEncoder} The message encoder.
|
||||
* @return {string} The XML queue message.
|
||||
*/
|
||||
QueueMessageResult.serialize = function (messageJs, encoder) {
|
||||
var doc = xmlbuilder.create();
|
||||
doc = doc.begin(Constants.QueueConstants.QUEUE_MESSAGE_ELEMENT, { version: '1.0', encoding: 'utf-8' });
|
||||
|
||||
if (messageJs) {
|
||||
var message;
|
||||
if (encoder !== null && encoder !== undefined) {
|
||||
message = encoder.encode(messageJs);
|
||||
} else {
|
||||
message = messageJs;
|
||||
}
|
||||
|
||||
doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT)
|
||||
.txt(message)
|
||||
.up();
|
||||
} else {
|
||||
doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT).up();
|
||||
}
|
||||
|
||||
return doc.doc().toString();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Pase the XML representation of a queue message to a QueueMessageResult object.
|
||||
*
|
||||
* @param {Object} messageXml The XML representation of the queue message.
|
||||
* @param {QueueMessageEncoder} The message encoder.
|
||||
* @return {QueueMessageResult} The QueueMessageResult object.
|
||||
*/
|
||||
QueueMessageResult.parse = function (messageXml, encoder) {
|
||||
var queueMessageResult = new QueueMessageResult();
|
||||
for (var property in messageXml) {
|
||||
if (property === Constants.QueueConstants.MESSAGE_TEXT_ELEMENT) {
|
||||
if (encoder !== null && encoder !== undefined) {
|
||||
queueMessageResult.messageText = encoder.decode(messageXml[property]);
|
||||
} else {
|
||||
queueMessageResult.messageText = messageXml[property];
|
||||
}
|
||||
} else {
|
||||
var resultPropertyName = azureutil.normalizePropertyNameFromXML(property);
|
||||
queueMessageResult[resultPropertyName] = messageXml[property];
|
||||
}
|
||||
}
|
||||
|
||||
// Convert dequeueCount to number
|
||||
if (queueMessageResult.dequeueCount) {
|
||||
queueMessageResult.dequeueCount = parseInt(queueMessageResult.dequeueCount);
|
||||
}
|
||||
|
||||
return queueMessageResult;
|
||||
};
|
||||
|
||||
QueueMessageResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setmessagePropertyFromHeaders = function (messageProperty, headerProperty) {
|
||||
if (!self[messageProperty] && headers[headerProperty.toLowerCase()]) {
|
||||
self[messageProperty] = headers[headerProperty.toLowerCase()];
|
||||
}
|
||||
};
|
||||
|
||||
setmessagePropertyFromHeaders('popReceipt', HeaderConstants.POP_RECEIPT);
|
||||
setmessagePropertyFromHeaders('timeNextVisible', HeaderConstants.TIME_NEXT_VISIBLE);
|
||||
};
|
||||
|
||||
module.exports = QueueMessageResult;
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var Constants = require('./../../../common/common.core').Constants;
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
|
||||
/**
|
||||
* Creates a new QueueResult object.
|
||||
* @class
|
||||
* The QueueResult class is used to store the queue information.
|
||||
*
|
||||
* @property {string} name The queue name.
|
||||
* @property {object} metadata The metadata key/value pair.
|
||||
* @property {number} approximateMessageCount The approximate number of messages in the queue. This number is not lower than the actual number of messages in the queue, but could be higher.
|
||||
* @property {Object.<string, AccessPolicy>} signedIdentifiers The container ACL settings. See `[AccessPolicy]{@link AccessPolicy}` for detailed information.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} [name] The queue name.
|
||||
* @param {string} [metadata] The metadata key/value pair.
|
||||
*/
|
||||
function QueueResult(name, metadata) {
|
||||
if (name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
if (metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
}
|
||||
|
||||
QueueResult.parse = function (messageXml) {
|
||||
var queueResult = new QueueResult();
|
||||
for (var property in messageXml) {
|
||||
if (messageXml.hasOwnProperty(property)) {
|
||||
queueResult[property.toLowerCase()] = messageXml[property];
|
||||
}
|
||||
}
|
||||
|
||||
return queueResult;
|
||||
};
|
||||
|
||||
QueueResult.prototype.getPropertiesFromHeaders = function (headers) {
|
||||
var self = this;
|
||||
|
||||
var setPropertyFromHeaders = function (queueProperty, headerProperty, typeConverterFunc) {
|
||||
if (!self[queueProperty] && headers[headerProperty.toLowerCase()]) {
|
||||
if(typeConverterFunc) {
|
||||
self[queueProperty] = typeConverterFunc(headers[headerProperty.toLowerCase()]);
|
||||
} else{
|
||||
self[queueProperty] = headers[headerProperty.toLowerCase()];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
setPropertyFromHeaders('approximateMessageCount', HeaderConstants.APPROXIMATE_MESSAGES_COUNT, parseInt);
|
||||
};
|
||||
|
||||
module.exports = QueueResult;
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
|
||||
var util = require('util');
|
||||
|
||||
/**
|
||||
* The interface for classes that represent a encoder which can be used to specify how the queue service encodes and decodes queue messages.
|
||||
*
|
||||
* To specify how the queue service encodes and decodes queue messages, set `queueService.messageEncoder` to object of built-in encoder types
|
||||
* `[TextBase64QueueMessageEncoder]{@link TextBase64QueueMessageEncoder}`, `[BinaryBase64QueueMessageEncoder]{@link BinaryBase64QueueMessageEncoder}`, `[TextXmlQueueMessageEncoder]{@link TextXmlQueueMessageEncoder}`,
|
||||
* or custom implementation of the QueueMessageEncoder.
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
function QueueMessageEncoder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to encode queue messages.
|
||||
*
|
||||
* @param {object} [input] The target to be encoded.
|
||||
* @return {string}
|
||||
*/
|
||||
QueueMessageEncoder.prototype.encode = function(input){
|
||||
return input;
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to decode queue messages
|
||||
*
|
||||
* @param {string} [textToBeDecoded] The base64 string to be decoded.
|
||||
* @returns {any}
|
||||
*/
|
||||
QueueMessageEncoder.prototype.decode = function(textToBeDecoded){
|
||||
return textToBeDecoded;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new TextBase64QueueMessageEncoder object
|
||||
* @class
|
||||
*
|
||||
* Encode from utf-8 string to base64 string
|
||||
* Decode from base64 string to utf-8 string.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {QueueMessageEncoder}
|
||||
*/
|
||||
function TextBase64QueueMessageEncoder(){
|
||||
}
|
||||
util.inherits(TextBase64QueueMessageEncoder, QueueMessageEncoder);
|
||||
|
||||
/**
|
||||
* Encode from utf-8 string to base64 string
|
||||
* @this TextBase64QueueMessageEncoder
|
||||
*
|
||||
* @param {string} [input] The target to be encoded.
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
TextBase64QueueMessageEncoder.prototype.encode = function(input){
|
||||
return new Buffer(input, 'utf8').toString('base64');
|
||||
};
|
||||
|
||||
/**
|
||||
* Decode from base64 string to utf-8 string.
|
||||
* @this TextBase64QueueMessageEncoder
|
||||
*
|
||||
* @param {string} [textToBeDecoded] The base64 string to be decoded.
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
TextBase64QueueMessageEncoder.prototype.decode = function(textToDecode){
|
||||
return new Buffer(textToDecode, 'base64').toString('utf8');
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new BinaryBase64QueueMessageEncoder object
|
||||
* @class
|
||||
*
|
||||
* Encode from binary buffer to base64 string
|
||||
* Decode from base64 string to binary buffer.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {QueueMessageEncoder}
|
||||
*/
|
||||
function BinaryBase64QueueMessageEncoder(){
|
||||
}
|
||||
util.inherits(BinaryBase64QueueMessageEncoder, QueueMessageEncoder);
|
||||
|
||||
/**
|
||||
* Encode from binary buffer string to base64 string
|
||||
* @this BinaryBase64QueueMessageEncoder
|
||||
*
|
||||
* @param {Buffer} [input] The target to be encoded.
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
BinaryBase64QueueMessageEncoder.prototype.encode = function(input){
|
||||
return input.toString('base64');
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Decode from base64 string to binary buffer.
|
||||
* @this BinaryBase64QueueMessageEncoder
|
||||
*
|
||||
* @param {string} [textToBeDecoded] The base64 string to be decoded.
|
||||
*
|
||||
* @return {Buffer}
|
||||
*/
|
||||
BinaryBase64QueueMessageEncoder.prototype.decode = function(textToDecode){
|
||||
return new Buffer(textToDecode, 'base64');
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new TextXmlQueueMessageEncoder object
|
||||
* @class
|
||||
*
|
||||
* Encode utf-8 string by escaping the xml markup characters.
|
||||
* Decode from utf-8 string by unescaping the xml markup characters.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {QueueMessageEncoder}
|
||||
*/
|
||||
function TextXmlQueueMessageEncoder(){
|
||||
}
|
||||
util.inherits(TextXmlQueueMessageEncoder, QueueMessageEncoder);
|
||||
|
||||
/**
|
||||
* Encode utf-8 string by escaping the xml markup characters.
|
||||
* @this TextXmlQueueMessageEncoder
|
||||
*
|
||||
* @param {string} [input] The target to be encoded.
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
TextXmlQueueMessageEncoder.prototype.encode = function(input){
|
||||
return input.replace(/&/gm, '&')
|
||||
.replace(/</gm, '<')
|
||||
.replace(/>/gm, '>')
|
||||
.replace(/"/gm, '"')
|
||||
.replace(/'/gm, ''');
|
||||
};
|
||||
|
||||
/**
|
||||
* Decode from utf-8 string by unescaping the xml markup characters.
|
||||
* @this TextXmlQueueMessageEncoder
|
||||
*
|
||||
* @param {string} [textToBeDecoded] The base64 string to be decoded.
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
TextXmlQueueMessageEncoder.prototype.decode = function(textToDecode){
|
||||
return textToDecode.replace(/&/gm, '&')
|
||||
.replace(/</gm, '<')
|
||||
.replace(/>/gm, '>')
|
||||
.replace(/"/gm, '"')
|
||||
.replace(/'/gm, '\'');
|
||||
};
|
||||
|
||||
module.exports = QueueMessageEncoder;
|
||||
module.exports.TextBase64QueueMessageEncoder = TextBase64QueueMessageEncoder;
|
||||
module.exports.BinaryBase64QueueMessageEncoder = BinaryBase64QueueMessageEncoder;
|
||||
module.exports.TextXmlQueueMessageEncoder = TextXmlQueueMessageEncoder;
|
||||
+1468
File diff suppressed because it is too large.
Load diff
+39
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Expose 'QueueUtilities'.
|
||||
exports = module.exports;
|
||||
|
||||
/**
|
||||
* Defines enums for use with the Queue service.
|
||||
* @namespace QueueUtilities
|
||||
*/
|
||||
var QueueUtilities = {
|
||||
/**
|
||||
* Permission types.
|
||||
*
|
||||
* @const
|
||||
* @enum {string}
|
||||
*/
|
||||
SharedAccessPermissions: {
|
||||
READ: 'r',
|
||||
ADD: 'a',
|
||||
UPDATE: 'u',
|
||||
PROCESS: 'p'
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = QueueUtilities;
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
var _ = require('underscore');
|
||||
var util = require('util');
|
||||
var guid = require('uuid');
|
||||
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var SR = azureCommon.SR;
|
||||
|
||||
var TableUtilities = require('../tableutilities');
|
||||
var EdmType = TableUtilities.EdmType;
|
||||
|
||||
/**
|
||||
* Get the Edm type of an object.
|
||||
*
|
||||
* @param {object} value A typed instance.
|
||||
* @return {string} The Edm type.
|
||||
*/
|
||||
exports.propertyType = function (value, guessNumberType) {
|
||||
if (_.isNumber(value)) {
|
||||
if (guessNumberType) {
|
||||
if (azureutil.objectIsInt(value)) {
|
||||
return 'Edm.Int32';
|
||||
} else {
|
||||
return 'Edm.Double';
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (_.isBoolean(value)) {
|
||||
return 'Edm.Boolean';
|
||||
} else if (_.isDate(value)) {
|
||||
return 'Edm.DateTime';
|
||||
} else {
|
||||
return 'Edm.String';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a JSON value from over the wire into the correct EDM type.
|
||||
*
|
||||
* Note that Int64, is remaining a string. Converting it to a Number would lose precision.
|
||||
* Int32, Boolean, and Double should already be the correct non-string types
|
||||
*
|
||||
* @param {string} type The type of the value as it appears in the type attribute.
|
||||
* @param value The value in JSON format.
|
||||
* @return {object} The unserialized value.
|
||||
*/
|
||||
exports.deserializeValueFromJson = function (type, value) {
|
||||
if (type) {
|
||||
switch (type) {
|
||||
case EdmType.BINARY:
|
||||
return new Buffer(value, 'base64');
|
||||
case EdmType.DATETIME:
|
||||
return new Date(value);
|
||||
case EdmType.GUID:
|
||||
return value;
|
||||
case EdmType.DOUBLE:
|
||||
// Account for Infinity and NaN:
|
||||
if (typeof value !== 'number') {
|
||||
return parseFloat(value);
|
||||
}
|
||||
return value;
|
||||
case EdmType.INT32:
|
||||
case EdmType.INT64:
|
||||
case EdmType.STRING:
|
||||
case EdmType.BOOLEAN:
|
||||
return value;
|
||||
default:
|
||||
throw new Error(util.format(SR.TYPE_NOT_SUPPORTED, type));
|
||||
}
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a raw EdmType value into the JSON value expected to be sent over the wire.
|
||||
*
|
||||
* TODO: validate correct input types?
|
||||
* Expects Edm.Int64 and Edm.String to be string, Edm.Double and Edm.Int32 to be Number,
|
||||
* Edm.Guid to be an array or buffer compatible with Node.uuid, Edm.Binary to be a Node Buffer, Edm.DateTime to be a Date,
|
||||
* and Edm.Boolean to be a boolean.
|
||||
*
|
||||
* @param {string} type The type of the value as it will appear in the type attribute.
|
||||
* @param {string} value The value
|
||||
* @return {object} The serialized value.
|
||||
*/
|
||||
exports.serializeValue = function (type, value) {
|
||||
switch (type) {
|
||||
case EdmType.BINARY:
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return value.toString('base64');
|
||||
}
|
||||
return value;
|
||||
case EdmType.DATETIME:
|
||||
if (_.isDate(value)) {
|
||||
return value.toISOString();
|
||||
}
|
||||
return value;
|
||||
case EdmType.GUID:
|
||||
if (Buffer.isBuffer(value) || _.isArray(value)) {
|
||||
return guid.unparse(value);
|
||||
}
|
||||
return value;
|
||||
case EdmType.INT64:
|
||||
case EdmType.DOUBLE:
|
||||
return value.toString();
|
||||
case EdmType.INT32:
|
||||
if (value === Number.POSITIVE_INFINITY) {
|
||||
return 'Infinity';
|
||||
}
|
||||
if (value === Number.NEGATIVE_INFINITY) {
|
||||
return '-Infinity';
|
||||
}
|
||||
if (azureutil.objectIsNaN(value)) {
|
||||
return 'NaN';
|
||||
}
|
||||
return value;
|
||||
case EdmType.STRING:
|
||||
case EdmType.BOOLEAN:
|
||||
return value;
|
||||
default:
|
||||
throw new Error(SR.TYPE_NOT_SUPPORTED + type);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Determines if a type annotation is required for the input type when sending JSON data to the service.
|
||||
*/
|
||||
exports.isTypeRequired = function(type, value) {
|
||||
switch (type) {
|
||||
case EdmType.BINARY:
|
||||
case EdmType.INT64:
|
||||
case EdmType.DATETIME:
|
||||
case EdmType.GUID:
|
||||
case EdmType.DOUBLE:
|
||||
return true;
|
||||
case EdmType.INT32:
|
||||
if (typeof value !== 'number' || value === Number.POSITIVE_INFINITY || value === Number.NEGATIVE_INFINITY || (azureutil.objectIsNaN(value))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case EdmType.STRING:
|
||||
case EdmType.BOOLEAN:
|
||||
return false;
|
||||
default:
|
||||
throw new Error(util.format(SR.TYPE_NOT_SUPPORTED, type));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Serializes value into proper value to be used in odata query value.
|
||||
*
|
||||
* @param {object} value The value to be serialized.
|
||||
* @return {string} The serialized value.
|
||||
*/
|
||||
exports.serializeQueryValue = function (value, type) {
|
||||
var edmType = type || exports.propertyType(value, true);
|
||||
switch (edmType) {
|
||||
case EdmType.INT32:
|
||||
return value.toString();
|
||||
case EdmType.BOOLEAN:
|
||||
return value ? 'true' : 'false';
|
||||
case EdmType.DOUBLE:
|
||||
return value.toString();
|
||||
case EdmType.INT64:
|
||||
return value.toString() + 'L';
|
||||
case EdmType.DATETIME:
|
||||
if(_.isDate(value)) {
|
||||
var dateTimeString = value.toISOString();
|
||||
return 'datetime\'' + dateTimeString + '\'';
|
||||
}
|
||||
throw new Error(util.format(SR.INVALID_EDM_TYPE, value, type));
|
||||
case EdmType.GUID:
|
||||
return 'guid\'' + value.toString() + '\'';
|
||||
case EdmType.BINARY:
|
||||
return 'X\'' + value.toString('hex') + '\'';
|
||||
default:
|
||||
return '\'' + value.toString().replace(/'/g, '\'\'') + '\'';
|
||||
}
|
||||
};
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Module dependencies.
|
||||
var util = require('util');
|
||||
|
||||
var azureCommon = require('./../../../common/common.core');
|
||||
var azureutil = azureCommon.util;
|
||||
var SR = azureCommon.SR;
|
||||
var Constants = azureCommon.Constants;
|
||||
var edmHandler = require('./edmhandler');
|
||||
|
||||
var prefixLength = Constants.TableConstants.ODATA_PREFIX.length;
|
||||
var suffixLength = Constants.TableConstants.ODATA_TYPE_SUFFIX.length;
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
/* Serialize an entity to an Odata (Json based) payload
|
||||
* Input must be in the following format:
|
||||
* { stringValue: { '$': 'Edm.String', '_': 'my string' }, myInt: { '$': 'Edm.Int32', '_': 3 } }
|
||||
*/
|
||||
exports.serializeJson = function (entity) {
|
||||
function normalizeEntityProperty(property) {
|
||||
if(azureutil.objectIsNull(property)) {
|
||||
return { _: property };
|
||||
}
|
||||
|
||||
if (typeof property === 'object' && property.hasOwnProperty(Constants.TableConstants.ODATA_VALUE_MARKER)) {
|
||||
return property;
|
||||
}
|
||||
|
||||
var result = { _: property };
|
||||
result[Constants.TableConstants.ODATA_TYPE_MARKER] = edmHandler.propertyType(property, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
var result = {};
|
||||
for (var propName in entity) {
|
||||
// ignore if .metadata or null or undefined
|
||||
if (propName !== Constants.TableConstants.ODATA_METADATA_MARKER) {
|
||||
var property = normalizeEntityProperty(entity[propName]);
|
||||
if (!azureutil.objectIsNull(property[Constants.TableConstants.ODATA_VALUE_MARKER])) {
|
||||
var value = property[Constants.TableConstants.ODATA_VALUE_MARKER];
|
||||
var type = property[Constants.TableConstants.ODATA_TYPE_MARKER];
|
||||
|
||||
if (type === undefined) {
|
||||
type = edmHandler.propertyType(value, true);
|
||||
}
|
||||
|
||||
result[propName] = edmHandler.serializeValue(type, value);
|
||||
if (edmHandler.isTypeRequired(type, value)) {
|
||||
result[propName + Constants.TableConstants.ODATA_TYPE_SUFFIX] = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var replacer = function(key, value) {
|
||||
if (value === Number.POSITIVE_INFINITY) {
|
||||
return 'Infinity';
|
||||
}
|
||||
if (value === Number.NEGATIVE_INFINITY) {
|
||||
return '-Infinity';
|
||||
}
|
||||
if (azureutil.objectIsNaN(value)) {
|
||||
return 'NaN';
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
return JSON.stringify(result, replacer);
|
||||
};
|
||||
|
||||
/*
|
||||
Input: The body of the HTTP response from the server from a table list as JSON (responseObject.response.body).
|
||||
|
||||
Return:
|
||||
This will return an array in the following format:
|
||||
|
||||
[
|
||||
tableName1,
|
||||
tableName2
|
||||
]
|
||||
|
||||
For example,
|
||||
|
||||
[
|
||||
myTable1,
|
||||
myTable2
|
||||
]
|
||||
|
||||
*/
|
||||
exports.parseJsonTables = function (response) {
|
||||
var result = [];
|
||||
|
||||
if (response.value) {
|
||||
for (var i = 0; i < response.value.length; i++) {
|
||||
var entity = response.value[i].TableName;
|
||||
result.push(entity);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/*
|
||||
Input: The body of the HTTP response from the server from a table query as JSON (responseObject.response.body).
|
||||
|
||||
Return:
|
||||
This will return an array in the following format:
|
||||
|
||||
[
|
||||
{{ '$': edmHandler1, '_': value1}, { '$': edmHandler2, '_': value2}, { '$': edmHandler3, '_': value3}},
|
||||
{{ '$': edmHandler4, '_': value4}, { '$': edmHandler5, '_': value5}, { '$': edmHandler6, '_': value6}}
|
||||
]
|
||||
|
||||
For example,
|
||||
|
||||
[
|
||||
{{ '$': Edm.Int32, '_': 42}, { '$': Edm.String, '_': 'sample string'}, { '$': Edm.Boolean, '_': false}},
|
||||
{{ '$': Edm.Int64, '_': 42}, { '$': Edm.String, '_': 'sample string 2'}, { '$': Edm.Boolean, '_': true}}
|
||||
]
|
||||
|
||||
*/
|
||||
exports.parseJsonEntities = function (response, autoResolveProperties, propertyResolver, entityResolver) {
|
||||
if (!response.value) {
|
||||
return [exports.parseJsonSingleEntity(response, autoResolveProperties, propertyResolver, entityResolver)];
|
||||
} else {
|
||||
var result = [];
|
||||
|
||||
for (var i = 0; i < response.value.length; i++) {
|
||||
var rawEntity = response.value[i];
|
||||
var entity = exports.parseJsonSingleEntity(rawEntity, autoResolveProperties, propertyResolver, entityResolver);
|
||||
result.push(entity);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
exports.parseJsonSingleEntity = function(rawEntity, autoResolveProperties, propertyResolver, entityResolver) {
|
||||
var rawEntityProperties = {};
|
||||
var entityPropertyTypes = {PartitionKey: 'Edm.String', RowKey: 'Edm.String', Timestamp: 'Edm.DateTime'};
|
||||
var odataMetadata = {};
|
||||
|
||||
// parse properties
|
||||
for (var entityPropertyName in rawEntity) {
|
||||
if (azureutil.stringStartsWith(entityPropertyName, Constants.TableConstants.ODATA_PREFIX)) {
|
||||
odataMetadata[entityPropertyName.slice(prefixLength)] = rawEntity[entityPropertyName];
|
||||
} else if (azureutil.stringEndsWith(entityPropertyName, Constants.TableConstants.ODATA_TYPE_SUFFIX)) {
|
||||
entityPropertyTypes[entityPropertyName.slice(0, entityPropertyName.length - suffixLength)] = rawEntity[entityPropertyName];
|
||||
} else {
|
||||
rawEntityProperties[entityPropertyName] = rawEntity[entityPropertyName];
|
||||
}
|
||||
}
|
||||
|
||||
// make sure etag is set
|
||||
if (!odataMetadata.etag && rawEntityProperties.Timestamp) {
|
||||
var timestampString = new Buffer(rawEntityProperties.Timestamp).toString();
|
||||
odataMetadata.etag = 'W/"datetime\'' + timestampString + '\'"';
|
||||
}
|
||||
|
||||
var entity = {};
|
||||
for (var entityPropertyName in rawEntityProperties) {
|
||||
if (rawEntityProperties.hasOwnProperty(entityPropertyName)) {
|
||||
// set the type, if given in the response
|
||||
var entityPropertyType = entityPropertyTypes[entityPropertyName];
|
||||
entity[entityPropertyName] = {};
|
||||
|
||||
// use the given property resolver if present, otherwise infer type if undefined
|
||||
if (propertyResolver) {
|
||||
// partition key, row key, name, value, type if present
|
||||
entityPropertyType = propertyResolver(rawEntityProperties.PartitionKey, rawEntityProperties.RowKey, entityPropertyName, rawEntityProperties[entityPropertyName], entityPropertyType);
|
||||
}
|
||||
if (!entityPropertyType && autoResolveProperties) {
|
||||
entityPropertyType = edmHandler.propertyType(rawEntityProperties[entityPropertyName], false);
|
||||
}
|
||||
|
||||
if (entityPropertyType) {
|
||||
entity[entityPropertyName][Constants.TableConstants.ODATA_TYPE_MARKER] = entityPropertyType;
|
||||
}
|
||||
|
||||
try {
|
||||
entity[entityPropertyName][Constants.TableConstants.ODATA_VALUE_MARKER] = edmHandler.deserializeValueFromJson(entityPropertyType, rawEntityProperties[entityPropertyName]);
|
||||
} catch (err) {
|
||||
if (propertyResolver) {
|
||||
// if a property resolver was used and the type is invalid, throw an appropriate error
|
||||
throw new Error(util.format(SR.INVALID_PROPERTY_RESOLVER, entityPropertyName, entityPropertyType, rawEntityProperties[entityPropertyName]));
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entity[Constants.TableConstants.ODATA_METADATA_MARKER] = odataMetadata;
|
||||
|
||||
if (entityResolver) {
|
||||
entity = entityResolver(entity);
|
||||
}
|
||||
|
||||
return entity;
|
||||
};
|
||||
Loaded 100 of 1288 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user