# This code is a Qiskit project.## (C) Copyright IBM 2023.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals."""===========================================================Storage utilities (:mod:`qiskit_serverless.utils.storage`)===========================================================.. currentmodule:: qiskit_serverless.utils.storageQiskit Serverless storage utilities====================================.. autosummary:: :toctree: ../stubs/ PersistentStorage"""fromioimportBytesIOimportosfromtypingimportOptionalfromabcimportabstractmethodimports3fs
[docs]classBaseStorage:"""Base class for persistent storage."""@abstractmethoddefsave(self,path:str,data:BytesIO):"""Save data."""raiseNotImplementedError@abstractmethoddefload(self,path:str):"""Load data."""raiseNotImplementedError
[docs]classS3Storage(BaseStorage):"""Class for storing s3 objects in a non-temporary manner."""
[docs]def__init__(self,endpoint:str,bucket:str,key:Optional[str]=None,secret:Optional[str]=None,):"""Long-term storage for serverless computation."""self.endpoint=endpointself.bucket=bucketself.key=keyoros.getenv("AWS_ACCESS_KEY")self.secret=secretoros.getenv("AWS_SECRET_ACCESS_KEY")self.storage=s3fs.core.S3FileSystem(endpoint_url=self.endpoint,key=self.key,secret=self.secret)
defsave(self,path,data):"""Store binary data in persistent storage."""withself.storage.open(f"{self.bucket}/{path}","wb")asf:f.write(data)defload(self,path):"""Get binary data from persistent storage."""withself.storage.open(f"{self.bucket}/{path}","rb")asf:print(f.read())