Skip to content
Snippets Groups Projects
Commit 1bd3943f authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added metadata and hash functions to files.py

parent fec088cf
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,8 @@ from minio import Minio ...@@ -43,6 +43,8 @@ from minio import Minio
import minio.error import minio.error
import logging import logging
import itertools import itertools
import hashlib
import base64
import nomad.config as config import nomad.config as config
...@@ -160,6 +162,7 @@ class Upload(): ...@@ -160,6 +162,7 @@ class Upload():
upload_file: The path of the tmp version of this file for an open upload. upload_file: The path of the tmp version of this file for an open upload.
upload_extract_dir: The path of the tmp directory with the extracted contents. upload_extract_dir: The path of the tmp directory with the extracted contents.
filelist: A list of filenames relative to the .zipped upload root. filelist: A list of filenames relative to the .zipped upload root.
metadata: The upload object storage metadata.
""" """
def __init__(self, upload_id: str) -> None: def __init__(self, upload_id: str) -> None:
self.upload_id = upload_id self.upload_id = upload_id
...@@ -168,7 +171,7 @@ class Upload(): ...@@ -168,7 +171,7 @@ class Upload():
self.filelist: List[str] = None self.filelist: List[str] = None
try: try:
_client.stat_object(config.s3.uploads_bucket, upload_id) self.metadata = _client.stat_object(config.s3.uploads_bucket, upload_id).metadata
except minio.error.NoSuchKey: except minio.error.NoSuchKey:
raise KeyError(self.upload_id) raise KeyError(self.upload_id)
...@@ -187,11 +190,14 @@ class Upload(): ...@@ -187,11 +190,14 @@ class Upload():
return wrapper return wrapper
@Decorators.handle_errors @Decorators.handle_errors
def metadata(self): def hash(self) -> str:
try: """ Calculates the first 28 bytes of a websafe base64 encoded SHA512 of the upload. """
return _client.stat_object(config.s3.uploads_bucket, self.upload_id).metadata hash = hashlib.sha512()
except minio.error.NoSuchKey: with open(self.upload_file, 'rb') as f:
raise KeyError(self.upload_id) for data in iter(lambda: f.read(65536), b''):
hash.update(data)
return base64.b64encode(hash.digest(), altchars=b'-_')[0:28].decode('utf-8')
@Decorators.handle_errors @Decorators.handle_errors
def open(self) -> None: def open(self) -> None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment