Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions storage/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,13 @@ def test_object_get_kms_key(test_bucket):

def test_storage_compose_file(test_bucket):
source_files = ["test_upload_blob_1", "test_upload_blob_2"]
blob_list = []
for source in source_files:
blob = test_bucket.blob(source)
blob.upload_from_string(source)
blob_list.append(blob)

with tempfile.NamedTemporaryFile() as dest_file:
destination = storage_compose_file.compose_file(
test_bucket.name, blob_list, dest_file.name
test_bucket.name, source_files[0], source_files[1], dest_file.name
)
composed = destination.download_as_string()

Expand Down
3 changes: 1 addition & 2 deletions storage/cloud-client/storage_change_file_storage_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

# [START storage_change_file_storage_class]
from google.cloud import storage
from google.cloud.storage import constants


def change_file_storage_class(bucket_name, blob_name):
Expand All @@ -30,7 +29,7 @@ def change_file_storage_class(bucket_name, blob_name):

bucket = storage_client.get_bucket(bucket_name)
blob = bucket.get_blob(blob_name)
blob.update_storage_class(constants.NEARLINE_STORAGE_CLASS)
blob.update_storage_class("NEARLINE")

print(
"Blob {} in bucket {} had its storage class set to {}".format(
Expand Down
17 changes: 12 additions & 5 deletions storage/cloud-client/storage_compose_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@
from google.cloud import storage


def compose_file(bucket_name, sources, destination_blob_name):
def compose_file(bucket_name, first_blob_name, second_blob_name, destination_blob_name):
"""Concatenate source blobs into destination blob."""
# bucket_name = "your-bucket-name"
# sources = [blob_1, blob_2]
# first_blob_name = "first-object-name"
# second_blob_name = "second-blob-name"
# destination_blob_name = "destination-object-name"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
destination = bucket.blob(destination_blob_name)
destination.content_type = "text/plain"

# sources is a list of Blob instances, up to the max of 32 instances per request
sources = [bucket.get_blob(first_blob_name), bucket.get_blob(second_blob_name)]
destination.compose(sources)

print(
"Composed new object {} in the bucket {}".format(
destination_blob_name, bucket.name
"New composite object {} in the bucket {} was created by combining {} and {}".format(
destination_blob_name, bucket_name, first_blob_name, second_blob_name
)
)
return destination
Expand All @@ -44,5 +48,8 @@ def compose_file(bucket_name, sources, destination_blob_name):

if __name__ == "__main__":
compose_file(
bucket_name=sys.argv[1], sources=sys.argv[2], destination_blob_name=sys.argv[3],
bucket_name=sys.argv[1],
first_blob_name=sys.argv[2],
second_blob_name=sys.argv[3],
destination_blob_name=sys.argv[4],
)