最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tfs - Download a Zip File from Azure DevOps Git Repo Using Azure DevOps SDK - Stack Overflow

programmeradmin3浏览0评论

I am trying to download a zip file from an Azure DevOps Git repository using the Azure DevOps SDK for .NET (dont want to use the plain REST APIs).

 public async Task DownloadItemAsync(string gitRepositoryName, string remotefilePath, string localFilePath)
 {
     VssConnection vssConnection = GetVssConnection();

     GitHttpClient gitHttpClient = vssConnection.GetClient<GitHttpClient>();

     var response = gitHttpClient.GetItemAsync(Settings.TfsProjectName, gitRepositoryName, remotefilePath, null, VersionControlRecursionType.None, null, null, download:true, null, true).Result;
}

How can I use the response object to download and save the zip file to a local path? Is there any better class/method from the SDK ?

Any guidance or examples would be greatly appreciated!

I am trying to download a zip file from an Azure DevOps Git repository using the Azure DevOps SDK for .NET (dont want to use the plain REST APIs).

 public async Task DownloadItemAsync(string gitRepositoryName, string remotefilePath, string localFilePath)
 {
     VssConnection vssConnection = GetVssConnection();

     GitHttpClient gitHttpClient = vssConnection.GetClient<GitHttpClient>();

     var response = gitHttpClient.GetItemAsync(Settings.TfsProjectName, gitRepositoryName, remotefilePath, null, VersionControlRecursionType.None, null, null, download:true, null, true).Result;
}

How can I use the response object to download and save the zip file to a local path? Is there any better class/method from the SDK ?

Any guidance or examples would be greatly appreciated!

Share Improve this question asked Feb 14 at 13:37 osim_ansosim_ans 4572 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

you may try GetItemZipAsync method

var fileStream = File.Create("C:\\Temp\\my.zip");

var zipstream = GitClient.GetItemZipAsync(TeamProjectName, GitRepoName, path: "/Readme.md").Result;

zipstream.CopyTo(fileStream);

fileStream.Close();

According to the official doc, GetItemAsync and GetItemZipAsync method don't apply to zipped content. Based on my investigation, I don't find a class/method supporting download zip file from Azure Repo except for HttpClient class.

Based on the current situation, it's suggested to use HttpClient class and REST API Items - Get.

One example for your reference:

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string anizationUrl = "https://dev.azure/{OrgName}";
        string projectName = "{ProjectName}";
        string repositoryName = "{RepoName}";
        string personalAccessToken = "{PAT}";
        string downloadPath = @"{LocalPath}\myzip.zip";
        string filePath = "myzip.zip";

        VssConnection connection = new VssConnection(new Uri(anizationUrl), new VssBasicCredential(string.Empty, personalAccessToken));
        GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

        using (HttpClient httpClient = new HttpClient())
        {
            var downloadUrl = $"{anizationUrl}/{projectName}/_apis/git/repositories/{repositoryName}/items?path={filePath}&api-version=6.0&$format=zip";
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($":{personalAccessToken}")));

            using (HttpResponseMessage response = await httpClient.GetAsync(downloadUrl))
            {
                response.EnsureSuccessStatusCode();
                using (Stream contentStream = await response.Content.ReadAsStreamAsync(),
                    fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
                {
                    await contentStream.CopyToAsync(fileStream);
                }
            }
        }

        Console.WriteLine("Download completed successfully.");
    }
}
发布评论

评论列表(0)

  1. 暂无评论