0
votes

I have an App Engine cron job that runs every week, uploading a file called logs.json to a Google Cloud Storage bucket.

For the past few months, this file has been overwritten each time the new version was uploaded.

In the last few weeks, rather than overwriting the file, the existing copy has been retained and the new one uploaded under a different name, e.g. logs_XHjYmP3.json.

This is a simplified snippet from the Django storage class where the upload is performed. I have verified that the filename is correct at the point of upload:

# Prints 'logs.json'
print(file.name)
blob.upload_from_file(file, content_type=content_type)
blob.make_public()

Reading the documentation, it says:

The effect of uploading to an existing blob depends on the “versioning” and “lifecycle” policies defined on the blob’s bucket. In the absence of those policies, upload will overwrite any existing contents.

The versioning for the bucket is set to suspended, and I'm not aware of any other settings or any changes I have made that would affect this.

How can I make the file upload overwrite any existing file with the same name?

1
Adding the related log entries to the operation could help us to track the origin of the incorrect name assigmentFabián García
Even with object versioning enabled, you should still be able to overwrite the file. I just tried what's in the documentation you mentioned above (with object versioning enabled in my bucket) and it works with no issue.Christopher
I recommend opening an issue to the client library's GitHub repository, asking if there was a behavior change.Ahmet Alp Balkan

1 Answers

1
votes

After further testing, although print(file.name) looked correct, the incorrect filename was actually coming from Django's get_available_name() storage class method. That method was trying to generate a unique filename if the file already existed. I have added the method to my custom storage class, and, if the file meets the criteria, I just return the existing name to allow it to overwrite. I'm still not sure why it started doing this, however.