使用 "Attachments - Create" API 将图像作为附件添加到 Azure DevOps 工作项在打开时不会呈现
Adding an image as an attachment to an Azure DevOps work item using the "Attachments - Create" API does not render when opened
我浏览了其他几个 SO 页面,虽然它确实帮助我理解了将附件上传到工作项的工作原理,但在通过 [= 成功上传后,我仍然找不到查看所述附件的方法27=],特别是如果它是图像。
API我用过:
使用从上一个 API 响应中检索到的附件 URL,我在工作项更新 API 中使用了它,下面是一个片段:
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
}
}
}
]
在此之后,附件在工作项中可见,但单击时不会呈现图像。取而代之的是 window 打开并播放无限加载屏幕动画。
它必须是导致此问题的图像编码,但是我找不到任何文档。
下面是将附件上传到现有工作项的完整代码。
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"))
我发现问题出在 POST 请求上。
requests.post(url=api, files=files, headers=headers, auth=("","*fmhcstt*4nwadqy3uk*go23fga"))
应该是:
requests.post(url=api, data=files, ...
我浏览了其他几个 SO 页面,虽然它确实帮助我理解了将附件上传到工作项的工作原理,但在通过 [= 成功上传后,我仍然找不到查看所述附件的方法27=],特别是如果它是图像。
API我用过:
使用从上一个 API 响应中检索到的附件 URL,我在工作项更新 API 中使用了它,下面是一个片段:
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
}
}
}
]
在此之后,附件在工作项中可见,但单击时不会呈现图像。取而代之的是 window 打开并播放无限加载屏幕动画。 它必须是导致此问题的图像编码,但是我找不到任何文档。
下面是将附件上传到现有工作项的完整代码。
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"))
我发现问题出在 POST 请求上。
requests.post(url=api, files=files, headers=headers, auth=("","*fmhcstt*4nwadqy3uk*go23fga"))
应该是:
requests.post(url=api, data=files, ...