Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Blob Storage delete_blobs() raises TypeError: quote_from_bytes() expected bytes #25764

Closed
wallflower762 opened this issue Aug 18, 2022 · 4 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Storage Storage Service (Queues, Blobs, Files)

Comments

@wallflower762
Copy link

  • Package Name: azure-storage-blob
  • Package Version: 12.13.1
  • Operating System: Windows 10
  • Python Version: 3.8.6

Describe the bug
Passing a list of BlobProperties or string blob names to delete_blobs raises TypeError: quote_from_bytes() expected bytes

To Reproduce

import os
from typing import List
from azure.storage.blob import BlobProperties, ContainerClient


def get_blob_connection() -> ContainerClient:
    return ContainerClient.from_connection_string(
        conn_str=os.environ["CONNECTION_STRING"],
        container_name="answer-products",
    )


def list_blobs(blob_connection: ContainerClient) -> List[BlobProperties]:
    prefix = 'a690b29f-88c5-4af2-9896-06c0752e9221/9_2100_19_1720_DP1/'
    blobs = [i for i in blob_connection.list_blobs(name_starts_with=prefix)]
    return blobs


def delete_blobs(
    blob_connection: ContainerClient, blob_list: List[BlobProperties]
):
    blob_connection.delete_blobs(blob_list)


def main():
    blob_connection = get_blob_connection()
    print('Connection created.')
    blob_list = list_blobs(blob_connection)
    print(f'Found {len(blob_list)} total blobs.')
    # blob_list = [str(b.name) for b in blob_list] # Also doesn't work, same exception
    delete_blobs(blob_connection, blob_list)
    print(f'Deleted {len(blob_list)} filtered blobs.')


if __name__ == "__main__":
    main()

The above code should give a stack trace like this:

File "c:/_DEV/prototyping/clean_up_qaqc_artifacts.py", line 40, in delete_blobs
    blob_connection.delete_blobs(blob_list)
  File "C:\_DEV\prototyping\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "C:\_DEV\prototyping\lib\site-packages\azure\storage\blob\_container_client.py", line 1358, in delete_blobs
    reqs, options = self._generate_delete_blobs_options(*blobs, **kwargs)
  File "C:\_DEV\prototyping\lib\site-packages\azure\storage\blob\_container_client.py", line 1262, in _generate_delete_blobs_options
    "/{}/{}{}".format(quote(container_name), quote(blob_name, safe='/~'), self._query_str),
  File "C:\Users\JMONKHOUSE\AppData\Local\Programs\Python\Python38\lib\urllib\parse.py", line 839, in quote
    return quote_from_bytes(string, safe)
  File "C:\Users\JMONKHOUSE\AppData\Local\Programs\Python\Python38\lib\urllib\parse.py", line 864, in quote_from_bytes
    raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes

Expected behavior
The blobs listed are deleted from blob storage.

Screenshots
NA

Additional context
There appears to be a few mentions of this issue around the internet (although no existing issues as far as I could see):

@ghost ghost added customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Aug 18, 2022
@github-actions github-actions bot added the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Aug 18, 2022
@azure-sdk azure-sdk added Client This issue points to a problem in the data-plane of the library. needs-team-triage Workflow: This issue needs the team to triage. Storage Storage Service (Queues, Blobs, Files) labels Aug 18, 2022
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Aug 18, 2022
@jalauzon-msft
Copy link
Member

Hi @wallflower762, thanks for reporting this! We'll investigate and get back to you ASAP.

@jalauzon-msft jalauzon-msft removed the needs-team-triage Workflow: This issue needs the team to triage. label Aug 18, 2022
@Stevenjin8
Copy link
Contributor

Stevenjin8 commented Aug 18, 2022

Hi @wallflower762, could try modifying your delete_blobs function to

def delete_blobs(
    blob_connection: ContainerClient, blob_list: List[BlobProperties]
):
    blob_connection.delete_blobs(*blob_list)

according to the docs, the ContainerClient.delete_blobs method takes *blobs as its first argument. So if you pass in a list without unpacking it, you will end up with blob == ([...],)

@wallflower762
Copy link
Author

Ah I see, that makes sense. Thanks for clarifying Steven.

Looking at the docs within the code base, it indicates that the blobs parameter can be a list of str, list of dict, or list of BlobProperties
image

I found this a bit misleading.

@jalauzon-msft
Copy link
Member

Thanks for confirming @wallflower762. I do agree that type hint is a bit misleading. We will make a change to properly document the type here as just Union[str, dict, BlobProperties] as that would be the proper way to type hint a *arg.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

5 participants