经过大量的努力,最终在下面的代码框架中通过RESTAPI与Azure Devops交互,但在Azure Devops-Repos的分支中看不到文件
首先使用以下API创建了一个附件:
## Create attachment ###########################################
ado_req_headers_ATT = {"Content-Type":"application/octet-stream"}
directory_path = str('C:\\Users\\XXX\\YYY')
txt_files = [f for f in os.listdir(directory_path) if f.endswith('.json')]
if len(txt_files) != 1:
raise ValueError('should be only one json file in the current directory')
else:
file_name = txt_files[0]
print("Test_file",file_name)
file_path = directory_path+"\\"+str(file_name)
with open(file_path, "rb") as f:
Test_file=f.read()
print(len(Test_file))
create_attach_url = str(organization_url)+str('/_apis/wit/attachments?fileName=')+file_name+str('&uploadType=Simple&api-version=6.0')
create_attach_details = requests.post(url=create_attach_url,headers=ado_req_headers_ATT,data=Test_file,auth=('',ADO_AUTH_PAT))
attachment_obj = create_attach_details.json()
print("create_attach_details_ID_:-",attachment_obj['id']) ## Got attachment ID
print("create_attach_details_URL_:-",attachment_obj['url']) ## Got Url, when this url get hit in browser, got the attachment to get download.
print("Attachment_details_Status_Code:-",create_attach_details.status_code) # code: 201
随后使用以下API上传了附件:
## upload attachment ###########################################
CL=len(Test_file)
Range=(f'"Content-Range":"bytes 0-{CL-1}/{CL}"')
ado_req_headers_ATT = str('{"Content-Type":"application/octet-stream","Content-Length":"')+str(CL)+str('"')+str(',')+Range+str('}')
ado_req_headers_ATT=json.loads(ado_req_headers_ATT)
print(ado_req_headers_ATT)
upload_attach_url = str(organization_url)+str('/_apis/wit/attachments/')+str(attachment_obj['id'])+str('?api-version=6.0')
print(upload_attach_url)
upload_attach_details = requests.put(url=upload_attach_url,headers=ado_req_headers_ATT,data=Test_file,auth=('',ADO_AUTH_PAT))
print(upload_attach_details)
attachment_obj = upload_attach_details.json()
print(attachment_obj)
print("Attachment_details:-",create_attach_details.text)
##print("create_attach_details_ID_:-",attachment_obj['id']) ## Got attachment ID
##print("create_attach_details_URL_:-",attachment_obj['url']) ## Got Url, when this url get hit in browser, got the attachment to get download.
print("Attachment_upload_Status_Code:-",create_attach_details.status_code)## Code: 201
这两个API都很有用,但仍然无法在azure devops Repos分支中看到该文件。我遗漏了什么,无法理解或发布API需要达到的要求?请建议
之后,我尝试使用以下API更新工作项:
##### Update Attachment in path #######################################
ado_req_headers_ATT = {"Content-Type":"application/json-patch+json","dataType":"application/json-patch+json",}
update_attach_url = str(organization_url)+str('/_apis/wit/workitems/')+str(attachment_obj['id'])+str('?api-version=6.0')
print(update_attach_url)
#Body_data = [{"op": "test","path": "/ref","value": 3},{"op": "add","path": "/heads/branchname","value": "Adding the necessary spec"},{"op": "add","path": "/relations/-","value": {"rel": "AttachedFile","url": attachment_obj['url'],"attributes": {"comment": "Spec for the work"}}}]
Body_data = [{"op": "add","path": ref/headers/banchname,"value": {"rel": "AttachedFile","url": str(attachment_obj['url'])+str('?fileName=')+file_name,"attributes": {"comment": "Spec for the work"}}}]
Body_data = json.dumps(Body_data)
print(Body_data)
update_attach_details = requests.patch(url=update_attach_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
print(update_attach_details.text) ## Page not found
print(update_attach_details.status_code) ## 404
有什么建议吗
以下两个API的ie足以将文件上载到存储库:
我终于成功了!!!谢谢大家的支持
使用工作项附件API“Upload Chunk”时,请求方法是
PUT
,而不是PATCH
请将请求方法更改为
PUT
以查看Python代码是否可以工作此外,请确保在API“Create”和“Upload Chunk”上使用了媒体类型“
application/octet-stream
”相关问题 更多 >
编程相关推荐