擅长:python、mysql、java
<p>我遇到了同样的问题,由于安全原因,我无法在本地复制该文件,我通过使用预取和bytesIO的组合解决了这个问题:</p>
<pre><code>def fetch_file_as_bytesIO(sftp, path):
"""
Using the sftp client it retrieves the file on the given path by using pre fetching.
:param sftp: the sftp client
:param path: path of the file to retrieve
:return: bytesIO with the file content
"""
with sftp.file(path, mode='rb') as file:
file_size = file.stat().st_size
file.prefetch(file_size)
file.set_pipelined()
return io.BytesIO(file.read(file_size))
</code></pre>