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

使用Python向Azure DevOps导入数据:上传图片附件后下载文件无效的问题求助

Fixing Corrupted Attachments When Uploading to Azure DevOps via Python

Let's break down why your attachments are getting corrupted and how to fix it—your core issue is incorrectly formatting the file data when uploading to Azure DevOps API.

What's Wrong With Your Current Code?

Your convertAttachmentToJsonBinary function is modifying the file content in ways that Azure DevOps doesn't expect:

  • In the first version, you're base64-encoding the file, then wrapping that string in [] and returning it. Azure DevOps doesn't want base64-encoded text for attachments—it wants the raw binary file content directly.
  • In the second version, converting a bytearray to a string gives you something like bytearray(b'PNG\x0d\x0a...'), which is just text describing the bytearray, not the actual binary data. Wrapping that in [] just stores this text as the attachment, hence the corrupted file when downloaded.

The Correct Approach

Azure DevOps's attachment upload API accepts raw binary data directly (no encoding or string manipulation needed). Here's how to fix your workflow:

1. Upload the Attachment Properly

First, create a function to upload the raw binary file to Azure DevOps. This will return the attachment URL you need for the patch step:

import os
import requests

def upload_attachment(attachment_path, org_name, project_name, headers):
    # Build the upload URL (adjust API version if needed)
    filename = os.path.basename(attachment_path)
    upload_url = f"https://dev.azure.com/{org_name}/{project_name}/_apis/wit/attachments?fileName={filename}&api-version=7.1-preview.3"
    
    # Send the raw binary file data
    with open(attachment_path, 'rb') as file:
        response = requests.post(upload_url, data=file, headers=headers)
    
    if response.status_code == 201:
        return response.json()["url"]
    else:
        raise Exception(f"Attachment upload failed: {response.text}")

2. Keep Your Existing Patch Function (It's Fine!)

Your patchWorkItemWithAttachment function is correctly associating the uploaded attachment URL with the work item—no changes needed here, as long as you pass the valid URL returned from upload_attachment.

3. Updated Workflow

Your end-to-end process should look like this:

# 1. Create your work item (your existing code here)
case_id = "YOUR_WORK_ITEM_ID"

# 2. Upload the attachment
att_url = upload_attachment(
    attachment_path="path/to/your/image.png",
    org_name="your-organization",
    project_name="your-project",
    headers=headers  # Your existing auth headers (e.g., Authorization: Bearer <token>)
)

# 3. Associate the attachment with the work item
patchWorkItemWithAttachment(case_id, "YOUR_ATTACHMENT_ID", att_url)

Key Notes

  • Headers Check: Ensure your headers include a valid Authorization token (e.g., Bearer <PAT_TOKEN>). You don't need to set Content-Type for the upload request—requests will auto-set it correctly for binary data.
  • API Version: The example uses API version 7.1-preview.3; you can adjust this to match your Azure DevOps environment's supported version.

After making these changes, your attachments should upload correctly, and downloading them from the Azure DevOps web UI will result in valid, openable files.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 14:08:12