File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed
airflow/providers/google/cloud/hooks
tests/providers/google/cloud/hooks Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -447,6 +447,7 @@ def upload(
447
447
chunk_size : Optional [int ] = None ,
448
448
timeout : Optional [int ] = DEFAULT_TIMEOUT ,
449
449
num_max_attempts : int = 1 ,
450
+ metadata : Optional [dict ] = None ,
450
451
) -> None :
451
452
"""
452
453
Uploads a local file or file data as string or bytes to Google Cloud Storage.
@@ -461,6 +462,7 @@ def upload(
461
462
:param chunk_size: Blob chunk size.
462
463
:param timeout: Request timeout in seconds.
463
464
:param num_max_attempts: Number of attempts to try to upload the file.
465
+ :param metadata: The metadata to be uploaded with the file.
464
466
"""
465
467
466
468
def _call_with_retry (f : Callable [[], None ]) -> None :
@@ -493,6 +495,10 @@ def _call_with_retry(f: Callable[[], None]) -> None:
493
495
client = self .get_conn ()
494
496
bucket = client .bucket (bucket_name )
495
497
blob = bucket .blob (blob_name = object_name , chunk_size = chunk_size )
498
+
499
+ if metadata :
500
+ blob .metadata = metadata
501
+
496
502
if filename and data :
497
503
raise ValueError (
498
504
"'filename' and 'data' parameter provided. Please "
Original file line number Diff line number Diff line change @@ -789,15 +789,21 @@ def tearDown(self):
789
789
def test_upload_file (self , mock_service ):
790
790
test_bucket = 'test_bucket'
791
791
test_object = 'test_object'
792
+ metadata = {'key1' : 'val1' , 'key2' : 'key2' }
792
793
793
- upload_method = mock_service .return_value .bucket .return_value .blob .return_value .upload_from_filename
794
+ bucket_mock = mock_service .return_value .bucket
795
+ blob_object = bucket_mock .return_value .blob
794
796
795
- self .gcs_hook .upload (test_bucket , test_object , filename = self .testfile .name )
797
+ upload_method = blob_object .return_value .upload_from_filename
798
+
799
+ self .gcs_hook .upload (test_bucket , test_object , filename = self .testfile .name , metadata = metadata )
796
800
797
801
upload_method .assert_called_once_with (
798
802
filename = self .testfile .name , content_type = 'application/octet-stream' , timeout = 60
799
803
)
800
804
805
+ self .assertEqual (metadata , blob_object .return_value .metadata )
806
+
801
807
@mock .patch (GCS_STRING .format ('GCSHook.get_conn' ))
802
808
def test_upload_file_gzip (self , mock_service ):
803
809
test_bucket = 'test_bucket'
You can’t perform that action at this time.
0 commit comments