使用io.BytesIO向SharePoint上传大文件触发fileno不支持异常
需求背景
业务要求必须通过字节数组或io.BytesIO实例上传较大的CSV文件到SharePoint文档库,不能使用本地文件路径。
分片上传尝试代码
尝试用分片上传的方式实现,代码如下:
from office365.sharepoint.folders.folder import Folder from office365.sharepoint.files.file import File import io def write_file_bytes(self, relative_url: str, file_name: str, file_bytes: bytes) -> None: folder: Folder = self.client_context.web.get_folder_by_server_relative_url(relative_url) chunk_size: int = 1024 * 1024 * 15 # 将字节转为IO流 file_stream: io.BytesIO = io.BytesIO(file_bytes) folder.files.create_upload_session(file_stream, chunk_size=chunk_size, file_name=file_name).execute_query()
遇到的异常问题
参考相关信息得知,通过io.BytesIO实现分片上传是可行的,需要将file_name和file_size作为关键字参数传入chunk_uploaded回调,但即使添加了带文件大小参数的回调,仍触发io.UnsupportedOperation: fileno异常。
替代方法的局限性
使用简单上传方法可以正常运行:
folder.upload_file(file_name, file_bytes).execute_query()
但该方法存在4.0MB的文件大小限制,无法满足大文件上传的需求。
内容的提问来源于stack exchange,提问作者ChaddRobertson
相关产品推荐
相关产品推荐

