def write(self, data, remote_path, mode='w+b', overwrite=False, log_level=0, encoding='utf8'): # Will raise an exception or delete the remote location, depending on what is needed self._overwrite_if_needed(remote_path, overwrite, log_level) # Data to be written must be always bytes data = data if isinstance(data, bytes) else data.encode(encoding) # A temporary file to write data to .. with NamedTemporaryFile(mode, suffix='zato-sftp-write.txt') as local_path: # .. wrap the file in separate thread so as not to block the event loop. thread_file = FileObjectThread(local_path, mode=mode) thread_file.write(data) thread_file.flush() try: # Data written out, we can now upload it to the remote location self.upload(local_path.name, remote_path, False, overwrite, log_level, False) except Exception: logger.warn('Exception in SFTP write method `%s`', format_exc()) finally: # Now we can close the file too thread_file.close()