2017-10-17 235 views
0

我正在使用以下脚本在SFTP服务器上上传zip文件。虽然我看到服务器上的文件,但它始终显示它的0 KB使用chilkat将zip文件上载到SFTP服务器python

#Code to upload file to a SFTP server abc.com 
import chilkat 
sftp = chilkat.CkSFtp() 
success = sftp.UnlockComponent("Anything trial") 
puttyKey = chilkat.CkSshKey() 
ppkText = puttyKey.loadText("xyz.ppk") 
success = puttyKey.FromPuttyPrivateKey(ppkText) 
sshHostname = "abc.com" 
sshPort = 22 
success = sftp.Connect(sshHostname,sshPort) 
sftp.AuthenticatePwPk("username", "password", puttyKey) 
success = sftp.InitializeSftp() 
filename = "file.zip" 
handle = sftp.openFile(filename ,"writeOnly","createTruncate") 
success = sftp.UploadFile(handle,"file.zip") 
success = sftp.CloseHandle(handle) 

回答

0

你不检查呼叫成功返回值。如果openFile和CloseHandle均成功,但UploadFile失败,则结果将是服务器上的长度为0的文件。

您传递了一个没有UploadFile路径的文件名。这意味着你正尝试从应用程序的当前工作目录上传文件“file.zip”,无论如何。我怀疑“file.zip”实际上并不在你应用的当前工作目录中。您可以指定完整的绝对路径或相对路径,而不是“file.zip”。例如,“c:\ someDir \ file.zip”。另外,如果在检查方法调用的成功/失败后发现失败,请检查对象的LastErrorText属性(sftp.LastErrorText)的内容。它将提供有关方法调用中发生的事件的详细信息。

+0

另外..传递给OpenFile的文件名是要在服务器上创建的文件名。传递给UploadFile的文件路径是本地文件路径。 – Matt

相关问题