使用Python向Azure DevOps导入数据:上传图片附件后下载文件无效的问题求助
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
bytearrayto a string gives you something likebytearray(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
headersinclude a validAuthorizationtoken (e.g.,Bearer <PAT_TOKEN>). You don't need to setContent-Typefor 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

