0

I'm using Google Cloud Python API to upload blobs into buckets, which is working just fine. However, for my specific use case, I need to prevent files from being overwritten. Instead of doing it in two go's (1st request to check if the file exists, 2nd upload the blob), I would much prefer to do it in one shot.
Apparently this can be done by using some conditional HTTP headers, e.g. If-None-Match: '*'.

Regretfully, the Google Cloud Python API doesn't seem to allow for any custom headers.

Any ideas on how I could go about this?

Thanks

ike
  • 1
  • 1

1 Answers1

0

Found a workaround in GoogleCloudPlatform Issues log in GitHub - #4490:

I am currently hacking around this by replacing google.cloud.storage.blob._MULTIPART_URL_TEMPLATE and google.cloud.storage.blob._RESUMABLE_URL_TEMPLATE with versions that have the precondition I require, e.g.

from google.cloud import storage as gcs

gcs.blob._MULTIPART_URL_TEMPLATE = (
    f'{gcs.blob._MULTIPART_URL_TEMPLATE}&ifGenerationMatch=0'
)
gcs.blob._RESUMABLE_URL_TEMPLATE = (
    f'{gcs.blob._RESUMABLE_URL_TEMPLATE}&ifGenerationMatch=0'
)

This is marked as a feature request. Hopefully it'll be added in the near future.

ike
  • 1
  • 1