From 52a3ff1e7eedcdbe9ee5e2d372144aead385aa60 Mon Sep 17 00:00:00 2001 From: Yulin Li Date: Sun, 9 Oct 2022 21:37:24 +0800 Subject: [PATCH] Fix #26720, ContainerClient.from_container_url cannot work with URL ending with / --- .../azure/storage/blob/_container_client.py | 4 ++-- sdk/storage/azure-storage-blob/tests/test_blob_client.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/sdk/storage/azure-storage-blob/azure/storage/blob/_container_client.py b/sdk/storage/azure-storage-blob/azure/storage/blob/_container_client.py index 16642ff358d0..300a70a86a1d 100644 --- a/sdk/storage/azure-storage-blob/azure/storage/blob/_container_client.py +++ b/sdk/storage/azure-storage-blob/azure/storage/blob/_container_client.py @@ -211,11 +211,11 @@ def from_container_url( container_url = "https://" + container_url except AttributeError: raise ValueError("Container URL must be a string.") - parsed_url = urlparse(container_url.rstrip('/')) + parsed_url = urlparse(container_url) if not parsed_url.netloc: raise ValueError("Invalid URL: {}".format(container_url)) - container_path = parsed_url.path.lstrip('/').split('/') + container_path = parsed_url.path.strip('/').split('/') account_path = "" if len(container_path) > 1: account_path = "/" + "/".join(container_path[:-1]) diff --git a/sdk/storage/azure-storage-blob/tests/test_blob_client.py b/sdk/storage/azure-storage-blob/tests/test_blob_client.py index 46cde4d0b680..f716cd565b1f 100644 --- a/sdk/storage/azure-storage-blob/tests/test_blob_client.py +++ b/sdk/storage/azure-storage-blob/tests/test_blob_client.py @@ -517,6 +517,13 @@ def test_create_service_with_custom_account_endpoint_path(self): assert service.primary_hostname == 'local-machine:11002/custom/account/path' assert service.url == 'http://local-machine:11002/custom/account/path/foo' + service = ContainerClient.from_container_url("http://local-machine:11002/custom/account/path/foo/?query=value") + assert service.account_name == None + assert service.container_name == "foo" + assert service.credential == None + assert service.primary_hostname == 'local-machine:11002/custom/account/path' + assert service.url == 'http://local-machine:11002/custom/account/path/foo' + service = BlobClient(account_url=custom_account_url, container_name="foo", blob_name="bar", snapshot="baz") assert service.account_name == None assert service.container_name == "foo"