'Is it possible to use x-goog-if-generation-match in google.cloud.storage python client?

I'm using google.cloud.storage Python library to store files in Google Cloud Storage from my App Engine flexible application. I would like to x-goog-if-generation-match header to prevent overwriting newer versions, but can't find a way to pass it to the library.

Is it possible to utilise blob generation checking with google.cloud.storage Python library?



Solution 1:[1]

As of today, google cloud python client supports generation and metageneration in the API, including Blob.upload_from_string:

https://googleapis.dev/python/storage/latest/generation_metageneration.html#conditional-parameters

Using if_generation_match

Passing the if_generation_match parameter to a method which retrieves a blob resource (e.g., Blob.reload) or modifies the blob (e.g., Blob.update) makes the operation conditional on whether the blob’s current generation matches the given value. As a special case, passing 0 as the value for if_generation_match makes the operation succeed only if there are no live versions of the blob.

Solution 2:[2]

AFAIK the x-goog-if-generation-match header is only available in the XML API.

The google.cloud.storage library doesn't allow generic, direct access to the request headers. In some cases access is supported, but via dedicated properties and I see none equivalent to x-goog-if-generation-match in google.cloud.storage.blob.Blob.

I do see methods for retrieving the object/blob's generation and meta-generations, though (but those aren't equivalent to x-goog-if-generation-match):

generation

Retrieve the generation for the object.

See https://cloud.google.com/storage/docs/json_api/v1/objects

Return type: int or NoneType Returns: The generation of the blob or None if the blob’s resource has not been loaded from the server.

and

metageneration

Retrieve the metageneration for the object.

See https://cloud.google.com/storage/docs/json_api/v1/objects

Return type: int or NoneType Returns: The metageneration of the blob or None if the blob’s resource has not been loaded from the server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alek Kowalczyk
Solution 2 Dan Cornilescu