如何使用Python在附加模式下打开GCS存储桶中的文件? [英] How to open a file in a GCS bucket in append mode using Python?

查看:95
本文介绍了如何使用Python在附加模式下打开GCS存储桶中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了所有相关问题,发现所有内容都已上传到GCS而不是写作.我不是要上传文件或创建新文件.我需要以附加模式打开位于谷歌云存储桶中的文件,即如果不存在则创建该文件并使用python写入文件的末尾.如果有人能指出正确的方向,我将不胜感激. 每次上传方法都会替换我的文件.我尝试下载,附加然后替换,但这要求文件最初存在于目录中. 有没有一种方法可以在gcs中实现附加模式功能?

I've looked at all related questions and all I can find are uploading to GCS and not writing. I'm not trying to upload a file or create a new one. I need to open the file located in a google cloud storage bucket in append mode i.e create it if it doesn't exist and write to the end of it using python. If anybody could point me in the right direction, I'd be really grateful. The upload method replaces my file each time. I've tried downloading, appending and then replacing but this requires the file to be initially present in the directory. Is there a way to implement the append mode functionality in gcs?

PS:我正在尝试在云功能中执行此操作,而不是在配备Cloud SDK的计算机上执行

PS: I'm trying to do this in cloud functions and not a machine equipped with the Cloud SDK

推荐答案

无法执行此操作,因为GCS中的对象是不可变的.

There's no way to do this, because objects in GCS are immutable.

来自 https://cloud.google.com/storage/docs/关键术语#immutability :

对象是不可变的,这意味着上载的对象在其整个存储生命周期内都无法更改.对象的存储生存期是成功创建(上传)对象和成功删除对象之间的时间.实际上,这意味着您无法对对象进行增量更改,例如追加操作或截断操作.但是,有可能覆盖存储在Cloud Storage中的对象,并且原子地进行此操作-直到新的上传完成后,旧版本的对象将被提供给读者,而在上传完成后,新版本的对象将被提供给读者.服务于读者.因此,一次覆盖操作仅标记了一个不可变对象的生存期的结束和一个新的不可变对象的生存期的开始.

Objects are immutable, which means that an uploaded object cannot change throughout its storage lifetime. An object's storage lifetime is the time between successful object creation (upload) and successful object deletion. In practice, this means that you cannot make incremental changes to objects, such as append operations or truncate operations. However, it is possible to overwrite objects that are stored in Cloud Storage, and doing so happens atomically — until the new upload completes the old version of the object will be served to readers, and after the upload completes the new version of the object will be served to readers. So a single overwrite operation simply marks the end of one immutable object's lifetime and the beginning of a new immutable object's lifetime.

这篇关于如何使用Python在附加模式下打开GCS存储桶中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆