Skip to content

Commit 9594828

Browse files
authored
[Storage] Give useful error msg when bad URL passed to BlobClient.from_blob_url() (Azure#23996)
1 parent 7138f19 commit 9594828

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

sdk/storage/azure-storage-blob/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 12.##.## (Unreleased)
4+
5+
### Bugs Fixed:
6+
- fixes a bug in `BlobClient.from_blob_url()` such that users will receive a more helpful error
7+
message if they pass an incorrect URL without a full `/container/blob` path.
8+
39
## 12.12.0b1 (2022-04-14)
410

511
### Features Added

sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,28 @@ def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
241241
account_path = ""
242242
if ".core." in parsed_url.netloc:
243243
# .core. is indicating non-customized url. Blob name with directory info can also be parsed.
244-
path_blob = parsed_url.path.lstrip('/').split('/', 1)
244+
path_blob = parsed_url.path.lstrip('/').split('/', maxsplit=1)
245245
elif "localhost" in parsed_url.netloc or "127.0.0.1" in parsed_url.netloc:
246-
path_blob = parsed_url.path.lstrip('/').split('/', 2)
246+
path_blob = parsed_url.path.lstrip('/').split('/', maxsplit=2)
247247
account_path += '/' + path_blob[0]
248248
else:
249249
# for customized url. blob name that has directory info cannot be parsed.
250250
path_blob = parsed_url.path.lstrip('/').split('/')
251251
if len(path_blob) > 2:
252252
account_path = "/" + "/".join(path_blob[:-2])
253+
253254
account_url = "{}://{}{}?{}".format(
254255
parsed_url.scheme,
255256
parsed_url.netloc.rstrip('/'),
256257
account_path,
257258
parsed_url.query)
259+
260+
msg_invalid_url = "Invalid URL. Provide a blob_url with a valid blob and container name."
261+
if len(path_blob) <= 1:
262+
raise ValueError(msg_invalid_url)
258263
container_name, blob_name = unquote(path_blob[-2]), unquote(path_blob[-1])
259264
if not container_name or not blob_name:
260-
raise ValueError("Invalid URL. Provide a blob_url with a valid blob and container name.")
265+
raise ValueError(msg_invalid_url)
261266

262267
path_snapshot, _ = parse_query(parsed_url.query)
263268
if snapshot:

sdk/storage/azure-storage-blob/tests/test_blob_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ def test_create_blob_client_with_sub_directory_path_in_blob_name(self):
480480
self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")
481481
self.assertEqual(blob_client.url, blob_emulator_url)
482482

483+
def test_from_blob_url_too_short_url(self):
484+
"""Test that a useful error message is obtained if user gives incorrect URL"""
485+
url = "https://testaccount.blob.core.windows.net/containername/"
486+
with pytest.raises(ValueError, match="Invalid URL"):
487+
_ = BlobClient.from_blob_url(url)
488+
483489
def test_create_client_for_emulator(self):
484490
container_client = ContainerClient(
485491
account_url='http://127.0.0.1:1000/devstoreaccount1',

0 commit comments

Comments
 (0)