Skip to content

Commit 8eec220

Browse files
docs(storage): add samples for file archive generation and cors configuration (GoogleCloudPlatform#3794)
1 parent 5fb1a5d commit 8eec220

8 files changed

+397
-0
lines changed

storage/cloud-client/snippets_test.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424

2525
import storage_add_bucket_label
2626
import storage_bucket_delete_default_kms_key
27+
import storage_change_default_storage_class
28+
import storage_change_file_storage_class
2729
import storage_compose_file
2830
import storage_copy_file
31+
import storage_copy_file_archived_generation
32+
import storage_cors_configuration
2933
import storage_create_bucket_class_location
3034
import storage_define_bucket_website_configuration
3135
import storage_delete_file
36+
import storage_delete_file_archived_generation
3237
import storage_disable_bucket_lifecycle_management
3338
import storage_disable_versioning
3439
import storage_download_file
@@ -44,12 +49,14 @@
4449
import storage_get_metadata
4550
import storage_get_service_account
4651
import storage_list_buckets
52+
import storage_list_file_archived_generations
4753
import storage_list_files
4854
import storage_list_files_with_prefix
4955
import storage_make_public
5056
import storage_move_file
5157
import storage_object_get_kms_key
5258
import storage_remove_bucket_label
59+
import storage_remove_cors_configuration
5360
import storage_set_bucket_default_kms_key
5461
import storage_set_metadata
5562
import storage_upload_file
@@ -382,3 +389,67 @@ def test_storage_compose_file(test_bucket):
382389
composed = destination.download_as_string()
383390

384391
assert composed.decode("utf-8") == source_files[0] + source_files[1]
392+
393+
394+
def test_cors_configuration(test_bucket, capsys):
395+
bucket = storage_cors_configuration.cors_configuration(test_bucket)
396+
out, _ = capsys.readouterr()
397+
assert "Set CORS policies for bucket" in out
398+
assert len(bucket.cors) > 0
399+
400+
bucket = storage_remove_cors_configuration.remove_cors_configuration(test_bucket)
401+
out, _ = capsys.readouterr()
402+
assert "Remove CORS policies for bucket" in out
403+
assert len(bucket.cors) == 0
404+
405+
406+
def test_delete_blobs_archived_generation(test_blob, capsys):
407+
storage_delete_file_archived_generation.delete_file_archived_generation(
408+
test_blob.bucket.name, test_blob.name, test_blob.generation
409+
)
410+
out, _ = capsys.readouterr()
411+
assert "blob " + test_blob.name + " was deleted" in out
412+
blob = test_blob.bucket.get_blob(test_blob.name, generation=test_blob.generation)
413+
assert blob is None
414+
415+
416+
def test_change_default_storage_class(test_bucket, capsys):
417+
bucket = storage_change_default_storage_class.change_default_storage_class(
418+
test_bucket
419+
)
420+
out, _ = capsys.readouterr()
421+
assert "Default storage class for bucket" in out
422+
assert bucket.storage_class == 'COLDLINE'
423+
424+
425+
def test_change_file_storage_class(test_blob, capsys):
426+
blob = storage_change_file_storage_class.change_file_storage_class(
427+
test_blob.bucket.name, test_blob.name
428+
)
429+
out, _ = capsys.readouterr()
430+
assert "Blob {} in bucket {}". format(blob.name, blob.bucket.name) in out
431+
assert blob.storage_class == 'NEARLINE'
432+
433+
434+
def test_copy_file_archived_generation(test_blob):
435+
bucket = storage.Client().bucket(test_blob.bucket.name)
436+
437+
try:
438+
bucket.delete_blob("test_copy_blob")
439+
except google.cloud.exceptions.NotFound:
440+
pass
441+
442+
storage_copy_file_archived_generation.copy_file_archived_generation(
443+
bucket.name, test_blob.name, bucket.name, "test_copy_blob", test_blob.generation
444+
)
445+
446+
assert bucket.get_blob("test_copy_blob") is not None
447+
assert bucket.get_blob(test_blob.name) is not None
448+
449+
450+
def test_list_blobs_archived_generation(test_blob, capsys):
451+
storage_list_file_archived_generations.list_file_archived_generations(
452+
test_blob.bucket.name
453+
)
454+
out, _ = capsys.readouterr()
455+
assert str(test_blob.generation) in out
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_change_default_storage_class]
20+
from google.cloud import storage
21+
from google.cloud.storage import constants
22+
23+
24+
def change_default_storage_class(bucket_name):
25+
"""Change the default storage class of the bucket"""
26+
# bucket_name = "your-bucket-name"
27+
28+
storage_client = storage.Client()
29+
30+
bucket = storage_client.get_bucket(bucket_name)
31+
bucket.storage_class = constants.COLDLINE_STORAGE_CLASS
32+
bucket.patch()
33+
34+
print("Default storage class for bucket {} has been set to {}".format(bucket_name, bucket.storage_class))
35+
return bucket
36+
37+
38+
# [END storage_change_default_storage_class]
39+
40+
if __name__ == "__main__":
41+
change_default_storage_class(bucket_name=sys.argv[1])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_change_file_storage_class]
20+
from google.cloud import storage
21+
from google.cloud.storage import constants
22+
23+
24+
def change_file_storage_class(bucket_name, blob_name):
25+
"""Change the default storage class of the blob"""
26+
# bucket_name = "your-bucket-name"
27+
# blob_name = "your-object-name"
28+
29+
storage_client = storage.Client()
30+
31+
bucket = storage_client.get_bucket(bucket_name)
32+
blob = bucket.get_blob(blob_name)
33+
blob.update_storage_class(constants.NEARLINE_STORAGE_CLASS)
34+
35+
print(
36+
"Blob {} in bucket {} had its storage class set to {}".format(
37+
blob_name,
38+
bucket_name,
39+
blob.storage_class
40+
)
41+
)
42+
return blob
43+
44+
45+
# [START storage_change_file_storage_class]
46+
47+
if __name__ == "__main__":
48+
change_file_storage_class(bucket_name=sys.argv[1], blob_name=sys.argv[2])
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_copy_file_archived_generation]
20+
from google.cloud import storage
21+
22+
23+
def copy_file_archived_generation(
24+
bucket_name, blob_name, destination_bucket_name, destination_blob_name, generation
25+
):
26+
"""Copies a blob from one bucket to another with a new name with the same generation."""
27+
# bucket_name = "your-bucket-name"
28+
# blob_name = "your-object-name"
29+
# destination_bucket_name = "destination-bucket-name"
30+
# destination_blob_name = "destination-object-name"
31+
# generation = 1579287380533984
32+
33+
storage_client = storage.Client()
34+
35+
source_bucket = storage_client.bucket(bucket_name)
36+
source_blob = source_bucket.blob(blob_name)
37+
destination_bucket = storage_client.bucket(destination_bucket_name)
38+
39+
blob_copy = source_bucket.copy_blob(
40+
source_blob, destination_bucket, destination_blob_name, source_generation=generation
41+
)
42+
43+
print(
44+
"Generation {} of the blob {} in bucket {} copied to blob {} in bucket {}.".format(
45+
source_blob.generation,
46+
source_blob.name,
47+
source_bucket.name,
48+
blob_copy.name,
49+
destination_bucket.name,
50+
)
51+
)
52+
53+
54+
# [END storage_copy_file_archived_generation]
55+
56+
if __name__ == "__main__":
57+
copy_file_archived_generation(
58+
bucket_name=sys.argv[1],
59+
blob_name=sys.argv[2],
60+
destination_bucket_name=sys.argv[3],
61+
destination_blob_name=sys.argv[4],
62+
generation=sys.argv[5]
63+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_cors_configuration]
20+
from google.cloud import storage
21+
22+
23+
def cors_configuration(bucket_name):
24+
"""Set a bucket's CORS policies configuration."""
25+
# bucket_name = "your-bucket-name"
26+
27+
storage_client = storage.Client()
28+
bucket = storage_client.get_bucket(bucket_name)
29+
bucket.cors = [
30+
{
31+
"origin": ["*"],
32+
"responseHeader": [
33+
"Content-Type",
34+
"x-goog-resumable"],
35+
"method": ['PUT', 'POST'],
36+
"maxAgeSeconds": 3600
37+
}
38+
]
39+
bucket.patch()
40+
41+
print("Set CORS policies for bucket {} is {}".format(bucket.name, bucket.cors))
42+
return bucket
43+
44+
45+
# [END storage_cors_configuration]
46+
47+
if __name__ == "__main__":
48+
cors_configuration(bucket_name=sys.argv[1])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_delete_file_archived_generation]
20+
from google.cloud import storage
21+
22+
23+
def delete_file_archived_generation(bucket_name, blob_name, generation):
24+
"""Delete a blob in the bucket with the given generation."""
25+
# bucket_name = "your-bucket-name"
26+
# blob_name = "your-object-name"
27+
# generation = 1579287380533984
28+
29+
storage_client = storage.Client()
30+
31+
bucket = storage_client.get_bucket(bucket_name)
32+
bucket.delete_blob(blob_name, generation=generation)
33+
print(
34+
"Generation {} of blob {} was deleted from {}".format(
35+
generation, blob_name, bucket_name
36+
)
37+
)
38+
39+
40+
# [END storage_delete_file_archived_generation]
41+
42+
43+
if __name__ == "__main__":
44+
delete_file_archived_generation(
45+
bucket_name=sys.argv[1],
46+
blob_name=sys.argv[2],
47+
generation=sys.argv[3]
48+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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_list_file_archived_generations]
20+
from google.cloud import storage
21+
22+
23+
def list_file_archived_generations(bucket_name):
24+
"""Lists all the blobs in the bucket with generation."""
25+
# bucket_name = "your-bucket-name"
26+
27+
storage_client = storage.Client()
28+
29+
blobs = storage_client.list_blobs(bucket_name)
30+
31+
for blob in blobs:
32+
print("{},{}".format(blob.name, blob.generation))
33+
34+
35+
# [END storage_list_file_archived_generations]
36+
37+
38+
if __name__ == "__main__":
39+
list_file_archived_generations(bucket_name=sys.argv[1])

0 commit comments

Comments
 (0)