5454
5555_STRING_CONTENT = "hello world"
5656_BYTE_CONTENT = b"12345678"
57+ _RESUMABLE_UPLOAD_CHUNK_SIZE = 2 * 1024 * 1024
5758
5859
5960########################################################################################################################################
@@ -461,7 +462,7 @@ def blob_upload_from_string(client, _preconditions, **resources):
461462 bucket = resources .get ("bucket" )
462463 _ , data = resources .get ("file_data" )
463464 blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
464- blob .chunk_size = 4 * 1024 * 1024
465+ blob .chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE
465466 if _preconditions :
466467 blob .upload_from_string (data , if_generation_match = 0 )
467468 else :
@@ -471,26 +472,67 @@ def blob_upload_from_string(client, _preconditions, **resources):
471472
472473def blob_upload_from_file (client , _preconditions , ** resources ):
473474 bucket = resources .get ("bucket" )
474- blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
475+ file , data = resources .get ("file_data" )
476+ file_blob = client .bucket (bucket .name ).blob (file .name )
477+ upload_blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
478+ upload_blob .chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE
479+
475480 with tempfile .NamedTemporaryFile () as temp_f :
481+ # Create a named temporary file with payload.
482+ with open (temp_f .name , "wb" ) as file_obj :
483+ client .download_blob_to_file (file_blob , file_obj )
484+ # Upload the temporary file and assert data integrity.
476485 if _preconditions :
477- blob .upload_from_file (temp_f , if_generation_match = 0 )
486+ upload_blob .upload_from_file (temp_f , if_generation_match = 0 )
478487 else :
479- blob .upload_from_file (temp_f )
488+ upload_blob .upload_from_file (temp_f )
489+
490+ upload_blob .reload ()
491+ assert upload_blob .size == len (data )
480492
481493
482494def blob_upload_from_filename (client , _preconditions , ** resources ):
483495 bucket = resources .get ("bucket" )
484496 blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
497+ blob .chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE
498+
499+ bucket = resources .get ("bucket" )
500+ file , data = resources .get ("file_data" )
501+ file_blob = client .bucket (bucket .name ).blob (file .name )
502+ upload_blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
503+ upload_blob .chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE
485504
486505 with tempfile .NamedTemporaryFile () as temp_f :
506+ # Create a named temporary file with payload.
507+ with open (temp_f .name , "wb" ) as file_obj :
508+ client .download_blob_to_file (file_blob , file_obj )
509+ # Upload the temporary file and assert data integrity.
487510 if _preconditions :
488- blob .upload_from_filename (temp_f .name , if_generation_match = 0 )
511+ upload_blob .upload_from_filename (temp_f .name , if_generation_match = 0 )
489512 else :
490- blob .upload_from_filename (temp_f .name )
513+ upload_blob .upload_from_filename (temp_f .name )
514+
515+ upload_blob .reload ()
516+ assert upload_blob .size == len (data )
491517
492518
493519def blobwriter_write (client , _preconditions , ** resources ):
520+ bucket = resources .get ("bucket" )
521+ _ , data = resources .get ("file_data" )
522+ blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
523+ if _preconditions :
524+ with blob .open (
525+ "w" , chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE , if_generation_match = 0
526+ ) as writer :
527+ writer .write (data )
528+ else :
529+ with blob .open ("w" , chunk_size = _RESUMABLE_UPLOAD_CHUNK_SIZE ) as writer :
530+ writer .write (data )
531+ blob .reload ()
532+ assert blob .size == len (data )
533+
534+
535+ def blobwriter_write_multipart (client , _preconditions , ** resources ):
494536 chunk_size = 256 * 1024
495537 bucket = resources .get ("bucket" )
496538 blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
@@ -502,6 +544,15 @@ def blobwriter_write(client, _preconditions, **resources):
502544 writer .write (_BYTE_CONTENT )
503545
504546
547+ def blob_upload_from_string_multipart (client , _preconditions , ** resources ):
548+ bucket = resources .get ("bucket" )
549+ blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
550+ if _preconditions :
551+ blob .upload_from_string (_STRING_CONTENT , if_generation_match = 0 )
552+ else :
553+ blob .upload_from_string (_STRING_CONTENT )
554+
555+
505556def blob_create_resumable_upload_session (client , _preconditions , ** resources ):
506557 bucket = resources .get ("bucket" )
507558 blob = client .bucket (bucket .name ).blob (uuid .uuid4 ().hex )
@@ -745,11 +796,15 @@ def object_acl_clear(client, _preconditions, **resources):
745796 bucket_rename_blob ,
746797 ],
747798 "storage.objects.insert" : [
799+ blob_upload_from_string_multipart ,
800+ blobwriter_write_multipart ,
801+ blob_create_resumable_upload_session ,
802+ ],
803+ "storage.resumable.upload" : [
748804 blob_upload_from_string ,
749805 blob_upload_from_file ,
750806 blob_upload_from_filename ,
751807 blobwriter_write ,
752- blob_create_resumable_upload_session ,
753808 ],
754809 "storage.objects.patch" : [
755810 blob_patch ,
0 commit comments