Skip to content

Commit adef26e

Browse files
authored
MRG: Merge pull request #494 from octue/fix/add-metadata-to-blob-before-uploading
Add metadata to blobs before uploading to cloud storage
2 parents 39bb252 + 7a45865 commit adef26e

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

octue/cloud/storage/client.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ def upload_file(self, local_path, cloud_path, metadata=None, timeout=_DEFAULT_TI
8383
with open(local_path, "rb") as f:
8484
blob.crc32c = self._compute_crc32c_checksum(f.read())
8585

86+
if metadata:
87+
blob.metadata = self._encode_metadata(metadata)
88+
8689
blob.upload_from_filename(filename=local_path, timeout=timeout)
87-
self._overwrite_blob_custom_metadata(blob, metadata)
8890
logger.debug("Uploaded %r to Google Cloud at %r.", local_path, blob.public_url)
8991

9092
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
99101
"""
100102
blob = self._blob(cloud_path)
101103
blob.crc32c = self._compute_crc32c_checksum(string)
104+
105+
if metadata:
106+
blob.metadata = self._encode_metadata(metadata)
107+
102108
blob.upload_from_string(data=string, timeout=timeout)
103-
self._overwrite_blob_custom_metadata(blob, metadata)
104109
logger.debug("Uploaded data to Google Cloud at %r.", blob.public_url)
105110

106111
def get_metadata(self, cloud_path, timeout=_DEFAULT_TIMEOUT):
@@ -139,15 +144,17 @@ def get_metadata(self, cloud_path, timeout=_DEFAULT_TIMEOUT):
139144
"custom_time": blob.custom_time,
140145
}
141146

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.
144150
145-
:param dict metadata: key-value pairs to set as the new custom metadata
146151
: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
147153
:return None:
148154
"""
149155
blob = self._blob(cloud_path)
150-
self._overwrite_blob_custom_metadata(blob, metadata)
156+
blob.metadata = self._encode_metadata(metadata or {})
157+
blob.patch()
151158

152159
def download_to_file(self, local_path, cloud_path, timeout=_DEFAULT_TIMEOUT):
153160
"""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):
310317
checksum = Checksum(string_or_bytes)
311318
return base64.b64encode(checksum.digest()).decode("utf-8")
312319

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-
326320
def _encode_metadata(self, metadata):
327321
"""Encode metadata as a dictionary of JSON strings.
328322

octue/resources/datafile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def update_cloud_metadata(self):
467467
if not self.cloud_path:
468468
self._raise_cloud_location_error()
469469

470-
GoogleCloudStorageClient().overwrite_custom_metadata(metadata=self.metadata(), cloud_path=self.cloud_path)
470+
GoogleCloudStorageClient().overwrite_custom_metadata(self.cloud_path, metadata=self.metadata())
471471

472472
def update_local_metadata(self):
473473
"""Create or update the local octue metadata file with the datafile's metadata.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "octue"
3-
version = "0.29.4"
3+
version = "0.29.5"
44
description = "A package providing template applications for data services, and a python SDK to the Octue API."
55
readme = "README.md"
66
authors = ["Marcus Lugg <[email protected]>", "Thomas Clark <[email protected]>"]

0 commit comments

Comments
 (0)