def __init__(self, blob_host_uri): """Initialize GridFS blob host. Args: blob_host_uri: MongoDB uri (mongodb://username:password@host:port/database) """ BLOBHost.__init__(self, blob_host_uri) try: # connect to mongo instance self.client = MongoClient( self.blob_host_uri, serverSelectionTimeoutMS=CONNECTION_TIMEOUT) # check connection self.client.server_info() # get database name from uri database_name = blob_host_uri.split("/")[-1] # connect to database self.database = self.client[database_name] # connect to gridfs self.fs = gridfs.GridFS(self.database) except Exception as e: raise BLOBError( "Unable to create the connection to the GridFS collection: %s" % str(e))
def setUpClass(cls): blob_host_factory = BLOBHostFactory('GridFS', MONGODB_URI) cls.blob_host = blob_host_factory.create_blob_host() if len(list(cls.blob_host.fs.find())) != 0: raise BLOBError( 'Unable to perform the tests if the database is not empty.')
def get(self, handle): """Returns a blob Args: handle: blob id Returns: """ try: if self.fs.exists(ObjectId(handle)): with self.fs.get(ObjectId(handle)) as blob: return blob.read() else: raise BLOBError('No file found for the given id.') except: raise BLOBError( 'An unexpected error occurred while retrieving the file.')
def _create_gridfs_host(self): """Returns a GridFS host Returns: """ if self.blob_host_uri is not None: return GridFSBLOBHost(self.blob_host_uri) else: raise BLOBError('BLOB_HOST_URI should be set.')
def _create_dspace_host(self): """Returns a DSpace host Returns: """ if self.blob_host_uri is not None and self.blob_host_user is not None and self.blob_host_password is not None: return DSpaceBLOBHost(self.blob_host_uri, self.blob_host_user, self.blob_host_password) else: raise BLOBError('BLOB_HOST_URI, BLOB_HOST_USER and BLOB_HOST_PASSWORD should be set.')
def create_blob_host(self): """Returns a blob host Returns: """ if self.blob_host is not None and self.blob_host in self.BLOB_HOSTS: if self.blob_host == self.BLOB_HOSTS[0]: return self._create_dspace_host() elif self.blob_host == self.BLOB_HOSTS[1]: return self._create_gridfs_host() else: raise BLOBError('BLOB_HOST should take a value in ' + str(self.BLOB_HOSTS))
def get(self, handle): """Return a blob with the given handle. Args: handle(str): ObjectId of the Blob object to retrieve. Returns: bytes: the content of the file stored with the given Blob id. Raises: BlobError: if the object does not exists or an unexpected error occurs. """ try: if not self.fs.exists(ObjectId(handle)): raise BLOBError("No file found with the given ID.") with self.fs.get(ObjectId(handle)) as blob: return blob.read() except Exception as exc: raise BLOBError( f"An unexpected error occurred while retrieving the file: {str(exc)}" )
def delete(self, handle): """Deletes a blob Args: handle: blob id Returns: """ try: if self.fs.exists(ObjectId(handle)): self.fs.delete(ObjectId(handle)) except: raise BLOBError("An error occurred while deleting the file.")
def delete(self, handle): """Delete a blob Args: handle: blob id Returns: """ try: if self.fs.exists(ObjectId(handle)): self.fs.delete(ObjectId(handle)) except Exception as exc: raise BLOBError( f"An error occurred while deleting the file: {str(exc)}")
def save(self, blob, filename=None, metadata=None): """Saves a blob Args: blob: filename: metadata: Returns: """ try: if metadata is None: metadata = dict() blob_id = self.fs.put(blob, filename=filename, metadata=metadata) except Exception, e: raise BLOBError("An error occurred while saving the file.")
def save(self, blob, filename=None, metadata=None): """Save a blob. Args: blob: filename: metadata: Returns: """ try: if metadata is None: metadata = dict() blob_id = self.fs.put(blob, filename=filename, metadata=metadata) except Exception as exc: raise BLOBError( f"An error occurred while saving the file: {str(exc)}") return blob_id