Reputation: 457
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!
Upvotes: 0
Views: 58
Reputation: 16133
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();
Upvotes: 1
Reputation: 5296
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 organizationUrl = "https://dev.azure.com/{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(organizationUrl), new VssBasicCredential(string.Empty, personalAccessToken));
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
using (HttpClient httpClient = new HttpClient())
{
var downloadUrl = $"{organizationUrl}/{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.");
}
}
Upvotes: 1