This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
storageserviceclient.js
252 lines (211 loc) · 9.99 KB
/
storageserviceclient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* Copyright (c) Microsoft. 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 http = require('http');
var url = require('url');
var util = require('util');
var qs = require('qs');
var crypto = require('crypto');
var xml2js = require('xml2js');
var azureutil = require('../../util/util');
var StorageServiceSettings = require('../core/storageservicesettings');
var ServiceClient = require('./serviceclient');
var Constants = require('../../util/constants');
var HeaderConstants = Constants.HeaderConstants;
var QueryStringConstants = Constants.QueryStringConstants;
var HttpConstants = Constants.HttpConstants;
var Logger = require('../../diagnostics/logger');
// Expose 'StorageServiceClient'.
exports = module.exports = StorageServiceClient;
// Validation error messages
StorageServiceClient.incorrectStorageAccountErr = 'You must supply an account name or use the environment variable AZURE_STORAGE_ACCOUNT if you are not running in the emulator.';
StorageServiceClient.incorrectStorageAccessKeyErr = 'You must supply an account key or use the environment variable AZURE_STORAGE_ACCESS_KEY if you are not running in the emulator.';
/**
* Creates a new ServiceClient object.
*
* @constructor
* @param {string} storageAccount The storage account.
* @param {string} storageAccessKey The storage access key.
* @param {string} host The host for the service.
* @param {bool} usePathStyleUri Boolean value indicating wether to use path style uris.
* @param {object} authenticationProvider The authentication provider object (e.g. sharedkey / sharedkeytable / sharedaccesssignature).
*/
function StorageServiceClient(storageAccount, storageAccessKey, host, usePathStyleUri, authenticationProvider) {
this._setAccountCredentials(storageAccount, storageAccessKey);
this.apiVersion = HeaderConstants.TARGET_STORAGE_VERSION;
this.usePathStyleUri = usePathStyleUri;
StorageServiceClient.super_.call(this, host, authenticationProvider);
this._initDefaultFilter();
}
util.inherits(StorageServiceClient, ServiceClient);
/**
* Gets the storage settings.
*
* @param {string} [storageAccountOrConnectionString] The storage account or the connection string.
* @param {string} [storageAccessKey] The storage access key.
* @param {string} [host] The host address.
*
* @return {StorageServiceSettings}
*/
StorageServiceClient.getStorageSettings = function (storageAccountOrConnectionString, storageAccessKey, host) {
var storageServiceSettings;
var usePathStyleUri = false;
if (storageAccountOrConnectionString && !storageAccessKey) {
// If storageAccountOrConnectionString was passed and no accessKey was passed, assume connection string
storageServiceSettings = StorageServiceSettings.createFromConnectionString(storageAccountOrConnectionString);
} else {
if (!(storageAccountOrConnectionString && storageAccessKey) && ServiceClient.isEmulated()) {
// Dev storage scenario
usePathStyleUri = true;
storageServiceSettings = StorageServiceSettings.developmentStorageAccount();
} else {
// Explicit credentials scenario
if (!storageAccountOrConnectionString) {
storageAccountOrConnectionString = process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCOUNT];
}
if (!storageAccessKey) {
storageAccessKey = process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCESS_KEY];
}
// Default endpoints
var blobendpoint = url.format({ protocol: 'http:', host: storageAccountOrConnectionString + '.' + ServiceClient.CLOUD_BLOB_HOST });
var tableendpoint = url.format({ protocol: 'http:', host: storageAccountOrConnectionString + '.' + ServiceClient.CLOUD_TABLE_HOST });
var queueendpoint = url.format({ protocol: 'http:', host: storageAccountOrConnectionString + '.' + ServiceClient.CLOUD_QUEUE_HOST });
if (host) {
var parsedHost = ServiceClient._parseHost(host);
blobendpoint = url.format({ protocol: parsedHost.protocol, port: parsedHost.port, hostname: storageAccountOrConnectionString + '.' + parsedHost.hostname });
tableendpoint = url.format({ protocol: parsedHost.protocol, port: parsedHost.port, hostname: storageAccountOrConnectionString + '.' + parsedHost.hostname });
queueendpoint = url.format({ protocol: parsedHost.protocol, port: parsedHost.port, hostname: storageAccountOrConnectionString + '.' + parsedHost.hostname });
}
var settings = {
accountname: storageAccountOrConnectionString,
accountkey: storageAccessKey,
blobendpoint: blobendpoint,
tableendpoint: tableendpoint,
queueendpoint: queueendpoint
};
storageServiceSettings = StorageServiceSettings.createFromSettings(settings);
}
}
storageServiceSettings._usePathStyleUri = usePathStyleUri;
return storageServiceSettings;
};
/**
* Builds the request options to be passed to the http.request method.
*
* @param {WebResource} webResource The webresource where to build the options from.
* @param {object} options The request options.
* @param {function(error, requestOptions)} callback The callback function.
* @return {undefined}
*/
StorageServiceClient.prototype._buildRequestOptions = function (webResource, options, callback) {
var self = this;
if (!webResource.headers || !webResource.headers[HeaderConstants.CONTENT_TYPE]) {
webResource.addOptionalHeader(HeaderConstants.CONTENT_TYPE, '');
}
if (!webResource.headers || webResource.headers[HeaderConstants.CONTENT_LENGTH] === undefined) {
webResource.addOptionalHeader(HeaderConstants.CONTENT_LENGTH, 0);
} else if(webResource.headers && webResource.headers[HeaderConstants.CONTENT_LENGTH] === null) {
delete webResource.headers[HeaderConstants.CONTENT_LENGTH];
}
webResource.addOptionalHeader(HeaderConstants.STORAGE_VERSION_HEADER, this.apiVersion);
webResource.addOptionalHeader(HeaderConstants.DATE_HEADER, new Date().toUTCString());
webResource.addOptionalHeader(HeaderConstants.ACCEPT_HEADER, 'application/atom+xml,application/xml');
webResource.addOptionalHeader(HeaderConstants.ACCEPT_CHARSET_HEADER, 'UTF-8');
webResource.addOptionalHeader(HeaderConstants.HOST_HEADER, this.host + ':' + this.port);
if (options) {
if (options.timeoutIntervalInMs) {
webResource.addOptionalQueryParam(QueryStringConstants.TIMEOUT, options.timeoutIntervalInMs);
}
if (options.accessConditions) {
webResource.addOptionalAccessConditionHeader(options.accessConditions);
}
if (options.sourceAccessConditions) {
webResource.addOptionalSourceAccessConditionHeader(options.sourceAccessConditions);
}
}
// Sets the request url in the web resource.
this._setRequestUrl(webResource);
// Now that the web request is finalized, sign it
this.authenticationProvider.signRequest(webResource, function (error) {
var requestOptions = null;
if (!error) {
requestOptions = {
url: url.format({
protocol: self._isHttps() ? 'https:' : 'http:',
hostname: self.host,
port: self.port,
pathname: webResource.path + webResource.getQueryString(true)
}),
method: webResource.httpVerb,
headers: webResource.headers
};
self._setRequestOptionsProxy(requestOptions);
}
callback(error, requestOptions);
});
};
/**
* Retrieves the normalized path to be used in a request.
* This takes into consideration the usePathStyleUri object field
* which specifies if the request is against the emulator or against
* the live service. It also adds a leading "/" to the path in case
* it's not there before.
*
* @param {string} path The path to be normalized.
* @return {string} The normalized path.
*/
StorageServiceClient.prototype._getPath = function (path) {
if (path === null || path === undefined) {
path = '/';
} else if (path.indexOf('/') !== 0) {
path = '/' + path;
}
if (this.usePathStyleUri) {
path = '/' + this.storageAccount + path;
}
return path;
};
/**
* Sets the account credentials taking into consideration the isEmulated value and the environment variables:
* AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY.
*
* @param {string} storageAccount The storage account.
* @param {string} storageAccessKey The storage access key.
* @return {undefined}
*/
StorageServiceClient.prototype._setAccountCredentials = function (storageAccount, storageAccessKey) {
if (azureutil.objectIsNull(storageAccount)) {
if (process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCOUNT]) {
storageAccount = process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCOUNT];
} else if (ServiceClient.isEmulated()) {
storageAccount = ServiceClient.DEVSTORE_STORAGE_ACCOUNT;
}
}
if (azureutil.objectIsNull(storageAccessKey)) {
if (process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCESS_KEY]) {
storageAccessKey = process.env[ServiceClient.EnvironmentVariables.AZURE_STORAGE_ACCESS_KEY];
} else if (ServiceClient.isEmulated()) {
storageAccessKey = ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY;
}
}
if (!azureutil.objectIsString(storageAccount) || azureutil.stringIsEmpty(storageAccount)) {
throw new Error(StorageServiceClient.incorrectStorageAccountErr);
}
if (!azureutil.objectIsString(storageAccessKey) || azureutil.stringIsEmpty(storageAccessKey)) {
throw new Error(StorageServiceClient.incorrectStorageAccessKeyErr);
}
this.storageAccount = storageAccount;
this.storageAccessKey = storageAccessKey;
};