Search code examples
pythonsftppysftp

How to verify a successful upload if I use pysftp to transfer a file?


I am using pysftp to transfer files over network. I want to ensure reliability in this process. There's a put API in pysftp and it returns a SFTPAttributes object containing size of the transferred files. Is that enough to verify a successful transfer, I mean by comparing the size.

Or... it seems to me that I can also open the transferred file and use check method... Is it the correct way? I am just a little confused. Thank you.


Solution

  • Pysftp Connection.put already checks the size of the uploaded file. That's what the confirm=True parameter is for, You do not need to do anything more:

    whether to do a stat() on the file afterwards to confirm the file size

    While you can theoretically verify the checksum with SFTPFile.check, it won't typically work, as most SFTP servers, including the widespread OpenSSH, do not support calculating checksums. So the call will fail. You would have to resort to running some shell command to calculate the checksum. See:
    Comparing MD5 of downloaded files against files on an SFTP server in Python

    But it's questionable whether it is worth the effort, see:
    How to perform checksums during a SFTP file transfer for data integrity?


    Though these days, you should not use pysftp, as it is dead. Use Paramiko directly instead. See pysftp vs. Paramiko. See basically the same question about Paramiko: How to check if Paramiko successfully uploaded a file to an SFTP server?