From ce91433096d72718e9d7c6e0b988e722eae7f0e0 Mon Sep 17 00:00:00 2001 From: Tomas Celaya Date: Thu, 25 Oct 2018 12:48:01 -0700 Subject: [PATCH] Avoid deleting blob when it's renamed to itself --- storage/google/cloud/storage/bucket.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/storage/google/cloud/storage/bucket.py b/storage/google/cloud/storage/bucket.py index 9829f3b6d7c9..56bcf145bae1 100644 --- a/storage/google/cloud/storage/bucket.py +++ b/storage/google/cloud/storage/bucket.py @@ -904,7 +904,8 @@ def rename_blob(self, blob, new_name, client=None): If :attr:`user_project` is set, bills the API request to that project. Effectively, copies blob to the same bucket with a new name, then - deletes the blob. + deletes the blob. In case the new and old names are the same, the blob + is returned without any operations occurring. .. warning:: @@ -926,6 +927,9 @@ def rename_blob(self, blob, new_name, client=None): :rtype: :class:`Blob` :returns: The newly-renamed blob. """ + if blob.name == new_name: + return blob + new_blob = self.copy_blob(blob, self, new_name, client=client) blob.delete(client=client) return new_blob