Azure / azure-storage-python

Microsoft Azure Storage Library for Python
https://azure-storage.readthedocs.io
MIT License
338 stars 240 forks source link

AttributeError: 'NoneType' object has no attribute 'on_request' when deleting blob #648

Closed fs-jaszczult closed 4 years ago

fs-jaszczult commented 4 years ago

Which service(blob, file, queue) does this issue concern?

azure-storage-blob

Which version of the SDK was used? Please provide the output of pip freeze.

azure-common==1.1.24 azure-core==1.1.1 azure-storage-blob==12.1.0 azure-storage-common==2.1.0

What problem was encountered?

I'm working on an azure blob wrapper class for my team members to use and can't get the ContainerClient.delete_blobs() function to work in my delete function. Here's the code throwing the error:

def delete_blob(self, blob_name: str):
        service_client = BlobServiceClient.from_connection_string(self.connect_str)
        container_client = service_client.get_container_client(self.container_name)

        # Verify blob file exists
        if self.blob_file_exists(blob_name):
            container_client.delete_blobs(blob_name) # Error on this line
            # Verify blob file has actually been deleted
            if self.blob_file_exists(blob_name):      
                return 0
            else:
                return 1
        else:
            return -1

When attempting to delete a blob using the steps provided here, I get the following error message:

Traceback (most recent call last):
  File "c:\Users\TJaszczul\.vscode\extensions\ms-python.python-2020.1.57204\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\TJaszczul\.vscode\extensions\ms-python.python-2020.1.57204\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\TJaszczul\.vscode\extensions\ms-python.python-2020.1.57204\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\TJaszczul\Documents\GitHub\fs-automation-python\Tests\test_azure_blob.py", line 25, in <module>
    abo.delete_blob(dest_dir + upload_file_name)
  File "..\library\azure_blob.py", line 76, in delete_blob
    container_client.delete_blobs(blob_name)
  File "C:\Python37\lib\site-packages\azure\core\tracing\decorator.py", line 62, in wrapper_use_tracer
    return func(*args, **kwargs) # type: ignore
  File "C:\Python37\lib\site-packages\azure\storage\blob\_container_client.py", line 1094, in delete_blobs
    return self._batch_send(*reqs, **options)
  File "C:\Python37\lib\site-packages\azure\storage\blob\_shared\base_client.py", line 265, in _batch_send
    request, **kwargs
  File "C:\Python37\lib\site-packages\azure\core\pipeline\_base.py", line 197, in run
    self._prepare_multipart_mixed_request(request)
  File "C:\Python37\lib\site-packages\azure\core\pipeline\_base.py", line 185, in _prepare_multipart_mixed_request
    _ for _ in executor.map(prepare_requests, requests)
  File "C:\Python37\lib\site-packages\azure\core\pipeline\_base.py", line 185, in <listcomp>
    _ for _ in executor.map(prepare_requests, requests)
  File "C:\Python37\lib\concurrent\futures\_base.py", line 598, in result_iterator
    yield fs.pop().result()
  File "C:\Python37\lib\concurrent\futures\_base.py", line 428, in result
    return self.__get_result()
  File "C:\Python37\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "C:\Python37\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Python37\lib\site-packages\azure\core\pipeline\_base.py", line 180, in prepare_requests
    _await_result(policy.on_request, pipeline_request)
AttributeError: 'NoneType' object has no attribute 'on_request'

Have you found a mitigation/solution?

No, I have not.

fs-jaszczult commented 4 years ago

While I'm still not sure why the code in the OP doesn't work, I changed the function and now it works.

def delete_blobs(self, contains: str):
        blobs = self.list_blobs(contains)
        container_client = ContainerClient.from_connection_string(self.connect_str, self.container_name)

        for blob_name in blobs:
            # Verify blob file exists
            if self.blob_file_exists(blob_name):
                container_client.delete_blob(blob=blob_name)
nofunatall commented 4 years ago

You used delete_blobs in your first example Then you changed to using delete_blob with a for loop.

The batch operation delete_blobs seems to be bugged for me as well.

SubhashPeshwa commented 2 years ago

If your 'self.blob_file_exists()' function is creating another BlobServiceClient instance, it might be linked to that. I'm facing the same issue at the moment. Would be good to see the code for 'blob_file_exists()' if that's possible :)

SubhashPeshwa commented 2 years ago

Switching from BlobServiceClient to ContainerClient as demonstrated by OP, fixed my issue as well. But that maybe more to do with the how we instantiate and reuse ContainerClients as opposed to BlobServiceClients.