Permalink
Browse files

499: Make generating blob URL with SAS easier

  • Loading branch information...
1 parent aeeae1b commit 9f56b087e3e1ed8c793b8d3733b32f844971047f Andre Rodrigues committed Jan 29, 2013
@@ -31,7 +31,7 @@ EventService.prototype = {
res.render('detail', {
title: eventItem.name,
eventItem: eventItem,
- imageUrl: self.blobClient.getBlobUrl('photos', eventItem.RowKey).url(),
+ imageUrl: self.blobClient.getBlobUrl('photos', eventItem.RowKey),
});
}
});
@@ -146,7 +146,7 @@ exports.createOrEditRole = function (roleData, roleImage, callback) {
function insertOrUpdateEntity(error, role) {
if (role) {
- roleData.imageUrl = blobClient.getBlobUrl(role.container, role.blob).url();
+ roleData.imageUrl = blobClient.getBlobUrl(role.container, role.blob);
}
if (!_.isUndefined(roleData.RowKey) && !_.isUndefined(roleData.PartitionKey)) {
@@ -2142,7 +2142,7 @@ BlobService.prototype.listBlobBlocks = function (container, blob, blocklisttype,
* @param {string} [blob] The blob name.
* @param {object} sharedAccessPolicy The shared access policy.
* @param {string} [sharedAccessPolicy.Id] The signed identifier.
-* @param {SharedAccessPermissions} sharedAccessPolicy.AccessPolicy.Permissions The permission type.
+* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type.
* @param {date|string} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid (The UTC value will be used).
* @param {date|string} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired (The UTC value will be used).
* @return {object} An object with the shared access signature.
@@ -2156,6 +2156,10 @@ BlobService.prototype.generateSharedAccessSignature = function (container, blob,
resourceType = BlobConstants.ResourceTypes.BLOB;
}
+ if (azureutil.objectIsNull(sharedAccessPolicy.AccessPolicy.Permissions)) {
+ sharedAccessPolicy.AccessPolicy.Permissions = BlobConstants.SharedAccessPermissions.READ;
+ }
+
if (!azureutil.objectIsNull(sharedAccessPolicy.AccessPolicy.Start)) {
sharedAccessPolicy.AccessPolicy.Start = ISO8061Date.format(sharedAccessPolicy.AccessPolicy.Start, true);
}
@@ -2186,24 +2190,29 @@ BlobService.prototype.generateSharedAccessSignature = function (container, blob,
* @this {BlobService}
* @param {string} container The container name.
* @param {string} [blob] The blob name.
+* @param {object} [sharedAccessPolicy] The shared access policy.
+* @param {string} [sharedAccessPolicy.Id] The signed identifier.
+* @param {SharedAccessPermissions} [sharedAccessPolicy.AccessPolicy.Permissions] The permission type.
+* @param {date|string} [sharedAccessPolicy.AccessPolicy.Start] The time at which the Shared Access Signature becomes valid (The UTC value will be used).
+* @param {date|string} sharedAccessPolicy.AccessPolicy.Expiry The time at which the Shared Access Signature becomes expired (The UTC value will be used).
* @return {object} An object with the blob URL.
*/
-BlobService.prototype.getBlobUrl = function (container, blob) {
+BlobService.prototype.getBlobUrl = function (container, blob, sharedAccessPolicy) {
// Validate container name. Blob name is optional.
validateContainerName(container);
- var resourceName = createResourceName(container, blob);
-
- var baseUrl = this.protocol + this.host + ':' + this.port;
- var path = this._getPath('/' + resourceName);
+ var signedQueryString = {};
+ if (sharedAccessPolicy) {
+ signedQueryString = this.generateSharedAccessSignature(container, blob, sharedAccessPolicy).queryString;
+ }
- return {
- baseUrl: baseUrl,
- path: path,
- url: function () {
- return baseUrl + path;
- }
- };
+ return url.format({
+ protocol: this._isHttps() ? 'https:' : 'http:',
+ hostname: this.host,
+ port: this.port,
+ pathname: this._getPath('/' + createResourceName(container, blob)),
+ query: signedQueryString
+ });
};
// Private methods
@@ -1250,11 +1250,29 @@ suite('blobservice-tests', function () {
var blobServiceassert = azure.createBlobService('storageAccount', 'storageAccessKey', 'host.com:80');
- var urlParts = blobServiceassert.getBlobUrl(containerName);
- assert.equal(urlParts.url(), 'http://host.com:80/' + containerName);
+ var blobUrl = blobServiceassert.getBlobUrl(containerName);
+ assert.equal(blobUrl, 'http://host.com:80/' + containerName);
- urlParts = blobServiceassert.getBlobUrl(containerName, blobName);
- assert.equal(urlParts.url(), 'http://host.com:80/' + containerName + '/' + blobName);
+ blobUrl = blobServiceassert.getBlobUrl(containerName, blobName);
+ assert.equal(blobUrl, 'http://host.com:80/' + containerName + '/' + blobName);
+
+ done();
+ });
+
+ test('GetBlobSharedUrl', function (done) {
+ var containerName = 'container';
+ var blobName = 'blob';
+
+ var blobServiceassert = azure.createBlobService('storageAccount', 'storageAccessKey', 'host.com:80');
+
+ var sharedAccessPolicy = {
+ AccessPolicy: {
+ Expiry: new Date('October 12, 2011 11:53:40 am GMT')
+ }
+ };
+
+ var blobUrl = blobServiceassert.getBlobUrl(containerName, blobName, sharedAccessPolicy);
+ assert.equal(blobUrl, 'http://host.com:80/' + containerName + '/' + blobName + '?se=2011-10-12T11%3A53%3A40Z&sr=b&sp=r&sig=eVkH%2BFxxShel2hcN50ZUmgPAHk%2FmqRVeaBfyry%2BVacw%3D');
done();
});

0 comments on commit 9f56b08

Please sign in to comment.