Skip to content

Commit e781d1f

Browse files
[Storage] Improve exception raised when uploading 'dict' Blob (Azure#24730)
1 parent 194f078 commit e781d1f

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def _upload_blob_options( # pylint:disable=too-many-statements
376376
stream = BytesIO(data)
377377
elif hasattr(data, 'read'):
378378
stream = data
379-
elif hasattr(data, '__iter__'):
379+
elif hasattr(data, '__iter__') and not isinstance(data, (list, tuple, set, dict)):
380380
stream = IterStreamer(data, encoding=encoding)
381381
else:
382382
raise TypeError("Unsupported data type: {}".format(type(data)))
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- application/xml
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '0'
13+
User-Agent:
14+
- azsdk-python-storage-blob/12.12.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)
15+
x-ms-date:
16+
- Tue, 07 Jun 2022 19:20:35 GMT
17+
x-ms-version:
18+
- '2021-06-08'
19+
method: PUT
20+
uri: https://storagename.blob.core.windows.net/utcontainerf66d1427?restype=container&timeout=5
21+
response:
22+
body:
23+
string: "\uFEFF<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ContainerAlreadyExists</Code><Message>The
24+
specified container already exists.\nRequestId:e6910659-b01e-0003-5ca3-7a1745000000\nTime:2022-06-07T19:20:34.5823446Z</Message></Error>"
25+
headers:
26+
content-length:
27+
- '230'
28+
content-type:
29+
- application/xml
30+
date:
31+
- Tue, 07 Jun 2022 19:20:34 GMT
32+
server:
33+
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
34+
x-ms-error-code:
35+
- ContainerAlreadyExists
36+
x-ms-version:
37+
- '2021-06-08'
38+
status:
39+
code: 409
40+
message: The specified container already exists.
41+
- request:
42+
body: null
43+
headers:
44+
Accept:
45+
- application/xml
46+
Accept-Encoding:
47+
- gzip, deflate
48+
Connection:
49+
- keep-alive
50+
Content-Length:
51+
- '0'
52+
User-Agent:
53+
- azsdk-python-storage-blob/12.12.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)
54+
x-ms-date:
55+
- Tue, 07 Jun 2022 19:20:35 GMT
56+
x-ms-version:
57+
- '2021-06-08'
58+
method: PUT
59+
uri: https://storagename.blob.core.windows.net/utcontainersourcef66d1427?restype=container&timeout=5
60+
response:
61+
body:
62+
string: "\uFEFF<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ContainerAlreadyExists</Code><Message>The
63+
specified container already exists.\nRequestId:e6910683-b01e-0003-7fa3-7a1745000000\nTime:2022-06-07T19:20:34.6763141Z</Message></Error>"
64+
headers:
65+
content-length:
66+
- '230'
67+
content-type:
68+
- application/xml
69+
date:
70+
- Tue, 07 Jun 2022 19:20:34 GMT
71+
server:
72+
- Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
73+
x-ms-error-code:
74+
- ContainerAlreadyExists
75+
x-ms-version:
76+
- '2021-06-08'
77+
status:
78+
code: 409
79+
message: The specified container already exists.
80+
version: 1
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- application/xml
7+
User-Agent:
8+
- azsdk-python-storage-blob/12.12.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)
9+
x-ms-date:
10+
- Tue, 07 Jun 2022 19:20:45 GMT
11+
x-ms-version:
12+
- '2021-06-08'
13+
method: PUT
14+
uri: https://storagename.blob.core.windows.net/utcontainer9941921?restype=container
15+
response:
16+
body:
17+
string: "\uFEFF<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ContainerAlreadyExists</Code><Message>The
18+
specified container already exists.\nRequestId:3c4418c3-601e-003f-37a3-7a3e82000000\nTime:2022-06-07T19:20:44.6188664Z</Message></Error>"
19+
headers:
20+
content-length: '230'
21+
content-type: application/xml
22+
date: Tue, 07 Jun 2022 19:20:44 GMT
23+
server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
24+
x-ms-error-code: ContainerAlreadyExists
25+
x-ms-version: '2021-06-08'
26+
status:
27+
code: 409
28+
message: The specified container already exists.
29+
url: https://vincenttranstock.blob.core.windows.net/utcontainer9941921?restype=container
30+
- request:
31+
body: null
32+
headers:
33+
Accept:
34+
- application/xml
35+
User-Agent:
36+
- azsdk-python-storage-blob/12.12.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)
37+
x-ms-date:
38+
- Tue, 07 Jun 2022 19:20:45 GMT
39+
x-ms-version:
40+
- '2021-06-08'
41+
method: PUT
42+
uri: https://storagename.blob.core.windows.net/utcontainersource9941921?restype=container
43+
response:
44+
body:
45+
string: "\uFEFF<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ContainerAlreadyExists</Code><Message>The
46+
specified container already exists.\nRequestId:3c4418db-601e-003f-4aa3-7a3e82000000\nTime:2022-06-07T19:20:44.7418262Z</Message></Error>"
47+
headers:
48+
content-length: '230'
49+
content-type: application/xml
50+
date: Tue, 07 Jun 2022 19:20:44 GMT
51+
server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
52+
x-ms-error-code: ContainerAlreadyExists
53+
x-ms-version: '2021-06-08'
54+
status:
55+
code: 409
56+
message: The specified container already exists.
57+
url: https://vincenttranstock.blob.core.windows.net/utcontainersource9941921?restype=container
58+
version: 1

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ def test_create_blob_with_metadata(self, storage_account_name, storage_account_k
364364
md = blob.get_blob_properties().metadata
365365
self.assertDictEqual(md, metadata)
366366

367+
@BlobPreparer()
368+
def test_upload_blob_with_dictionary(self, storage_account_name, storage_account_key):
369+
self._setup(storage_account_name, storage_account_key)
370+
blob_name = 'test_blob'
371+
blob_data = {'hello': 'world'}
372+
373+
# Act
374+
blob = self.bsc.get_blob_client(self.container_name, blob_name)
375+
376+
# Assert
377+
with self.assertRaises(TypeError):
378+
blob.upload_blob(blob_data)
379+
367380
@BlobPreparer()
368381
def test_upload_blob_from_generator(self, storage_account_name, storage_account_key):
369382
self._setup(storage_account_name, storage_account_key)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,20 @@ async def test_create_blob_with_metadata(self, storage_account_name, storage_acc
450450
md = (await blob.get_blob_properties()).metadata
451451
self.assertDictEqual(md, metadata)
452452

453+
@BlobPreparer()
454+
@AsyncStorageTestCase.await_prepared_test
455+
async def test_upload_blob_with_dictionary_async(self, storage_account_name, storage_account_key):
456+
await self._setup(storage_account_name, storage_account_key)
457+
blob_name = 'test_blob'
458+
blob_data = {'hello': 'world'}
459+
460+
# Act
461+
blob = self.bsc.get_blob_client(self.container_name, blob_name)
462+
463+
# Assert
464+
with self.assertRaises(TypeError):
465+
await blob.upload_blob(blob_data)
466+
453467
@BlobPreparer()
454468
@AsyncStorageTestCase.await_prepared_test
455469
async def test_create_blob_with_generator_async(self, storage_account_name, storage_account_key):

0 commit comments

Comments
 (0)