diff --git a/lib/services/blob/blobservice.js b/lib/services/blob/blobservice.js index e2303c395c..fbc87ac88a 100644 --- a/lib/services/blob/blobservice.js +++ b/lib/services/blob/blobservice.js @@ -2189,6 +2189,32 @@ BlobService.prototype.generateSharedAccessSignature = function (container, blob, }; }; +/** +* Retrieves a blob URL. +* +* @this {BlobService} +* @param {string} container The container name. +* @param {string} [blob] The blob name. +* @return {string} +*/ +BlobService.prototype.getBlobUrl = function (container, blob) { + // Validate container name. Blob name is optional. + validateContainerName(container); + + var resourceName = createResourceName(container, blob); + + var baseUrl = this.protocol + this.getHostname() + ':' + this.port; + var path = this.getPath('/' + resourceName); + + return { + baseUrl: baseUrl, + path: path, + url: function () { + return baseUrl + path; + } + }; +}; + // Private methods /** diff --git a/test/services/blob/blobservice-tests.js b/test/services/blob/blobservice-tests.js index ff8e6c1ba0..3f212575eb 100644 --- a/test/services/blob/blobservice-tests.js +++ b/test/services/blob/blobservice-tests.js @@ -1210,6 +1210,29 @@ module.exports = testCase( }); }); }); + }, + + testGetBlobUrl: function (test) { + var containerName = testutil.generateId(containerNamesPrefix, containerNames); + var blobName = testutil.generateId(blobNamesPrefix, blobNames); + + var blobServiceTest = azure.createBlobService('storageAccount', 'storageAccessKey', 'host:80'); + blobServiceTest.usePathStyleUri = false; + + var urlParts = blobServiceTest.getBlobUrl(containerName); + test.equal(urlParts.url(), 'http://storageAccount.host:80/' + containerName); + + urlParts = blobServiceTest.getBlobUrl(containerName, blobName); + test.equal(urlParts.url(), 'http://storageAccount.host:80/' + containerName + '/' + blobName); + + blobServiceTest.usePathStyleUri = true; + urlParts = blobServiceTest.getBlobUrl(containerName); + test.equal(urlParts.url(), 'http://host:80/storageAccount/' + containerName); + + urlParts = blobServiceTest.getBlobUrl(containerName, blobName); + test.equal(urlParts.url(), 'http://host:80/storageAccount/' + containerName + '/' + blobName); + + test.done(); } });