diff --git a/app.properties b/app.properties index 60b2dfeb6ff0f8b58f0f577d5476f7236f09da64..ac82217cd542fb3157bbe8829d14f30d92fb35d7 100644 --- a/app.properties +++ b/app.properties @@ -33,4 +33,10 @@ tone_token = xxxx tone_user_name = xxxxx # font -oss_url=http://0.0.0.0:8005 \ No newline at end of file +oss_url=http://0.0.0.0:8005 + +# tone-storage +storage_host = 127.0.0.1 +storage_sftp_port = 22 +storage_user = tonestorage +storage_password = 123456 diff --git a/app/__init__.py b/app/__init__.py index dbc9c41a5d6abeac2aa0d85cd92d66aaf1ebad07..8c1dde41c0e0de6ec95c91eec3d9e0ca663ef54e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,6 +5,7 @@ from app.db import db from app.log import log from app.oss import alg_oss from app.redis import redis +from app.sftp_client import sftp_client from app.setting import STATIC_ROOT, STATICFILES_DIRS, OUTLINE_PATH @@ -13,6 +14,7 @@ def init_app(app): log.init_app(app) db.init_app(app) redis.init_app(app) + sftp_client.init_app(app) create_outline_files() init_blueprints(app) diff --git a/app/sftp_client.py b/app/sftp_client.py new file mode 100644 index 0000000000000000000000000000000000000000..cbae59184b81c46d36a0b50608ac8e38ae366de1 --- /dev/null +++ b/app/sftp_client.py @@ -0,0 +1,71 @@ +import logging + +import paramiko + +logger = logging.getLogger('sftp') + + +class SFTPClient(object): + + def init_app(self, app): + self.host = app.config['STORAGE_HOST'] + self.port = app.config['STORAGE_SFTP_PORT'] + self.user = app.config['STORAGE_USER'] + self.password = app.config['STORAGE_PASSWORD'] + + def upload_file(self, local_path, server_path, timeout=10): + """ + 上传文件,注意:不支持文件夹 + :param server_path: 远程路径,比如:/home/sdn/tmp.txt + :param local_path: 本地路径,比如:D:/text.txt + :param timeout: 超时时间(默认),必须是int类型 + :return: bool + """ + try: + t = paramiko.Transport((self.host, self.port)) + t.connect(username=self.user, password=self.password) + sftp = paramiko.SFTPClient.from_transport(t) + sftp.banner_timeout = timeout + self._mkdirs(server_path, sftp) + sftp.put(local_path, server_path) + t.close() + return True + except Exception as e: + logger.error(f'sftp upload file[{local_path}] failed! error:{e}') + return False + + def down_file(self, server_path, local_path, timeout=10): + """ + 下载文件,注意:不支持文件夹 + :param server_path: 远程路径,比如:/home/sdn/tmp.txt + :param local_path: 本地路径,比如:D:/text.txt + :param timeout: 超时时间(默认),必须是int类型 + :return: bool + """ + try: + t = paramiko.Transport((self.host, 22)) + t.banner_timeout = timeout + t.connect(username=self.user, password=self.password) + sftp = paramiko.SFTPClient.from_transport(t) + sftp.get(server_path, local_path, sftp) + t.close() + return True + except Exception as e: + logger.error(f'sftp download file[{local_path}] failed! error:{e}') + return False + + @staticmethod + def _mkdirs(server_path, sftp): + path_list = server_path.split("/")[0:-1] + for path_item in path_list: + if not path_item: + continue + try: + sftp.chdir(path_item) + except Exception as e: + logger.info(f'The directory does not exist, now create it.[{e}]') + sftp.mkdir(path_item) + sftp.chdir(path_item) + + +sftp_client = SFTPClient() diff --git a/services/tone_job_service.py b/services/tone_job_service.py index 476cb61d0eeea7e2bdaefdb1fe77b0352ef47114..84f192817dd601ba6872647fecf250d0b408c78e 100644 --- a/services/tone_job_service.py +++ b/services/tone_job_service.py @@ -11,7 +11,7 @@ from models.plan_model import update_plan_status from services.const import ERROR_UN_EXISTED_TONE_JOB from common.tone.tone_request import get_res_from_tone from common.tone.api import TONE_JOB_QUERY, TONE_CREATE_JOB -from app.oss import alg_oss +from app.sftp_client import sftp_client async def create_job(data, task_id): @@ -129,10 +129,9 @@ async def parse_result_file(res_file, job_id): index = res_file.find('/' + str(job_id)) oss_file = res_file[index + 1:] filepath = 'common/static/' + str(uuid.uuid4()) + '.json' - if alg_oss.file_exists(oss_file): - alg_oss.get_object_to_file(oss_file, filepath) - with open(filepath, 'r') as f: - return json.load(f) + sftp_client.down_file(oss_file, filepath) + with open(filepath, 'r') as f: + return json.load(f) return None