使用shareplum和paramiko库无法传递从SharePoint下载的文件对象
我在用paramiko的putfo()方法传递文件对象时遇到了一个错误。
错误信息:'bytes'对象没有'read'这个属性
这是我使用的putfo()方法的样子:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host, port=port, username=username,password=password, look_for_keys=False)
ftp = ssh_client.open_sftp()
ftp.putfo(file_obj, remotepath=file_name)
file_obj
是通过shareplum的get_file()方法下载到内存中的。
我不知道该怎么做才能解决这个问题。
1 个回答
2
putfo
这个函数需要一个文件或者类似文件的对象。错误信息说明你传入的是原始字节数据,而不是文件输入输出对象(原始字节数据没有 read
方法,但文件输入输出对象是有的)。
要解决这个问题,你可以用 io.BytesIO
来包装你返回的字节数据。
from io import BytesIO
ftp.putfo(BytesIO(file_obj), remotepath=file_name)