Skip to content

Commit

Permalink
[Storage][Blob][Bug]Support parsing blob url with '/' in blob name
Browse files Browse the repository at this point in the history
  • Loading branch information
xiafu-msft committed Jul 21, 2020
1 parent 062062f commit 537a3a6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
17 changes: 11 additions & 6 deletions sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
try:
from urllib.parse import urlparse, quote, unquote
except ImportError:
from urlparse import urlparse # type: ignore
from urllib2 import quote, unquote # type: ignore
from urlparse import urlparse # type: ignore
from urllib2 import quote, unquote # type: ignore

import six
from azure.core.tracing.decorator import distributed_trace
Expand Down Expand Up @@ -180,7 +180,7 @@ def _format_url(self, hostname):
@classmethod
def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
# type: (str, Optional[Any], Optional[Union[str, Dict[str, Any]]], Any) -> BlobClient
"""Create BlobClient from a blob url.
"""Create BlobClient from a blob url. This doesn't support customized blob url with '/' in blob name.
:param str blob_url:
The full endpoint URL to the Blob, including SAS token and snapshot if used. This could be
Expand Down Expand Up @@ -209,10 +209,15 @@ def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
if not parsed_url.netloc:
raise ValueError("Invalid URL: {}".format(blob_url))

path_blob = parsed_url.path.lstrip('/').split('/')
account_path = ""
if len(path_blob) > 2:
account_path = "/" + "/".join(path_blob[:-2])
if ".core." in parsed_url.netloc:
# .core. is indicating non-customized url. Blob name with directory info can also be parsed.
path_blob = parsed_url.path.lstrip('/').split('/', 1)
else:
# for customized url. blob name that has directory info cannot be parsed.
path_blob = parsed_url.path.lstrip('/').split('/')
if len(path_blob) > 2:
account_path = "/" + "/".join(path_blob[:-2])
account_url = "{}://{}{}?{}".format(
parsed_url.scheme,
parsed_url.netloc.rstrip('/'),
Expand Down
6 changes: 6 additions & 0 deletions sdk/storage/azure-storage-blob/tests/test_blob_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ def test_create_service_with_custom_account_endpoint_path(self):
self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
self.assertEqual(service.url, 'http://local-machine:11002/custom/account/path/foo/bar?snapshot=baz')

def test_create_blob_client_with_sub_directory_path_in_blob_name(self):
blob_url = "https://testaccount.blob.core.windows.net/containername/dir1/sub000/2010_Unit150_Ivan097_img0003.jpg"
blob_client = BlobClient.from_blob_url(blob_url)
self.assertEqual(blob_client.container_name, "containername")
self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")

@GlobalStorageAccountPreparer()
def test_request_callback_signed_header(self, resource_group, location, storage_account, storage_account_key):
# Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ def test_create_service_with_custom_account_endpoint_path(self):
self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
self.assertEqual(service.url, 'http://local-machine:11002/custom/account/path/foo/bar?snapshot=baz')

def test_create_blob_client_with_sub_directory_path_in_blob_name(self):
blob_url = "https://testaccount.blob.core.windows.net/containername/dir1/sub000/2010_Unit150_Ivan097_img0003.jpg"
blob_client = BlobClient.from_blob_url(blob_url)
self.assertEqual(blob_client.container_name, "containername")
self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")

@GlobalStorageAccountPreparer()
@AsyncStorageTestCase.await_prepared_test
async def test_request_callback_signed_header_async(self, resource_group, location, storage_account, storage_account_key):
Expand Down

0 comments on commit 537a3a6

Please sign in to comment.