I'm trying to automate build artifact download. The objective is to download a specific build artifact. When I call BuildClient.GetArtifactContentZipAsync, it throws the following error

I am using the following code
private static void DownloadBuildResults(Build StartedBuild, string ArtifactName)
{
//BuildArtifact drop = BuildClient.GetArtifactAsync(StartedBuild.Id, ArtifactName).Result; //get detiled info
//Console.WriteLine("Build location: " + drop.Resource.DownloadUrl);
//string dropFileName = String.Format("{0}_{1}.zip", StartedBuild.Definition.Name, StartedBuild.BuildNumber);
Stream zipStream = BuildClient.GetArtifactContentZipAsync(79414, "abc").Result; //get content
using (FileStream zipFile = new FileStream("abc.zip", FileMode.Create))
zipStream.CopyTo(zipFile);
Please note that I buildID and artifact name is correct. I have double-checked these values. The above code example is taken from https://github.com/ashamrai/TFRestApi/blob/master/19.TFRestApiAppQueueBuild/TFRestApiApp/Program.cs
Is this a bug in the API/SDK? or Am I doing something wrong?
Here is the sample code if someone wants to give a try GIST: https://gist.github.com/abhishekgoenka/9759256f995e7f7c9cbcb7872c8591c0
I get following error in code

Stack trace
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<HandleResponseAsync>d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__51.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.TeamFoundation.Build.WebApi.BuildHttpClientBase.<GetArtifactContentZipAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Utils.Dashboard.FrmAzureDashboard.<BtnDownloadKHP_Click>d__13.MoveNext() in C:\code\MetaFXTools\ui\Utils\Dashboard\FrmAzureDashboard.cs:line 118
I have hit the same issue as well.
What I have determined so far is that the call to GetArtifactContentZipAsync results in a 302 response from the server.
(The Location header in the response is along the lines of: https://artproduks1.artifacts.visualstudio.com/.../_apis/artifact/.../content?format=zip)
Inside of GetArtifactContentZipAsync is a call to httpResponseMessage.EnsureSuccessStatusCode() and this causes the error to be thrown as the library is not expecting a redirect to be returned. It is possible to create the connection using: new VssConnection(uri, creds, new VssClientHttpRequestSettings{AllowAutoRedirect = true}), so that the redirect is followed, however this then errors with a different message indicating that an incorrect version has been sent to the server.
I got to know the reason of the exception being thrown when calling this API. In the pipeline if you are using the "Publish build artifacts" to publish the artifacts, the API will work fine. Whereas if you use "Publish pipeline artifact" task, the API will return 302 ("Found").
Microsoft currently has no workaround for this issue and I would suggest you to use either HttpClient or WebClient to download the artifacts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With