TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
正在尝试使用 ssh 从 Internet 上传文件到我的服务器。有以下代码可以很好地上传本地文件,但我不知道还需要做什么才能让图片字节对象上传。
from io import BytesIO
import requests
import pysftp
url = 'https://vignette.wikia.nocookie.net/disney/images/d/db/Donald_Duck_Iconic.png'
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
response = requests.get(url)
netimage = BytesIO(response.content) #imagefromurl
srv = pysftp.Connection(host="12.34.567.89", username="root123",
password="password123",cnopts=cnopts)
with srv.cd('/var/www'): #srvdir
#srv.put('C:\Program Files\Python36\LICENSE.txt') #local file test
srv.put(netimage)
print('Complete')
您需要使用 .open()
method to get a file-like object, then copy across your data, using shutil.copyfileobj()
:
import shutil
with srv.cd('/var/www'):
with srv.open(image_filename, 'w') as remote_file:
shutil.copyfileobj(netimage, remote_file)
Paramiko(以及扩展名为 pysftp)不支持直接放置 in-memory 文件对象。
正在尝试使用 ssh 从 Internet 上传文件到我的服务器。有以下代码可以很好地上传本地文件,但我不知道还需要做什么才能让图片字节对象上传。
from io import BytesIO
import requests
import pysftp
url = 'https://vignette.wikia.nocookie.net/disney/images/d/db/Donald_Duck_Iconic.png'
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
response = requests.get(url)
netimage = BytesIO(response.content) #imagefromurl
srv = pysftp.Connection(host="12.34.567.89", username="root123",
password="password123",cnopts=cnopts)
with srv.cd('/var/www'): #srvdir
#srv.put('C:\Program Files\Python36\LICENSE.txt') #local file test
srv.put(netimage)
print('Complete')
您需要使用 .open()
method to get a file-like object, then copy across your data, using shutil.copyfileobj()
:
import shutil
with srv.cd('/var/www'):
with srv.open(image_filename, 'w') as remote_file:
shutil.copyfileobj(netimage, remote_file)
Paramiko(以及扩展名为 pysftp)不支持直接放置 in-memory 文件对象。