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

本地正常线上异常:ADO工作项附件上传遇已关闭文件I/O错误

线上环境上传附件至ADO Backlog时文件已关闭报错

我们有一个用户提交Bug的表单,提交后会调用接口将信息及附件(如PDF)上传至ADO的Backlog工作项。该代码在本地运行正常,但在线上环境中,日志显示报错:

上传附件错误:对已关闭的文件执行I/O操作。

最初出现错误的代码(本地运行无报错)

if document["files"]:
            for file in document["files"]:
                workitem_id = r.json()["id"]
                attachment_data = file.file.read()
                r = requests.post(
                    f"{self.base_url}/_apis/wit/attachments?fileName={file.filename}&api-version={self.api_version}",
                    data=attachment_data,
                    auth=("", self.pat),
                    headers={"Content-Type": "application/octet-stream"},
                )
                update_data = [
                    {
                        "op": "add",
                        "path": "/relations/-",
                        "value": {
                            "rel": "AttachedFile",
                            "url": r.json()["url"],
                            "attributes": {"comment": "Spec for the work"},
                        },
                    }
                ]
                r = requests.request(
                    method="PATCH",
                    url=f"{self.base_url}/_apis/wit/workitems/{workitem_id}?api-version= {self.api_version}",
                    json=update_data,
                    headers={"Content-Type": "application/json-patch+json"},
                    auth=("", self.pat),
                )

尝试修改后的代码

为定位错误添加了日志,并尝试使用with语句管理文件上下文,修改后的代码如下:

if document["files"]:
            for file in document["files"]:
                try:
                    workitem_id = r.json()["id"]
                    with file.file as attachment_file:
                        attachment_data = attachment_file.read()
                        r = requests.post(
                            f"{self.base_url}/_apis/wit/attachments?fileName={file.filename}&api-version={self.api_version}", 
                            data=attachment_data,
                            auth=("", self.pat),
                            headers={"Content-Type": "application/octet-stream"},
                        )
                        logger.info(
                            f"Uploading {file.filename} to backlog attachments returned status code: {r.status_code}, response: {r.text}"
                        )
                        update_data = [
                            {
                                "op": "add",
                                "path": "/relations/-",
                                "value": {
                                    "rel": "AttachedFile",
                                    "url": r.json()["url"],
                                    "attributes": {"comment": "Spec for the work"},
                                },
                            }
                        ]
                        r = requests.request(
                            method="PATCH",
                            url=f"{self.base_url}/_apis/wit/workitems/{workitem_id}?api-version={self.api_version}",
                            json=update_data,
                            headers={"Content-Type": "application/json-patch+json"},
                            auth=("", self.pat),
                        )
                        logger.info(
                            f"Patching '{file.filename}' to item '{document['title']}' returned status code: {r.status_code}, response: {r.text}"
                        )
                except Exception as e:
                    logger.error(f"Error uploading attachment: {e}")

但修改后出现新的报错:

上传附件错误:无法进入已关闭文件的上下文。

内容的提问来源于stack exchange,提问作者eamohego

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:55:59