[英]unable to upload png files using python to azure devops wiki
I am trying to create a png file as attachment in azure wiki using python and getting the following issue:我正在尝试使用 python 在 azure wiki 中创建一个 png 文件作为附件,并遇到以下问题:
azure.devops.exceptions.AzureDevOpsServiceError: The wiki attachment creation failed with message: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. azure.devops.exceptions.AzureDevOpsServiceError:wiki 附件创建失败并显示消息:输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符.
This is the snippet这是片段
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.wiki.models import GitVersionDescriptor
personal_access_token = '<pat>'
organization_url = 'https://dev.azure.com/<org-name>'
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
wiki_client = connection.clients.get_wiki_client()
version_descriptor = GitVersionDescriptor(version="main", version_type="branch")
with open("./sample-image.png","rb") as png_file:
wiki_client.create_attachment( upload_stream=png_file,
version_descriptor=version_descriptor,
project="<project-name>",
wiki_identifier="<wiki-name>",
name="sample-image.png" )
I'm not very familiar with Python.我对 Python 不是很熟悉。 But according to the error message, it seems that there is a problem with the input format of your image.
但是根据报错信息,好像是你图片的输入格式有问题。
According to this REST API Attachments - Create , Media Types should be application/octet-stream". So the image needs to be converted to Base64.根据此 REST API Attachments - Create , Media Types 应为 application/octet-stream"。因此需要将图像转换为 Base64。
Here are my PowerShell scripts for reference:这是我的 PowerShell 脚本供参考:
$Image_Path = "<your image path>"
$Base64_Code = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$Image_Path"));
$token = "<your PAT>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url= "https://dev.azure.com/<org>/<project>/_apis/wiki/wikis/<wikiId>/attachments?name=<Wiki attachment name>&api-version=6.0"
$head = @{ Authorization =" Basic $token" }
Invoke-RestMethod -Uri $url -Method PUT -Headers $head -Body $Base64_Code -ContentType application/octet-stream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.