Skip to content

Commit

Permalink
[Storage] Give useful error msg when bad URL passed to BlobClient.fro…
Browse files Browse the repository at this point in the history
…m_blob_url() (Azure#23996)
  • Loading branch information
pat-hearps authored Apr 18, 2022
1 parent 7138f19 commit 9594828
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions sdk/storage/azure-storage-blob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

## 12.##.## (Unreleased)

### Bugs Fixed:
- fixes a bug in `BlobClient.from_blob_url()` such that users will receive a more helpful error
message if they pass an incorrect URL without a full `/container/blob` path.

## 12.12.0b1 (2022-04-14)

### Features Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,28 @@ def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
account_path = ""
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)
path_blob = parsed_url.path.lstrip('/').split('/', maxsplit=1)
elif "localhost" in parsed_url.netloc or "127.0.0.1" in parsed_url.netloc:
path_blob = parsed_url.path.lstrip('/').split('/', 2)
path_blob = parsed_url.path.lstrip('/').split('/', maxsplit=2)
account_path += '/' + path_blob[0]
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('/'),
account_path,
parsed_url.query)

msg_invalid_url = "Invalid URL. Provide a blob_url with a valid blob and container name."
if len(path_blob) <= 1:
raise ValueError(msg_invalid_url)
container_name, blob_name = unquote(path_blob[-2]), unquote(path_blob[-1])
if not container_name or not blob_name:
raise ValueError("Invalid URL. Provide a blob_url with a valid blob and container name.")
raise ValueError(msg_invalid_url)

path_snapshot, _ = parse_query(parsed_url.query)
if snapshot:
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 @@ -480,6 +480,12 @@ def test_create_blob_client_with_sub_directory_path_in_blob_name(self):
self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")
self.assertEqual(blob_client.url, blob_emulator_url)

def test_from_blob_url_too_short_url(self):
"""Test that a useful error message is obtained if user gives incorrect URL"""
url = "https://testaccount.blob.core.windows.net/containername/"
with pytest.raises(ValueError, match="Invalid URL"):
_ = BlobClient.from_blob_url(url)

def test_create_client_for_emulator(self):
container_client = ContainerClient(
account_url='http://127.0.0.1:1000/devstoreaccount1',
Expand Down

0 comments on commit 9594828

Please sign in to comment.