user2427317
user2427317

Reputation: 111

Create/append file in Google cloud bucket

How do I create a file in my google cloud bucket, or append to the file if it already exists?

Let's say I want to add a file text.txt to my bucket, located at 'data_folder'. How do I check that it doesn't already exist?

Below does not work, at least for me.

    if os.path.isfile(os.path.join(data_folder, 'test.txt')):
        write_append()
    else:
        write_create()  # first run

Upvotes: 0

Views: 2694

Answers (1)

LundinCast
LundinCast

Reputation: 9810

It isn't possible to append to an existing Cloud Storage file. As per the documentation (emphasis is mine):

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.

You'll need to manually check if the file exists and, if it does, get its content, append to it in your code and upload the new one.

Upvotes: 1

Related Questions