Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions azure-storage-blob/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
27 changes: 27 additions & 0 deletions azure-storage-blob/azure/storage/blob/baseblobservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down
47 changes: 42 additions & 5 deletions tests/blob/test_common_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down