@@ -83,8 +83,10 @@ def upload_file(self, local_path, cloud_path, metadata=None, timeout=_DEFAULT_TI
83
83
with open (local_path , "rb" ) as f :
84
84
blob .crc32c = self ._compute_crc32c_checksum (f .read ())
85
85
86
+ if metadata :
87
+ blob .metadata = self ._encode_metadata (metadata )
88
+
86
89
blob .upload_from_filename (filename = local_path , timeout = timeout )
87
- self ._overwrite_blob_custom_metadata (blob , metadata )
88
90
logger .debug ("Uploaded %r to Google Cloud at %r." , local_path , blob .public_url )
89
91
90
92
def upload_from_string (self , string , cloud_path , metadata = None , timeout = _DEFAULT_TIMEOUT ):
@@ -99,8 +101,11 @@ def upload_from_string(self, string, cloud_path, metadata=None, timeout=_DEFAULT
99
101
"""
100
102
blob = self ._blob (cloud_path )
101
103
blob .crc32c = self ._compute_crc32c_checksum (string )
104
+
105
+ if metadata :
106
+ blob .metadata = self ._encode_metadata (metadata )
107
+
102
108
blob .upload_from_string (data = string , timeout = timeout )
103
- self ._overwrite_blob_custom_metadata (blob , metadata )
104
109
logger .debug ("Uploaded data to Google Cloud at %r." , blob .public_url )
105
110
106
111
def get_metadata (self , cloud_path , timeout = _DEFAULT_TIMEOUT ):
@@ -139,15 +144,17 @@ def get_metadata(self, cloud_path, timeout=_DEFAULT_TIMEOUT):
139
144
"custom_time" : blob .custom_time ,
140
145
}
141
146
142
- def overwrite_custom_metadata (self , metadata , cloud_path ):
143
- """Overwrite the custom metadata for the given cloud file.
147
+ def overwrite_custom_metadata (self , cloud_path , metadata = None ):
148
+ """Overwrite the custom metadata for the given cloud file. If no metadata is given, the custom metadata is
149
+ erased.
144
150
145
- :param dict metadata: key-value pairs to set as the new custom metadata
146
151
:param str cloud_path: full cloud path to file (e.g. `gs://bucket_name/path/to/file.csv`)
152
+ :param dict|None metadata: key-value pairs to set as the new custom metadata
147
153
:return None:
148
154
"""
149
155
blob = self ._blob (cloud_path )
150
- self ._overwrite_blob_custom_metadata (blob , metadata )
156
+ blob .metadata = self ._encode_metadata (metadata or {})
157
+ blob .patch ()
151
158
152
159
def download_to_file (self , local_path , cloud_path , timeout = _DEFAULT_TIMEOUT ):
153
160
"""Download a file to a file from a Google Cloud bucket at gs://<bucket_name>/<path_in_bucket>.
@@ -310,19 +317,6 @@ def _compute_crc32c_checksum(self, string_or_bytes):
310
317
checksum = Checksum (string_or_bytes )
311
318
return base64 .b64encode (checksum .digest ()).decode ("utf-8" )
312
319
313
- def _overwrite_blob_custom_metadata (self , blob , metadata ):
314
- """Overwrite the custom metadata for the given blob. Note that this is synced up with Google Cloud.
315
-
316
- :param google.cloud.storage.blob.Blob blob: Google Cloud Storage blob to update
317
- :param dict metadata: key-value pairs of metadata to overwrite the blob's metadata with
318
- :return None:
319
- """
320
- if not metadata :
321
- return None
322
-
323
- blob .metadata = self ._encode_metadata (metadata )
324
- blob .patch ()
325
-
326
320
def _encode_metadata (self , metadata ):
327
321
"""Encode metadata as a dictionary of JSON strings.
328
322
0 commit comments