Skip to content

Commit edde950

Browse files
authored
samples: add samples for move api to rename an object (#1505)
* docs: add samples for move api to rename an object * minor change * fix lint errors * minor fix * resolving comments
1 parent 500c0d0 commit edde950

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

samples/snippets/snippets_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import storage_list_soft_deleted_objects
7474
import storage_make_public
7575
import storage_move_file
76+
import storage_move_file_atomically
7677
import storage_object_get_kms_key
7778
import storage_remove_bucket_label
7879
import storage_remove_cors_configuration
@@ -1037,3 +1038,20 @@ def test_storage_restore_soft_deleted_object(test_soft_delete_enabled_bucket, ca
10371038
# Verify the restoration
10381039
blob = test_soft_delete_enabled_bucket.get_blob(blob_name)
10391040
assert blob is not None
1041+
1042+
1043+
def test_move_object(test_blob):
1044+
bucket = test_blob.bucket
1045+
try:
1046+
bucket.delete_blob("test_move_blob_atomic")
1047+
except google.cloud.exceptions.NotFound:
1048+
print(f"test_move_blob_atomic not found in bucket {bucket.name}")
1049+
1050+
storage_move_file_atomically.move_object(
1051+
bucket.name,
1052+
test_blob.name,
1053+
"test_move_blob_atomic",
1054+
)
1055+
1056+
assert bucket.get_blob("test_move_blob_atomic") is not None
1057+
assert bucket.get_blob(test_blob.name) is None
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the 'License');
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sys
18+
19+
# [START storage_move_object]
20+
from google.cloud import storage
21+
22+
23+
def move_object(bucket_name: str, blob_name: str, new_blob_name: str) -> None:
24+
"""Moves a blob to a new name within the same bucket using the move API."""
25+
# The name of your GCS bucket
26+
# bucket_name = "your-bucket-name"
27+
28+
# The name of your GCS object to move
29+
# blob_name = "your-file-name"
30+
31+
# The new name of the GCS object
32+
# new_blob_name = "new-file-name"
33+
34+
storage_client = storage.Client()
35+
36+
bucket = storage_client.bucket(bucket_name)
37+
blob_to_move = bucket.blob(blob_name)
38+
39+
# Use move_blob to perform an efficient, server-side move.
40+
moved_blob = bucket.move_blob(
41+
blob=blob_to_move, new_name=new_blob_name
42+
)
43+
44+
print(f"Blob {blob_to_move.name} has been moved to {moved_blob.name}.")
45+
46+
47+
# [END storage_move_object]
48+
49+
if __name__ == "__main__":
50+
move_object(
51+
bucket_name=sys.argv[1],
52+
blob_name=sys.argv[2],
53+
new_blob_name=sys.argv[3],
54+
)

0 commit comments

Comments
 (0)