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

Bucket.delete_blobs doesn't call on_error in a batch context #659

Closed
jd185367 opened this issue Dec 3, 2021 · 2 comments
Closed

Bucket.delete_blobs doesn't call on_error in a batch context #659

jd185367 opened this issue Dec 3, 2021 · 2 comments
Assignees
Labels
api: storage Issues related to the googleapis/python-storage API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jd185367
Copy link

jd185367 commented Dec 3, 2021

Trying to delete a file that doesn't exist with Bucket.delete_blobs should call the function passed to the on_error arg (if provided) instead of raising an exception. That's the behavior I observe when calling this method normally, but trying to do this inside a batch deletion seems to ignore the passed function and throw the exception anyway (instead of calling the function like I'd expect).

This appears to be closely related to issue #31, but seemed a distinct enough use case to be worth mentioning.

Environment details

  • OS: Windows 10 Enterprise, Version 1909
  • Python version: 3.9.6
  • pip version: 21.2.4 (using the poetry package manager, v1.1.11)
  • google-cloud-storage version: 1.43.0

Steps to reproduce

  1. Create a bucket in a Google Cloud project and confirm you can connect to it from a Python script on your machine (GOOGLE_APPLICATION_CREDENTIALS set, etc).
  2. Run the example code below on the machine with at least 1 name in filenames that doesn't exist in the bucket.
  3. Confirm that print_blob_name is executed if the with gcs_client.batch(): line is removed, but an exception is thrown instead when it's present.

Code example

import google.cloud.storage

gcs_client = google.cloud.storage.Client()
bucket = gcs_client.lookup_bucket("bucket-name")
filenames = ["non_existent.png"]

def print_blob_name(blob: google.cloud.storage.blob.Blob):
    print(f"ERROR - tried to delete missing blob '{str(blob)}'")

with gcs_client.batch():
    bucket.delete_blobs(filenames, on_error=print_blob_name)

Stack trace

Traceback (most recent call last):
  File "C:\Users\jd185367\.pyenv\pyenv-win\versions\3.9.6\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\jd185367\.pyenv\pyenv-win\versions\3.9.6\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\jd185367\<redacted>\gcs_deletion_test.py", line 11, in <module>
    bucket.delete_blobs(filenames, on_error=print_blob_name)
  File "C:\Users\jd185367\AppData\Local\pypoetry\Cache\virtualenvs\<redacted>-I8hA3ptd-py3.9\lib\site-packages\google\cloud\storage\batch.py", line 299, in __exit__
    self.finish()
  File "C:\Users\jd185367\AppData\Local\pypoetry\Cache\virtualenvs\<redacted>-I8hA3ptd-py3.9\lib\site-packages\google\cloud\storage\batch.py", line 285, in finish
    self._finish_futures(responses)
  File "C:\Users\jd185367\AppData\Local\pypoetry\Cache\virtualenvs\<redacted>-I8hA3ptd-py3.9\lib\site-packages\google\cloud\storage\batch.py", line 261, in _finish_futures
    raise exceptions.from_http_response(exception_args)
google.api_core.exceptions.NotFound: 404 BATCH contentid://None: No such object: bucket-name/non_existent.png
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/python-storage API. label Dec 3, 2021
@andrewsg andrewsg added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Dec 3, 2021
@andrewsg
Copy link
Contributor

andrewsg commented Dec 3, 2021

Thank you for this detailed bug report.

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels Mar 4, 2022
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jun 2, 2022
@cojenco cojenco self-assigned this Jun 3, 2022
@cojenco cojenco added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p3 Desirable enhancement or fix. May not be included in next release. and removed type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. 🚨 This issue needs some love. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Jun 3, 2022
@cojenco
Copy link
Contributor

cojenco commented Jun 6, 2023

The current batch design does not support library methods whose return values depend on the response payload. In this case, the error handling via the on_error callable in bucket.delete_blobs() is not supported in a Batch context.

We've updated the docstrings with a warning and added clarifications on the limited Batch support in the python client, see details in #1045

@cojenco cojenco closed this as completed Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/python-storage API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

4 participants