8000 Add storage move_blob sample and fix confusion with rename (#6554) · orossini/python-docs-samples@7fa18f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fa18f1

Browse files
Aaron Gabriel Neyercojencoleahecole
authored
Add storage move_blob sample and fix confusion with rename (GoogleCloudPlatform#6554)
* Add storage move_blob sample and fix confusion with rename * fix license heading issues * Add descriptive comments to parameters * Update storage/cloud-client/storage_move_file.py * Apply suggestions from code review Add print statement in except clause Co-authored-by: cojenco <cathyo@google.com> Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 7549e24 commit 7fa18f1

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

storage/cloud-client/snippets_test.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import storage_object_get_kms_key
5858
import storage_remove_bucket_label
5959
import storage_remove_cors_configuration
60+
import storage_rename_file
6061
import storage_set_bucket_default_kms_key
6162
import storage_set_metadata
6263
import storage_upload_file
@@ -285,14 +286,31 @@ def test_rename_blob(test_blob):
285286
try:
286287
bucket.delete_blob("test_rename_blob")
287288
except google.cloud.exceptions.exceptions.NotFound:
288-
pass
289+
print("test_rename_blob not found in bucket {}".format(bucket.name))
289290

290-
storage_move_file.rename_blob(bucket.name, test_blob.name, "test_rename_blob")
291+
storage_rename_file.rename_blob(bucket.name, test_blob.name, "test_rename_blob")
291292

292293
assert bucket.get_blob("test_rename_blob") is not None
293294
assert bucket.get_blob(test_blob.name) is None
294295

295296

297+
def test_move_blob(test_bucket_create, test_blob):
298+
bucket = test_blob.bucket
299+
storage.Client().create_bucket(test_bucket_create)
300+
301+
try:
302+
test_bucket_create.delete_blob("test_move_blob")
303+
except google.cloud.exceptions.NotFound:
304+
print("test_move_blob not found in bucket {}".format(test_bucket_create.name))
305+
306+
storage_move_file.move_blob(
307+
bucket.name, test_blob.name, test_bucket_create.name, "test_move_blob"
308+
)
309+
310+
assert test_bucket_create.get_blob("test_move_blob") is not None
311+
assert bucket.get_blob(test_blob.name) is None
312+
313+
296314
def test_copy_blob(test_blob):
297315
bucket = storage.Client().bucket(test_blob.bucket.name)
298316

storage/cloud-client/storage_move_file.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright 2019 Google Inc. All Rights Reserved.
3+
# Copyright 2019 Google LLC. All Rights Reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the 'License');
66
# you may not use this file except in compliance with the License.
@@ -20,24 +20,44 @@
2020
from google.cloud import storage
2121

2222

23-
def rename_blob(bucket_name, blob_name, new_name):
24-
"""Renames a blob."""
23+
def move_blob(bucket_name, blob_name, destination_bucket_name, destination_blob_name):
24+
"""Moves a blob from one bucket to another with a new name."""
25+
# The ID of your GCS bucket
2526
# bucket_name = "your-bucket-name"
27+
# The ID of your GCS object
2628
# blob_name = "your-object-name"
27-
# new_name = "new-object-name"
29+
# The ID of the bucket to move the object to
30+
# destination_bucket_name = "destination-bucket-name"
31+
# The ID of your new GCS object (optional)
32+
# destination_blob_name = "destination-object-name"
2833

2934
storage_client = storage.Client()
30-
bucket = storage_client.bucket(bucket_name)
31-
blob = bucket.blob(blob_name)
3235

33-
new_blob = bucket.rename_blob(blob, new_name)
36+
source_bucket = storage_client.bucket(bucket_name)
37+
source_blob = source_bucket.blob(blob_name)
38+
destination_bucket = storage_client.bucket(destination_bucket_name)
3439

35-
print("Blob {} has been renamed to {}".format(blob.name, new_blob.name))
40+
blob_copy = source_bucket.copy_blob(
41+
source_blob, destination_bucket, destination_blob_name
42+
)
43+
source_bucket.delete_blob(blob_name)
44+
45+
print(
46+
"Blob {} in bucket {} moved to blob {} in bucket {}.".format(
47+
source_blob.name,
48+
source_bucket.name,
49+
blob_copy.name,
50+
destination_bucket.name,
51+
)
52+
)
3653

3754

3855
# [END storage_move_file]
3956

4057
if __name__ == "__main__":
41-
rename_blob(
42-
bucket_name=sys.argv[1], blob_name=sys.argv[2], new_name=sys.argv[3]
58+
move_blob(
59+
bucket_name=sys.argv[1],
60+
blob_name=sys.argv[2],
61+
destination_bucket_name=sys.argv[3],
62+
destination_blob_name=sys.argv[4],
4363
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC. All Rights Reserved.
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_rename_file]
20+
from google.cloud import storage
21+
22+
23+
def rename_blob(bucket_name, blob_name, new_name):
24+
"""Renames a blob."""
25+
# The ID of your GCS bucket
26+
# bucket_name = "your-bucket-name"
27+
# The ID of the GCS object to rename
28+
# blob_name = "your-object-name"
29+
# The new ID of the GCS object
30+
# new_name = "new-object-name"
31+
32+
storage_client = storage.Client()
33+
bucket = storage_client.bucket(bucket_name)
34+
blob = bucket.blob(blob_name)
35+
36+
new_blob = bucket.rename_blob(blob, new_name)
37+
38+
print("Blob {} has been renamed to {}".format(blob.name, new_blob.name))
39+
40+
41+
# [END storage_rename_file]
42+
43+
if __name__ == "__main__":
44+
rename_blob(bucket_name=sys.argv[1], blob_name=sys.argv[2], new_name=sys.argv[3])

0 commit comments

Comments
 (0)
0