Michaela Ervin
Michaela Ervin

Reputation: 750

GCS file locking query

We are using GCS (google cloud storage) and I am wondering if the methods described in https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage using open/close lock the file from other things accessing it during the operations?

I am a novice and currently the first method I found is using the download and upload as string functions in import cloudstorage as gcs

like...

current_details = json.loads(device_blob.download_as_string().decode('utf-8'))
LOGGER.info(current_details)
current_details.update(details)
device_blob.upload_from_string(json.dumps(current_details))

But it is obvious here that between the download and upload calls something else could modify the file.

So if the import cloudstorage as gcs is the way to go, please let me know.

Thank you!

Upvotes: 1

Views: 2403

Answers (1)

David
David

Reputation: 9731

GCS does not have any locking at all. If you want to do "read-modify-write" safely, use Preconditions on the upload to make sure that when you write nothing has been changed in the interim (and if it has, re-do the read & modify with the lastest version).

Unfortunately the cloudstorage python library has weak support for this, you may need to use automatically generated "google API library".

Upvotes: 2

Related Questions