Azure Devops: Fail to get artifact content zip with TFS Client lib

孤者浪人 提交于 2021-01-29 05:21:53

问题


I want to download an artifact with Azure DevOps Services API.
While programing with C#, I choose to use Microsoft.TeamFoundationServer.Client SDK, Version: 16.153.0 as a tool.

I am sure I have the artifact.
But After I use BuildHttpClient::GetArtifactContentZipAsync(project: "XXX", buildId: buildid, artifactName: "XXX")
to get zip stream. I get exception with a message like :
The requested version \"5.1\" of the resource is under preview. The -preview flag must be supplied in the api-version for such requests. For example: \"5.1-preview\"

It seems I use the wrong version of API, but I really didn't see any API to set this version to "5.1-preview".
Is there a way to solve this problem? Or should I use an older version of TFS SDK?


回答1:


I tested with the 15.131.1 version of Microsoft.TeamFoundationServer.Client SDK and it works well , but trying the 16.153.0 version and it failed .

Sample code:

static readonly string TFUrl = "https://dev.azure.com/OrgName/";
static readonly string UserPAT = "PAT";

static void Main(string[] args)
{
    try
    {
        int buildId = xx; // update to an existing build definition id
        string artifactName = "drop"; //default artifact name
    //  string project = "projectName";
        ConnectWithPAT(TFUrl, UserPAT);

        Stream zipStream = BuildClient.GetArtifactContentZipAsync(buildId, artifactName).Result; //get content
        using (FileStream zipFile = new FileStream(@"C:\MySite\test.zip", FileMode.Create))
            zipStream.CopyTo(zipFile);
        Console.WriteLine("Done");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception: " + ex.Message);
        if (ex.InnerException != null) Console.WriteLine("Detailed Info: " + ex.InnerException.Message);
        Console.WriteLine("Stack:\n" + ex.StackTrace);
    }
}



回答2:


Thanks for Hugh's answer. Maybe there is something wrong with the SDK of version 16.153.0.
For some reason, I should use a new version SDK, so I choose to bypass the API and download the artifact by myself.

Firstly, I use BuildHttpClient::GetArtifactContentZipAsync to get an object of struct BuildArtifact. Then I find the download link of artifact in BuildArtifact.Resource.DownloadUrl. Finally I get the resource from this link.

In this way, I should handle some details like Auth/Httpdownload by myself. But anyway I get what I want.

Hope to work in the next version of TFS SDK.



来源:https://stackoverflow.com/questions/62911393/azure-devops-fail-to-get-artifact-content-zip-with-tfs-client-lib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!