Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Ensure GCS object is copied before deleting it.

The Cloud Storage documentation shows how to rename an object. It involves copying it to a new object then deleting the original via this code:

// Copy source object to target object
storage.copy(
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());
Blob copiedObject = storage.get(target);
// Delete the original blob now that we've copied to where we want it, finishing the "move"
// operation
storage.get(source).delete();

But I do not see any error handling here. What if the original object does not get copied and you delete it? What would be the appropriate try/catch or other technique to ensure the object got copied before deleting it?

 

1 2 362
2 REPLIES 2

Hi @Zelgon 

If renaming an object involves copying it and deleting the original then most likely it follows the Cloud Storage protocol in copying files and objects. 

In Copying Files and Objects, if an error occurs while trying to copy, it will try to attempt to continue the remaining files. if any copies are unsuccessful, gsutil's exit status is non-zero, even if this flag is set.

You can also contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!

I'm more looking for a Java answer here. Is there a try/catch to run and a specific Java exception to catch?