gpt4 book ai didi

c# - REST API 未从 AZURE git 分支下载文件

转载 作者:行者123 更新时间:2023-12-03 06:33:44 27 4
gpt4 key购买 nike

我是 C# 新手,正在尝试从 azure git repo 下载文件。响应状态为成功,但未将文件下载到本地驱动器。我正确传递了所有参数。

 using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", personalaccesstoken))));

using (HttpResponseMessage response = client.GetAsync(
$"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&download={download}&versionDescriptor.versionType={versionDescriptor_versionType}&versionDescriptor.version={versionDescriptor_version}&api-version=7.0").Result)
{
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();

Console.WriteLine(responseBody);

}
}

我得到的输出如下。

{
"objectId": "34e9cd84219e51533b19b3a70121e73b612c0eca",
"gitObjectType": "blob",
"commitId": "1916fd237ce345505a0cdbb285ce0ba71a64691b",
"path": "/Backend/DATA/Document_AA083_A100001.xml",
"url": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/items?path=%2FBackend%2FDATA%2FDocument_AA083_A100001.xml&versionType=Branch&version=tcs00107%2FGIT_Tool&versionOptions=None",
"_links": {
"self": {
"href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/items?path=%2FBackend%2FDATA%2FDocument_AA083_A100001.xml&versionType=Branch&version=tcs00107%2FGIT_Tool&versionOptions=None"
},
"repository": {
"href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5"
},
"blob": {
"href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/blobs/34e9cd84219e51533b19b3a70121e73b612c0eca"
}
}
}

如果有人能帮助我解决这个问题,那将非常有帮助。非常感谢您的帮助。

最佳答案

问题是您获得的 JSON 响应包含有关文件的元数据。您必须使用“url”并使用“HttpClient.GetByteArrayAsync()”执行另一个获取请求。这会将文件作为字节数组获取,然后可以使用“File.WriteAllBytes(string, byte[])”将其写入本地驱动器。

using (HttpClient client = new HttpClient())
{
//.. your existing code
var responseBody = await response.Content.ReadAsStringAsync();
dynamic jsonData = JsonConvert.DeserializeObject(responseBody);
var blobUrl = jsonData._links.blob.href;
byte[] fileBytes = await client.GetByteArrayAsync(blobUrl);
File.WriteAllBytes("path/to/local/file", fileBytes);
}

将路径更改为要保存文件的位置,并注意文件路径的任何权限/限制。

关于c# - REST API 未从 AZURE git 分支下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75087296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com