简体   繁体   English

Azure Devops 上传图片

[英]Azure Devops Upload Image

I am importing a lot of data into Azure Devops using python. I have all the pieces working.我正在使用 python 将大量数据导入 Azure Devops。我的所有部分都在工作。 I can upload images, my issue is when I download the image from the Devops website it is not a valid file.我可以上传图像,我的问题是当我从 Devops 网站下载图像时它不是有效文件。 I will share my image upload code.我将分享我的图片上传代码。

def convertAttachmentToJsonBinary(attachmentPath):
    file = open(attachmentPath, 'rb')
    file_data = file.read()
    encoded_data = base64.b64encode(file_data)
    decoded_data = encoded_data.decode()
    data = "[" + decoded_data + "]"
    return data


def patchWorkItemWithAttachment(case_id, att_id, att_url):
    json_template = open("attachment.json", "r")
    json_data = json.load(json_template)
    json_data[0]['value']['url'] = att_url
    json_data[0]['value']['attributes']['comment'] = "Adding attachment"

    response = requests.patch(patch_workitem_url.replace("{id}", case_id), json=json_data, headers=headers)
    print(response.json())

While I am uploading a new case, I create the case, add attachments, then patch the newly created case with the new attachment.在上传新案例时,我会创建案例、添加附件,然后使用新附件修补新创建的案例。 Again all of this work.再次所有这些工作。 I can see the attachment in my case online, however when I download it the file is invalid.我可以在网上看到我案例中的附件,但是当我下载它时文件无效。 DevOps 上的图像 在此处输入图像描述

I have attempted to read the data as a byte array as follows:我试图将数据读取为字节数组,如下所示:

def convertAttachmentToJsonBinary(attachmentPath):
    file = open(attachmentPath, 'rb')
    encoded_data = bytearray(file.read())

    data = "[" + str(encoded_data) + "]"
    return data

I get the same error, uploading the image works however downloading the image it is invalid.我得到同样的错误,上传图像有效但下载图像无效。

I tested again and found I can't open the url returned from uploading to attachment Rest API successfully.我再次测试,发现我无法打开上传到附件Rest API成功返回的url。 So the image has broken when uploading.所以上传的时候图片坏了。

Please try to convert image to byte array with bytearray(image_file.read()) .请尝试使用bytearray(image_file.read())将图像转换为字节数组

Please replace the value of <>请替换<>的值

import requests
import base64
from io import BytesIO

pat = 'xxx'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
url = 'https://dev.azure.com/<org_name>/<image_name>/_apis/wit/attachments?fileName=<filename.bmp>&api-version=6.0'

headers = {
    'Content-Type': 'application/octet-stream', 
    'Authorization': 'Basic '+authorization
}

#upload to attachment
with open("<image_path>", "rb") as image_file:
    # base64 doesn't work well
    # encoded_data = base64.b64encode(image_file.read())
    # decoded_data = encoded_data.decode()
    # data = "[" + decoded_data + "]"
    data = bytearray(image_file.read())

    r = requests.post(url, data=data, 
        headers=headers)

    print(r.text)

#connect wit with attachment
......
 ‘Paint cannot read this file. This is not a valid bitmap file, or its format is not currently supported'.

This error is not related to the code but the image which you are downloading must be corrupted so that might be the reason why paint unable to open this image.此错误与代码无关,但您正在下载的图像必须已损坏,因此这可能是绘图无法打开此图像的原因。

You can try the below mentioned things :您可以尝试以下提到的事情:

1. Try to Open image on Different PC . 1.尝试在不同的PC上打开图像

2. Open it in Another Photo Viewer Application . 2.在另一个照片查看器应用程序中打开它

3. Run Windows Troubleshooter . 3.运行 Windows 疑难解答

4. Try to Open image on Different PC 4.尝试在不同的PC上打开图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM