Upload a file-like object with Paramiko?

前端 未结 2 611
温柔的废话
温柔的废话 2021-01-02 07:28

I have a bunch of code that looks like this:

with tempfile.NamedTemporaryFile() as tmpfile:
    tmpfile.write(fileobj.read()) # fileobj is some file-like obj         


        
2条回答
  •  时光取名叫无心
    2021-01-02 07:42

    Update As of Paramiko 1.10, you can use putfo:

    self.sftp.putfo(fileobj, path)
    

    Instead of using paramiko.SFTPClient.put, you can use paramiko.SFTPClient.open, which opens a file-like object. You can write to that. Something like this:

    f = self.sftp.open(path, 'wb')
    f.write(fileobj.read())
    f.close()
    

    Note that it may be worthwhile to feed paramiko data in 32 KiB chunks, since that's the largest chunk underlying SSH protocol can handle without breaking it into multiple packets.

提交回复
热议问题