From 52fc5e9b2f3ceb2b65b9a4b068359425b9658a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AD=A6=E5=B3=B0?= Date: Thu, 29 Sep 2022 15:33:21 +0800 Subject: [PATCH 1/2] feature: add sftp utils --- app/__init__.py | 2 + app/sftp_client.py | 71 ++++++++++++++++++++++++++++++++++++ services/tone_job_service.py | 9 ++--- 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 app/sftp_client.py diff --git a/app/__init__.py b/app/__init__.py index dbc9c41..8c1dde4 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 0000000..cbae591 --- /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 476cb61..84f1928 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 -- Gitee From eb133eb4b2acf4e87d1d8e451b41d574af9efaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AD=A6=E5=B3=B0?= Date: Thu, 29 Sep 2022 15:52:23 +0800 Subject: [PATCH 2/2] feature: add config for ftp --- app.properties | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app.properties b/app.properties index 60b2dfe..ac82217 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 -- Gitee