【问题标题】:GCS file locking queryGCS 文件锁定查询
【发布时间】:2019-02-19 14:39:54
【问题描述】:

我们正在使用 GCS(谷歌云存储),我想知道https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage 中描述的方法是否会在操作期间锁定文件以防止其他访问它的东西?

我是新手,目前我发现的第一个方法是在import cloudstorage as gcs中使用下载和上传作为字符串函数

喜欢...

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))

但很明显,在下载和上传调用之间,其他东西可能会修改文件。

所以如果import cloudstorage as gcs 是要走的路,请告诉我。

谢谢!

【问题讨论】:

  • “如果导入cloudstorage as gcs 是要走的路”是什么意思?
  • 如果使用打开/关闭锁定文件,使其他服务无法访问它,直到它被关闭。

标签: python-3.x google-cloud-platform google-cloud-storage


【解决方案1】:

GCS 根本没有任何锁定。如果您想安全地执行“读取-修改-写入”,请在上传时使用Preconditions,以确保在您写入时没有任何更改(如果有,请重新执行读取和修改)最新版本)。

不幸的是cloudstorage python 库对此的支持较弱,您可能需要使用automatically generated "google API library"

【讨论】:

  • 所以即使这样做 gcs_file = gcs.open(filename, 'w', ...) gcs_file.write('abcde\n') gcs_file.write('f'*1024*4 + '\n') gcs_file.close() 在写入期间,其他一些服务可以打开文件并同时写入吗?
  • 正确。在您调用 close() 之前不会写入任何内容,并且最后一次关闭的写入会覆盖它之前的任何内容。
  • 不要将 GCS 视为文件系统 - 它是纯粹的对象存储,没有您期望从文件系统获得的许多行为。
猜你喜欢
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2020-06-09
  • 1970-01-01
相关资源
最近更新 更多