我正试图使用pysftp将文件从bucket发送到FTP服务器。
为此,我使用了带有python 3.7 的谷歌云功能
我尝试过很多不同的方法,但总是会出错。
1.-以字符串形式下载文件:在这个例子中,为了避免文件内容出错,我将创建一个包含"测试字符串"的文件,而不是使用桶文件中的内容
def sftp_handler():
myHostname = "hotsname.com"
myUsername = "username"
myPassword = "pass"
try:
keydata = b"""AAAAB..."""
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('hotname.com', 'ssh-rsa', key)
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,port=22,cnopts=cnopts) as sftp:
print ("Connection succesfully stablished ... ")
remoteFilePath = '/dir1/dir2/dir3/'
sftp.putfo(StringIO('test string'), os.path.join(remoteFilePath, "OC.csv"))
# connection closed automatically at the end of the with-block
except:
print("Unexpected error in sftp_handler:")
traceback.print_exc()
错误输出(堆栈驱动程序日志(
Traceback (most recent call last): E undefined
File "/user_code/main.py", line 36, in sftp_handler E
sftp.putfo(StringIO('xml string'), os.path.join(remoteFilePath, "OC.csv")) E
File "/env/local/lib/python3.7/site-packages/pysftp/__init__.py", line 474, in putfo E
callback=callback, confirm=confirm) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 714, in putfo E
with self.file(remotepath, "wb") as fr: E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 372, in open E
t, msg = self._request(CMD_OPEN, filename, imode, attrblock) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request E
return self._read_response(num) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response E
self._convert_status(msg) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 898, in _convert_status E
raise IOError(text)
OSError E
2.-使用一个临时文件,我将在其中写入bucket 中的文件内容
<adding this to the previous code>
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf:
tf.write(b'Hello world!')
remoteFilePath = '/dir1/dir2/dir3/'
sftp.put(os.path.dirname(tf.name), os.path.join(remoteFilePath, "OC.csv"))
错误日志:
Traceback (most recent call last): E
File "/user_code/main.py", line 40, in sftp_handler E
sftp.put(os.path.dirname(tf.name), os.path.join(remoteFilePath, "OC.csv")) E
File "/env/local/lib/python3.7/site-packages/pysftp/__init__.py", line 364, in put E
confirm=confirm) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 758, in put E
with open(localpath, "rb") as fl: E
IsADirectoryError: [Errno 21] Is a directory: '/tmp' E
3.-正如马丁在回答中所建议的那样:
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,port=22,cnopts=cnopts) as sftp:
print ("Connection succesfully stablished ... ")
remoteFilePath = '/dir1/dir2/dir3/'
sftp.putfo(StringIO('test string'), remoteFilePath + "OC.csv")
我得到以下错误:
Traceback (most recent call last): E
File "/user_code/main.py", line 36, in sftp_handler E
sftp.putfo(StringIO('test string'), remoteFilePath + "OC.csv") E
File "/env/local/lib/python3.7/site-packages/pysftp/__init__.py", line 474, in putfo E
callback=callback, confirm=confirm) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 714, in putfo E
with self.file(remotepath, "wb") as fr: E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 372, in open E
t, msg = self._request(CMD_OPEN, filename, imode, attrblock) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request E
return self._read_response(num) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response E
self._convert_status(msg) E
File "/env/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 898, in _convert_status E
raise IOError(text) E
OSError E
4.-从bucket下载文件,在执行云功能时创建一个文件,并将此文件发送到sftp(尚未实现,因为我不确定它是否能在云功能中创建文件(
- 我该怎么做
- 为什么会失败
注意:我也尝试过使用FileZilla,但我也有一个错误,所以这应该与FTP服务器有关:
09:00:46 Status: Connecting to sftp-staging.messagesnetwork.com...
09:01:06 Status: Connected to sftp-staging.messagesnetwork.com
09:01:06 Status: Retrieving directory listing...
09:01:06 Status: Listing directory /
09:01:07 Status: Directory listing of "/" successful
09:01:11 Status: Retrieving directory listing of "/folder1"...
09:01:37 Command: cd "folder1"
09:01:37 Response: New directory is: "/folder1"
09:01:37 Command: ls
09:01:37 Error: Connection timed out after 20 seconds of inactivity
09:01:38 Error: Failed to retrieve directory listing
09:01:38 Status: Disconnected from server
09:01:38 Status: Connecting to sftp-staging.messagesnetwork.com...
09:02:04 Status: Connected to sftp-staging.messagesnetwork.com
09:02:09 Status: Retrieving directory listing of "/folder1"...
09:02:10 Status: Listing directory /folder1
09:02:10 Status: Directory listing of "/folder1" successful
09:02:12 Status: Retrieving directory listing of "/folder1/folder2"...
09:02:21 Status: Listing directory /folder1/folder2
09:02:23 Status: Directory listing of "/folder1/folder2" successful
09:02:24 Status: Retrieving directory listing of "/folder1/folder2/folder3"...
09:02:25 Status: Listing directory /folder1/folder2/folder3
09:02:26 Status: Directory listing of "/folder1/folder2/folder3" successful
09:02:30 Status: Connecting to sftp-staging.messagesnetwork.com...
09:02:41 Status: Connected to sftp-staging.messagesnetwork.com
09:02:45 Status: Starting upload of <...>Desktopfolder2cf.txt
09:02:59 Command: cd "/folder1/folder2/folder3"
09:02:59 Response: New directory is: "/folder1/folder2/folder3"
09:02:59 Command: put "<...>Desktopfolder2cf.txt" "folder2cf.txt"
09:02:59 Error: /folder1/folder2/folder3/folder2cf.txt: open for write: failure
09:02:59 Error: File transfer failed
09:02:59 Status: Starting upload of <...>Desktopfolder2cf.txt
09:02:59 Status: Retrieving directory listing of "/folder1/folder2/folder3"...
09:02:59 Status: Listing directory /folder1/folder2/folder3
09:03:11 Command: put "<...>Desktopfolder2cf.txt" "folder2cf.txt"
09:03:11 Error: /folder1/folder2/folder3/folder2cf.txt: open for write: failure
09:03:11 Error: File transfer failed
09:03:11 Status: Starting upload of <...>Desktopfolder2cf.txt
09:03:11 Status: Retrieving directory listing of "/folder1/folder2/folder3"...
09:03:11 Status: Listing directory /folder1/folder2/folder3
09:03:34 Command: put "<...>Desktopfolder2cf.txt" "folder2cf.txt"
09:03:34 Error: /folder1/folder2/folder3/folder2cf.txt: open for write: failure
09:03:34 Error: File transfer failed
09:03:34 Status: Retrieving directory listing of "/folder1/folder2/folder3"...
09:03:39 Status: Listing directory /folder1/folder2/folder3
09:03:44 Status: Directory listing of "/folder1/folder2/folder3" successful
sftp.putfo(StringIO('test string'), os.path.join(remoteFilePath, "OC.csv"))
如果这是真正的问题,我不能说是atm,但您不应该将path.join
用于SFTP路径。SFTP总是使用正斜杠。path.join
使用本地操作系统的分隔符。它可能有所不同(尤其是在Windows上(。
代码应该是:
remoteFilePath = '/dir1/dir2/dir3/'
sftp.putfo(StringIO('test string'), remoteFilePath + "OC.csv")
sftp.put(os.path.dirname(tf.name), os.path.join(remoteFilePath, "OC.csv"))
path.dirname
的使用在这里很有意义。你想上传一个文件,所以你必须给出要上传的文件的路径,而不是包含文件夹的路径。
path.join
也存在与以前相同的问题。