Browse Source

新增sftp上传

master^2
zhangshu 5 months ago
parent
commit
56ac25a5bf
  1. 56
      sftp.py

56
sftp.py

@ -9,9 +9,14 @@ import requests
import time
import json
def upload(path, remote):
pass
if __name__ == "__main__":
# 读取配置文件
with open('search_video_config.json', 'r', encoding='utf-8') as f:
with open('sftp_config.json', 'r', encoding='utf-8') as f:
# 使用json.load()方法读取文件内容
data = json.load(f)
# 初始化日志
@ -41,5 +46,50 @@ if __name__ == "__main__":
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()
Loading…
Cancel
Save