将文件从 Ubuntu 复制到 SFTP 服务器(Windows 11),不包括现有文件

问题描述 投票:0回答:1

我在 Windows 11 Pro 计算机上安装了 SFTP 服务器。此外,我有一台位于不同位置和网络的 Ubuntu 20.04 计算机。我可以使用其公共 IP 地址访问 SFTP 服务器,并且我已在路由器中设置了端口转发。

挑战是我无法连接到 Ubuntu 机器。每天,这台 Ubuntu 机器都会生成我想要每 1 分钟复制到 SFTP 服务器的文件。我使用 paramiko 库开发了一个 Python 脚本,将 Ubuntu 中的文件夹同步到 SFTP 服务器。该脚本扫描 Ubuntu 上的文件并检查 SFTP 服务器上已存在哪些文件以排除它们。

但是,随着时间的推移,文件数量会增加,扫描时间也会增长。我知道 rsync,但它需要在 Windows 计算机上安装(目前对我来说不可能)。

这是我扫描文件上传到SFTP服务器的代码(仅新的和更新的文件):

def get_files_for_uploading(local_folder: str, remote_folder: str, sftp: paramiko.SFTPClient) -> List[dict]:
    """ Returns list of files for uploading"""
    # Get all files in the local folder
    all_files = glob.glob(os.path.join(local_folder, "**", "*"), recursive=True)
    all_files = [file for file in all_files if os.path.isfile(file)]
    files_for_uploading = []
    for file in all_files:
        relative_path = os.path.relpath(file, local_folder)
        remote_file_path = os.path.join(remote_folder, relative_path)
        row = {
            "source": file,
            "destination": remote_file_path,
        }
        try:
            # Check if the remote file exists and has the same size
            remote_file_stat = sftp.stat(remote_file_path)
            if os.path.getsize(file) != remote_file_stat.st_size:
                files_for_uploading.append(row)
        except FileNotFoundError:
            files_for_uploading.append(row)
    return files_for_uploading

我该如何改进这个功能,让它运行得更快?

感谢您的帮助。预先感谢!

** 我看到如果在 Ubuntu 上安装了 rsync 而在 SFTP 服务器(Windows)上没有安装,那么我收到此错误:

python sftp paramiko
1个回答
0
投票

我找到了如何在 Windows 11/10 上安装 rsync 而不安装 wsl。我需要安装 cygwin,然后安装 rsync:cygwin

© www.soinside.com 2019 - 2024. All rights reserved.