import os import shutil import paramiko import argparse import Contant from LoggerUtils import Logger, initLogger import configparser import requests import time import json def upload(path, remote): pass if __name__ == "__main__": # 读取配置文件 with open('sftp_config.json', 'r', encoding='utf-8') as f: # 使用json.load()方法读取文件内容 data = json.load(f) # 初始化日志 Contant.logDir = data['log']['dir'] Contant.logFileName = data['log']['fileName'] initLogger(Contant.logDir, Contant.logFileName) # 设置sftp client hostname = data['sftp']['hostname'] port = data['sftp']['port'] username = data['sftp']['username'] password = data['sftp']['password'] Logger.info("host:{},port:{},username:{},password:{}".format( hostname, port, username, password)) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) sftp_client = None # 设置默认值 ssh_client.connect(hostname, port, username, password) # 创建SFTP客户端 sftp_client = ssh_client.open_sftp() Logger.info("SFTP客户端已经建立:{}".format(sftp_client)) remote_root = "/Inbound/YouTube Captions" local_root = data['sftp']['local'] Logger.info("remote_root:{},local_root:{}".format(remote_root, local_root)) # 遍历所有文件,然后上传 region_names = os.listdir(local_root) for region_name in region_names: # sftp创建region文件夹 try: sftp_client.chdir(remote_root + "/" + region_name) except BaseException: Logger.info(f"create region dir: {remote_root}/{region_name}") sftp_client.mkdir(remote_root + "/" + region_name) sftp_client.chdir(remote_root + "/" + region_name) channel_names = os.listdir(f"{local_root}/{region_name}") for channel_name in channel_names: # sftp创建channel文件夹 try: sftp_client.chdir(f"{remote_root}/{region_name}/{channel_name}") except BaseException: Logger.info(f"create channel dir: {remote_root}/{region_name}/{channel_name}") sftp_client.mkdir(f"{remote_root}/{region_name}/{channel_name}") sftp_client.chdir(f"{remote_root}/{region_name}/{channel_name}") # 获取本地文件夹 srt_names = os.listdir(f"{local_root}/{region_name}/{channel_name}") for srt_name in srt_names: # 获取远程文件路径以及本地文件路径 srt_path = f"{local_root}/{region_name}/{channel_name}/{srt_name}" remote_path = f"{remote_root}/{region_name}/{channel_name}/{srt_name}" # 如果远程文件存在,则进行删除 try: sftp_client.stat(remote_path) # 如果文件存在,删除它 sftp_client.remove(remote_path) Logger.info("Remote file '{}' deleted.".format(remote_path)) except FileNotFoundError: Logger.info("Remote file '{}' not found.".format(remote_path)) # 上传本地文件 try: # 判断本地文件是否存在,存在则上传 if os.path.exists(srt_path): Logger.info("本地文件 '{}' 存在,开始上传.".format(srt_path)) sftp_client.put(srt_path, remote_path, confirm=False) os.remove(srt_path) else: Logger.info("本地文件 '{}' 不存在,无法上传.".format(srt_path)) except Exception as e: Logger.info("上传失败 '{}' 文件名长度{}".format( remote_path, len(remote_path))) Logger.error(e) sftp_client.close() sftp_client = ssh_client.open_sftp()