How to confirm SFTP file delivery?
All you can do is to check that there are no errors, when uploading the file. That's all information the SFTP server gives you.
With command-line OpenSSH sftp
client, you can check its exit code (you need to use the -b
switch).
echo "put file.txt" | sftp -b - user@host
if [ $? -eq 0 ]
then
echo "File uploaded"
else
echo "File NOT uploaded"
fi
See also How to perform checksums during a SFTP file transfer for data integrity?
It's perfectly possible that the SFTP server does not allow you to download a file that you have just uploaded.
There are two common reasons for such behavior:
- Public "upload" directory. This is to prevent you from downloading other user's files.
- There's some process that immediately picks uploaded file for some processing.
How to verify a successful upload if I use pysftp to transfer a file?
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?
How to detect that a file is being uploaded over FTP
There's no generic solution to this problem.
Some FTP servers lock the file being uploaded, preventing you from accessing it, while the file is still being uploaded. For example IIS FTP server does that. Most other FTP servers do not. See my answer at Prevent file from being accessed as it's being uploaded.
There are some common workarounds to the problem (originally posted in SFTP file lock mechanism, but relevant for the FTP too):
You can have the client upload a "done" file once the upload finishes. Make your automated system wait for the "done" file to appear.
You can have a dedicated "upload" folder and have the client (atomically) move the uploaded file to a "done" folder. Make your automated system look to the "done" folder only.
Have a file naming convention for files being uploaded (".filepart") and have the client (atomically) rename the file after upload to its final name. Make your automated system ignore the ".filepart" files.
See (my) article Locking files while uploading / Upload to temporary file name for an example of implementing this approach.
Also, some FTP servers have this functionality built-in. For example ProFTPD with its
HiddenStores
directive.A gross hack is to periodically check for file attributes (size and time) and consider the upload finished, if the attributes have not changed for some time interval.
You can also make use of the fact that some file formats have clear end-of-the-file marker (like XML or ZIP). So you know, that the file is incomplete.
Some FTP servers allow you to configure a hook to be called, when an upload is finished. You can make use of that. For example ProFTPD has a mod_exec
module (see the ExecOnCommand
directive).
Related Topics
Memory Access Error Sys_Rt_Sigaction (Signal Handler)
Find -Exec Cmd {} + VS | Xargs
Check If Directory Mounted with Bash
Is There Any Modern Review of Solutions to the 10000 Client/Sec Problem
Limit on File Name Length in Bash
Set-Up X11 Forwarding Over Ssh
Ld_Library_Path Doesn't Seem to Work
When "Vagrant Up" It Says "It Appears Your Machine Doesn't Support Nfs" (Debian Jessie)
Conda.Exe: Error While Loading Shared Libraries: Libz.So.1
Valgrind Does Not Work with Wsl
How to Remove Folders with a Certain Name
What's an Alternative for Dtrace on Linux
How to Create a Callback for "Monitor Plugged" on an Intel Graphics
How to Copy Multiple Files from a Different Directory Using Cp
What Is the Concept of Vruntime in Cfs
How to Use Iptables in Linux to Forward Http and Https Traffic to a Transparent Proxy