From 6885b6c4161b0a4aa1983c8460e43dbb550fcf59 Mon Sep 17 00:00:00 2001 From: zezha-msft Date: Wed, 29 Nov 2017 01:06:02 -0800 Subject: [PATCH] Added convenient method to generate container url --- azure-storage-blob/ChangeLog.md | 1 + .../azure/storage/blob/baseblobservice.py | 27 +++++++++++ tests/blob/test_common_blob.py | 47 +++++++++++++++++-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/azure-storage-blob/ChangeLog.md b/azure-storage-blob/ChangeLog.md index c07f9453..c5ab0416 100644 --- a/azure-storage-blob/ChangeLog.md +++ b/azure-storage-blob/ChangeLog.md @@ -5,6 +5,7 @@ ## Version XX.XX.XX: - Optimized page blob upload for create_blob_from_* methods, by skipping the empty chunks. +- Added convenient method to generate container url (make_container_url). ## Version 0.37.1: diff --git a/azure-storage-blob/azure/storage/blob/baseblobservice.py b/azure-storage-blob/azure/storage/blob/baseblobservice.py index d9790152..2c625bd9 100644 --- a/azure-storage-blob/azure/storage/blob/baseblobservice.py +++ b/azure-storage-blob/azure/storage/blob/baseblobservice.py @@ -258,6 +258,33 @@ def make_blob_url(self, container_name, blob_name, protocol=None, sas_token=None return url + def make_container_url(self, container_name, protocol=None, sas_token=None): + ''' + Creates the url to access a container. + + :param str container_name: + Name of container. + :param str protocol: + Protocol to use: 'http' or 'https'. If not specified, uses the + protocol specified when BaseBlobService was initialized. + :param str sas_token: + Shared access signature token created with + generate_shared_access_signature. + :return: container access URL. + :rtype: str + ''' + + url = '{}://{}/{}?restype=container'.format( + protocol or self.protocol, + self.primary_endpoint, + container_name, + ) + + if sas_token: + url = '{}&{}'.format(url, sas_token) + + return url + def generate_account_shared_access_signature(self, resource_types, permission, expiry, start=None, ip=None, protocol=None): ''' diff --git a/tests/blob/test_common_blob.py b/tests/blob/test_common_blob.py index 88168c0f..9e59435b 100644 --- a/tests/blob/test_common_blob.py +++ b/tests/blob/test_common_blob.py @@ -204,6 +204,36 @@ def test_make_blob_url_with_snapshot_and_sas(self): + '.blob.core.windows.net/vhds/my.vhd?' 'snapshot=2016-11-09T14:11:07.6175300Z&sas') + def test_make_container_url(self): + # Arrange + + # Act + res = self.bs.make_container_url('vhds') + + # Assert + self.assertEqual(res, 'https://' + self.settings.STORAGE_ACCOUNT_NAME + + '.blob.core.windows.net/vhds?restype=container') + + def test_make_container_url_with_protocol(self): + # Arrange + + # Act + res = self.bs.make_container_url('vhds', protocol='http') + + # Assert + self.assertEqual(res, 'http://' + self.settings.STORAGE_ACCOUNT_NAME + + '.blob.core.windows.net/vhds?restype=container') + + def test_make_container_url_with_sas(self): + # Arrange + + # Act + res = self.bs.make_container_url('vhds', sas_token='sas') + + # Assert + self.assertEqual(res, 'https://' + self.settings.STORAGE_ACCOUNT_NAME + + '.blob.core.windows.net/vhds?restype=container&sas') + @record def test_create_blob_with_question_mark(self): # Arrange @@ -959,22 +989,29 @@ def test_account_sas(self): blob_name = self._create_block_blob() token = self.bs.generate_account_shared_access_signature( - ResourceTypes.OBJECT, + ResourceTypes.OBJECT + ResourceTypes.CONTAINER, AccountPermissions.READ, datetime.utcnow() + timedelta(hours=1), ) # Act - url = self.bs.make_blob_url( + blob_url = self.bs.make_blob_url( self.container_name, blob_name, sas_token=token, ) - response = requests.get(url) + container_url = self.bs.make_container_url( + self.container_name, + sas_token=token, + ) + + blob_response = requests.get(blob_url) + container_response = requests.get(container_url) # Assert - self.assertTrue(response.ok) - self.assertEqual(self.byte_data, response.content) + self.assertTrue(blob_response.ok) + self.assertEqual(self.byte_data, blob_response.content) + self.assertTrue(container_response.ok) @record def test_shared_read_access_blob(self):