【发布时间】:2021-05-12 16:33:24
【问题描述】:
我浏览了其他几个 SO 页面,虽然它确实帮助我了解将附件上传到工作项的工作原理,但在通过 API 成功上传附件后,我仍然找不到查看所述附件的方法,尤其是如果这是一张图片。
我使用过的 API:
使用从上一个 API 响应中检索到的附件 URL,我在 Work Item Update API 中使用它,下面是一个 sn-p:
api = f"https://dev.azure.com/{organization}/_apis/wit/workitems/{work_item_id}?api-version=6.0"
file_size = os.path.getsize(file_path)
payload = [
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": attachment_url,
"attributes": {
"comment": "Test",
"resourceSize": file_size
}
}
}
]
在此之后,附件在工作项中可见,但在单击时,它不会呈现图像。而是打开一个窗口并播放无限加载屏幕动画。 它必须是导致这种情况的图像编码,但是我找不到任何文档。
以下是将附件上传到现有工作项的完整代码。
def upload_attachment(file_path, work_item_id):
# This part retrieves the Attachment URL
headers = {
"Accept": "application/json",
"Content-Size": str(os.path.getsize(file_path)),
"Content-Type": "application/octet-stream",
}
files = {'attachment': open(file_path, 'rb')}
filename = os.path.basename(file_path)
api = f"https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?uploadType=Simple&fileName={filename}&api-version=1.0"
resp = requests.post(url=api, files=files, headers=headers, auth=("","*fmhcstt*4nwadqy3uk*go23fga"))
attachment_url = resp.json()['url']
# This part updates an existing work item, adding the attachment here.
api = f"https://dev.azure.com/{organization}/_apis/wit/workitems/{work_item_id}?api-version=6.0"
file_size = os.path.getsize(file_path)
payload = [
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": attachment_url,
"attributes": {
"comment": "Test",
"resourceSize": file_size
}
}
}
]
headers = {
'Accept':'application/json',
'dataType': 'application/json-patch+json',
'Content-type':'application/json-patch+json'
}
resp = requests.patch(url=api, headers=headers, json=payload, auth=("","*fmhcstt*4nwadqy3uk*go23fga"))
【问题讨论】:
-
参考this doc 您可以更改patch to put方法。您要上传什么样的附件?附件在哪里?
-
当然!我会试一试。我正在将 PNG 文件上传到工作项 @Jeff
-
好的,所以在进行分块上传之前,我尝试使用 PUT 而不是 PATCH,但我收到一条错误消息,指出特定的 HTTP 方法不支持 PUT。然后我尝试使用分块上传 API,我收到错误,因为“附件上传请求中指定的内容范围无效”。我猜这与小文件大小有关,因为它只有一个我要上传的 PNG。