Python 尽管创建附件&;上传附件API';已成功,但上载的文件在azure devops repos分支中不可见

Python 尽管创建附件&;上传附件API';已成功,但上载的文件在azure devops repos分支中不可见,python,azure-devops,azure-devops-rest-api,Python,Azure Devops,Azure Devops Rest Api,经过大量的努力,最终在下面的代码框架中通过RESTAPI与Azure Devops交互,但在Azure Devops-Repos的分支中看不到文件 首先使用以下API创建了一个附件: 随后使用以下API上传了附件: 这两个API都很有用,但仍然无法在azure devops Repos分支中看到该文件。我遗漏了什么,无法理解或发布API需要达到的要求?请建议 之后,我尝试使用以下API更新工作项: 请提供任何建议使用工作项附件API“”时,请求方法为PUT,而不是补丁 请将请求方法更改为P

经过大量的努力,最终在下面的代码框架中通过RESTAPI与Azure Devops交互,但在Azure Devops-Repos的分支中看不到文件

首先使用以下API创建了一个附件:

随后使用以下API上传了附件:

这两个API都很有用,但仍然无法在azure devops Repos分支中看到该文件。我遗漏了什么,无法理解或发布API需要达到的要求?请建议

之后,我尝试使用以下API更新工作项:


请提供任何建议

使用工作项附件API“”时,请求方法为
PUT
,而不是
补丁

请将请求方法更改为
PUT
,以查看Python代码是否可以工作


此外,请确保在API“”和“”上使用了媒体类型“
应用程序/octet流”

当使用工作项附件API“”时,请求方法是
放置
,而不是
补丁

请将请求方法更改为
PUT
,以查看Python代码是否可以工作


此外,请确保您在API“”和“”上使用了媒体类型“
应用程序/octet流”

以下两个API即足以将文件上载到存储库中:

######## Get   last commit ID ##########################
#Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][0]['id'])+str('/commits?api-version=6.0')
Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/commits?api-version=6.0')
print(Get_commit_url)
Get_commit_Details = requests.get(url=Get_commit_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT))
##print(Get_Push_Details.text)
##print(Get_Push_Details.status_code)
Get_commit_Details=Get_commit_Details.json()
print(Get_commit_Details)
for item in Get_commit_Details["value"]:
    print( item["commitId"])
    old_objectID= item["commitId"]
    break

## Run push API

ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"}
push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/pushes?api-version=6.0')
print(push_file_url)
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][0]['defaultBranch'],"oldObjectId":"0000000000000000000000000000000000000000"}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Remove file.","changes":[{"changeType": "delete","item": {"path":"/Hello.doc"}}]}]}
Body_data = json.dumps(Body_data)
##print(Body_data)
Push_file= requests.post(url=push_file_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
print(Push_file.status_code)
print(Push_file.text)

我终于成功了!!!感谢大家对以下两个API的支持,这两个API足以将文件上载到存储库:

######## Get   last commit ID ##########################
#Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][0]['id'])+str('/commits?api-version=6.0')
Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/commits?api-version=6.0')
print(Get_commit_url)
Get_commit_Details = requests.get(url=Get_commit_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT))
##print(Get_Push_Details.text)
##print(Get_Push_Details.status_code)
Get_commit_Details=Get_commit_Details.json()
print(Get_commit_Details)
for item in Get_commit_Details["value"]:
    print( item["commitId"])
    old_objectID= item["commitId"]
    break

## Run push API

ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"}
push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/pushes?api-version=6.0')
print(push_file_url)
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][0]['defaultBranch'],"oldObjectId":"0000000000000000000000000000000000000000"}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Remove file.","changes":[{"changeType": "delete","item": {"path":"/Hello.doc"}}]}]}
Body_data = json.dumps(Body_data)
##print(Body_data)
Push_file= requests.post(url=push_file_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
print(Push_file.status_code)
print(Push_file.text)

我终于成功了!!!感谢大家的支持

最终理解了上述逻辑并使用了创建和上传区块。对于创建,响应代码为201,但对于上载区块:{“计数”:1,“值”:{“消息”:“附件上载请求中指定的内容范围无效。”}}附件详细信息\状态\代码:-400。关于如何定义长度的任何帮助。我已将其定义为:{“内容类型”:“应用程序/八位字节流”,“内容范围”:“11000”}FYR,我的文件上载大小为11kb。请如何纠正内容范围问题“值”:{“消息”:“附件上传请求中指定的内容范围无效。”}上传的附件高于上传限制(>130MB)时,通常使用uploadType
Chunked
。您可以尝试使用
Simple
类型而不是
Chunked
{“内容类型”:“应用程序/八位字节流”,“内容长度”:“11100”,“内容范围”:“字节0-11099/11100”}。该头解决了上传请求的目的,最终得到的响应代码是201&具有有效的url和附件ID。仍然无法在azure devops respos文件夹中查看我的文件。下一步是什么?最终理解了上面的逻辑并使用了创建和上传块。对于创建,响应代码为201,但对于上载区块:{“计数”:1,“值”:{“消息”:“附件上载请求中指定的内容范围无效。”}}附件详细信息\状态\代码:-400。关于如何定义长度的任何帮助。我已将其定义为:{“内容类型”:“应用程序/八位字节流”,“内容范围”:“11000”}FYR,我的文件上载大小为11kb。请如何纠正内容范围问题“值”:{“消息”:“附件上传请求中指定的内容范围无效。”}上传的附件高于上传限制(>130MB)时,通常使用uploadType
Chunked
。您可以尝试使用
Simple
类型而不是
Chunked
{“内容类型”:“应用程序/八位字节流”,“内容长度”:“11100”,“内容范围”:“字节0-11099/11100”}。该头解决了上传请求的目的,最终得到的响应代码是201&具有有效的url和附件ID。仍然无法在azure devops respos文件夹中查看我的文件。下一步是什么??
##### 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
######## Get   last commit ID ##########################
#Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][0]['id'])+str('/commits?api-version=6.0')
Get_commit_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/commits?api-version=6.0')
print(Get_commit_url)
Get_commit_Details = requests.get(url=Get_commit_url,headers=ado_req_headers_ATT,auth=('',ADO_AUTH_PAT))
##print(Get_Push_Details.text)
##print(Get_Push_Details.status_code)
Get_commit_Details=Get_commit_Details.json()
print(Get_commit_Details)
for item in Get_commit_Details["value"]:
    print( item["commitId"])
    old_objectID= item["commitId"]
    break

## Run push API

ado_req_headers_ATT = {"Content-Type":"application/json-patch+json"}
push_file_url = str(organization_url)+str('/_apis/git/repositories/')+str(Repos_obj['value'][Repository_ID]['id'])+str('/pushes?api-version=6.0')
print(push_file_url)
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][0]['defaultBranch'],"oldObjectId":"0000000000000000000000000000000000000000"}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Initial Commmit.","changes":[{"changeType": "add","item": {"path":"/Hello.doc"},"newContent": {"content": "# Tasks\n\n* Item 1Hello\n* Item 2","contentType": "rawtext"}}]}]}
##Body_data = {"refUpdates": [{"name": Repos_obj['value'][Repository_ID]['defaultBranch'],"oldObjectId":old_objectID}],"commits":[{"comment": "Remove file.","changes":[{"changeType": "delete","item": {"path":"/Hello.doc"}}]}]}
Body_data = json.dumps(Body_data)
##print(Body_data)
Push_file= requests.post(url=push_file_url,headers=ado_req_headers_ATT,data=Body_data,auth=('',ADO_AUTH_PAT))
print(Push_file.status_code)
print(Push_file.text)