【问题标题】:TypeError: expected str, bytes or os.PathLike object, not _io.BytesIOTypeError:预期的 str、字节或 os.PathLike 对象,而不是 _io.BytesIO
【发布时间】:2018-03-13 23:34:21
【问题描述】:

尝试使用 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')

【问题讨论】:

    标签: python python-3.x sftp paramiko pysftp


    【解决方案1】:

    您需要使用.open() method 来获取类似文件的对象,然后使用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)不支持直接放入内存中的文件对象。

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2017-10-23
      • 2020-07-12
      • 2019-04-11
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      相关资源
      最近更新 更多