You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用io.BytesIO向SharePoint上传大文件触发fileno不支持异常

问题:使用Office365-REST-Python-Client通过io.BytesIO上传大CSV文件至SharePoint遇阻

需求背景

业务要求必须通过字节数组或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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:01:01