Access file content within an Azure Dev Ops/VSTS artifact using REST API
S

2

8

I am looking to get the contents of a file I pushed as an artifact to Azure DevOps

I was able to get a json response with a URL to the artifact zip by using this API

https://dev.azure.com/uifabric/cd9e4e13-b8db-429a-9c21-499bf1c98639/_apis/build/builds/8838/artifacts?artifactName=drop&api-version=5.0

However, what I really want is the contents of a file called bundlesizes.json within this zip.

I did come across the Get File API here which mentions an API as follows

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&fileId={fileId}&fileName={fileName}&api-version=5.0

I tried replacing it as follows

https://dev.azure.com/uifabric/fabricpublic/_apis/build/builds/8838/artifacts?artifactName=drop&fileId=bundlesizes.json&fileName=bundlesizes.json&api-version=5.0

However, I get this error enter image description here

I think what I am missing is the fileId field, where I am not aware what needs to go in. The documentation says fileId is the The primary key for the file. However, I don't know where I can find it.

Speculative answered 9/3, 2019 at 2:43 Comment(0)
S
3

Microsoft doesn't have complete documentation on how to get FileID.

You can take a different approach and download the file using below API. You can get the ContainerID through GET build details.

https://collectionurl/tfs/defaultcollection/_apis/resources/Containers/${containerid}?itempath=drop
Sri answered 9/10, 2019 at 14:38 Comment(6)
how I get container id?Harmattan
@Harmattan "URL/_apis/build/builds/{buildid}/artifacts" you have the container ID on resource.data.Sri
Thank you for replying. Do you mean here: resources.azure.com?Harmattan
@gsscoder: Did you find an answer for your question above? I'm trying to do the same and wondering how to do this.Sherd
@Sherd Are you able to get the API response for below? <TeamProjectURL>/_apis/build/builds/<BuildID>/artifactsSri
@Sherd I found how to download the whole artifact as zip archive. You shuold use the builds endpoint (learn.microsoft.com/en-us/rest/api/azure/devops/build/artifacts/…) Than add '&%24format=zip Without this you'll get just some JSON that describes the artifact. Hope this can help you!Harmattan
R
3

View my blog post to see how this is done here!

Let us start by hitting the Artifacts - Get Artifact so we can get some info about the artifact.

Request

https://dev.azure.com/oswaldtechnologies/add3132d-b1ce-4519-8299-4e67eecad1f5/_apis/build/builds/703/artifacts?artifactName=TestArtifact

Response

{
    "id": 322,
    "name": "TestArtifact",
    "source": "12f1170f-54f2-53f3-20dd-22fc7dff55f9",
    "resource": {
        "type": "PipelineArtifact",
        "data": "973C2055701973A0FDFB695031EE3F9E7A91741016CA639E9D21ECCD1B387E9B01",
        "properties": {
            "RootId": "DC04A61FBB2E879A10EE8BA01B28B4623140546805A968AB2B491B2EE1BD2E4102",
            "artifactsize": "28",
            "HashType": "DEDUPNODEORCHUNK"
        },
        "url": "https://dev.azure.com/oswaldtechnologies/add3132d-b1ce-4519-8299-4e67eecad1f5/_apis/build/builds/703/artifacts?artifactName=TestArtifact&api-version=7.1",
        "downloadUrl": "https://artprodeus21.artifacts.visualstudio.com/A3e090689-466b-408e-a12e-87c169eff347/add3132d-b1ce-4519-8299-4e67eecad1f5/_apis/artifact/cGlwZWxpbmVhcnRpZmFjdDovL29zd2FsZHRlY2hub2xvZ2llcy9wcm9qZWN0SWQvYWRkMzEzMmQtYjFjZS00NTE5LTgyOTktNGU2N2VlY2FkMWY1L2J1aWxkSWQvNzAzL2FydGlmYWN0TmFtZS9UZXN0QXJ0aWZhY3Q1/content?format=zip"
    }
}

Notice resource.data. This is actually the fileId. And this is what the documentation does not mention. We use the fileId of the actual artifact, to get the artifacts manifest contents. The manifest contains an array of all items in the artifact, including their file path and file id. So now lets hit Aritfacts - Get File with the artifacts file id to get the manifest. Oddly, you must add fileId and fileName to the query, even though what you set fileName to doesn't matter. For this example, I'm going to call it manifest.json, because well, that's what it is.

Request

https://dev.azure.com/oswaldtechnologies/add3132d-b1ce-4519-8299-4e67eecad1f5/_apis/build/builds/703/artifacts?artifactName=TestArtifact&fileId=973C2055701973A0FDFB695031EE3F9E7A91741016CA639E9D21ECCD1B387E9B01&fileName=manifest.json

Response

{
    "manifestFormat": "1.1.0",
    "items": [
        {
            "path": "/test1.txt",
            "blob": {
                "id": "D21C967E56201F344B44EE00F537263C3503AEB13931F99754F9E78E14E6C90C01",
                "size": 14
            }
        },
        {
            "path": "/test2.txt",
            "blob": {
                "id": "FE2A48E456C37C7BAF1F86D724E2C2B30658AA1A899201D61E23CE59A333A63801",
                "size": 14
            }
        }
    ],
    "manifestReferences": []
}

Now we can hit Aritfacts - Get File once more. This time let us get the contents of test1.txt. Again, it does not matter what you set fileName too, as long as the fileId matches the blob.id of the file you want to download.

Request

https://dev.azure.com/{{organization}}/{{project}}/_apis/build/builds/703/artifacts?artifactName=TestArtifact&fileId=D21C967E56201F344B44EE00F537263C3503AEB13931F99754F9E78E14E6C90C01&fileName=test1.txt

Response

Test string 1
Renarenado answered 8/10, 2023 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.