【问题标题】:Adding an image as an attachment to an Azure DevOps work item using the "Attachments - Create" API does not render when opened使用“附件 - 创建”API 将图像作为附件添加到 Azure DevOps 工作项在打开时不会呈现
【发布时间】:2021-05-12 16:33:24
【问题描述】:

我浏览了其他几个 SO 页面,虽然它确实帮助我了解将附件上传到工作项的工作原理,但在通过 API 成功上传附件后,我仍然找不到查看所述附件的方法,尤其是如果这是一张图片。

我使用过的 API:

创建附件链接:https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?view=azure-devops-server-rest-5.0

使用从上一个 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。

标签: azure-devops-rest-api


【解决方案1】:

我发现问题出在 POST 请求上。

requests.post(url=api, files=files, headers=headers, auth=("","*fmhcstt*4nwadqy3uk*go23fga"))

应该是:

requests.post(url=api, data=files, ...

【讨论】:

  • 太棒了!请接受这个答案。这将对其他用户有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2021-05-18
  • 2018-07-31
相关资源
最近更新 更多