Is there a way to download the artifacts using API?
Asked by Ruban Siva:

I can access the artifacts for a build using:

[\<PlanKey\>?buildstate=Successful\&max-results=1\&expand=results.result.artifacts](http://ci.uk.mizuho-sc.com:8085/rest/api/latest/result/REG-SQL-JOB1?buildstate=Successful&max-results=1&expand=results.result.artifacts)

Result something like:  
\<artifacts start-index="0" max-result="2" size="2"\>  
\<artifact\>  
\<name\>DACPAC\</name\>  
\<link href="[http://\<link\>/artifact/shared/DACPAC/](http://ci.uk.mizuho-sc.com:8085/browse/REG-SQL-10/artifact/shared/DACPAC/)" rel="self"/\>  
\<producerJobKey\>REG-SQL-JOB1-10\</producerJobKey\>  
\<shared\>true\</shared\>  
\<size\>945514\</size\>  
\<prettySizeDescription\>923 KB\</prettySizeDescription\>  
\</artifact\>  
\</artifacts\>

...

However, is it possible then to download the artifacts from the artifact link result without resorting to some sort of HTML scraping?


Answers:
author: Ruban Siva
body: OK I can see it will give the artifact but how can i use this to download all the artifacts associated with the plan?

In fact, how is this more useful than the original API url I was using in the question?

[http://\<URL\>/rest/api/latest/result/\<\[projectKey\]-\[buildKey\]\>?buildstate=Successful\&max-results=1\&expand=results.result.artifacts](http://ci.uk.mizuho-sc.com:8085/rest/api/latest/result/REG-SQL?buildstate=Successful&max-results=1&expand=results.result.artifacts)

comments: author: rsperafico
body: Hello Ruban,"-- However, is it possible then to download the artifacts from the artifact link result without resorting to some sort of HTML scraping?"No, as mentioned earlier, it will be required to write a script to loop trough the results and download the artifacts.---Kind regards,Rafael P. SperaficoAtlassian Support


author: Yong Lee
body: so what url to use for downloading the artifacts in the loop ?


author: rsperafico
body: Hello Ruban,

Thank you for your question.

This is the REST API call associated to Artifacts [/plan/{projectKey}-{buildKey}/artifact](https://docs.atlassian.com/bamboo/REST/5.8.0/#d2e381) which will provide you with the following output:

```
&lt;artifacts expand="artifacts"&gt;	&lt;link href="[bamboo-base-url]/rest/api/latest/plan/[projectKey]-[buildKey]/artifact" rel="self"/&gt;	&lt;artifacts start-index="0" max-result="1" size="1"&gt;		&lt;artifact&gt;			&lt;id&gt;1081345&lt;/id&gt;			&lt;name&gt;Greeting&lt;/name&gt;			&lt;location/&gt;			&lt;copyPattern&gt;*.txt&lt;/copyPattern&gt;			&lt;shared&gt;true&lt;/shared&gt;		&lt;/artifact&gt;	&lt;/artifacts&gt;&lt;/artifacts&gt;
```

It will be required to iterate through the result(s) above and use it to build the URL to download the files.

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

---

Kind regards,  
Rafael P. Sperafico  
Atlassian Support

comments: author: Peter Kahn
body: Hi Rafael, this doesn't seem to work for me.<http://builder:8085/rest/api/latest/plan/UTILS-SVNPERM/artifact?expand=artifacts>should return my lone artifact definition but I get\<artifacts expand="artifacts"\>\<link href="[http://builder:8085/rest/api/latest/plan/UTILS-SVNPERM/artifact"](http://builder:8085/rest/api/latest/plan/UTILS-SVNPERM/artifact&quot); rel="self"/\>\<artifacts start-index="0" max-result="0" size="0"/\>\</artifacts\>Any clue what I'm missing?


author: Peter Kahn
body: Oh, that ONLY works for share artifacts.Verified by looking at source and experimentation. Ok, I must share my artifacts to be able to use this call


author: Nima Rafiei
body: "It will be required to iterate through the result(s) above and use it to build the URL to download the files."

Exactly how to use the results to build the URL? **What** should go **where**?


author: Yong Lee
body: @Rafael Please provide more details on how the iteration is done as there is no obvious info on what URL to use for subsequent download requests to get the artifacts, especially for plans that produce multiple artifacts


author: Kaushik Veluru
body: Yong, were you able to figure out how to do the iteration or artifact results?


author: jeffrey monaco
body: [@Kaushik Veluru](/t5/user/viewprofilepage/user-id/510414) Do you know how the URL should be put together to download the artifact? I have no problem getting the information and parsing it. I am just not sure how the URL is formatted


author: Kelly Schoenhofen
body: The REST api ended up not helping me, because most of the interesting artifacts I needed to grab weren't individually named but shared via expressions, e.g. \*.deb. As far as I can tell in Feb 2021, the REST API will not detail out individual artifacts that were gathered via an expression, so I had to resort to a small amount of html web scraping.   

In Python 3.8, here's my grand html scraping class:  

```
class ParseBambooHTML(HTMLParser):
    artifacts_list = list()

    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            for name, value in attrs:
                if name == 'href':
                    self.artifacts_list.append(value)
```

My entire flow is to use requests to do a GET on the url that you can browse to in a normal browser, e.g.

```
https://bamboo/artifact/PROJ-ECT/shared/build-latest/example-folder/
```

(I'm using dummy values there, substitute your bamboo url, project key, and folder, but importantly it has to be a url that shows the files you want to parse/download)

In the example below I'll search for a hypothetical file with 'xxxyyy' in the name, but the version changes each build: (this is massively cut down, but it gives you the gist)

```
r = session.get(url, timeout=30)
parser = ParseBambooHTML()
parser.feed(r.text)
parser.close()

for line in parser.artifacts_list:
  if 'xxxyyy' in line: # '/artifact/PROJ-ECT/shared/build-latest/example-folder/xxxyyy_21.2.17.123.deb'
    artifact_uri = line
    artifact_filename = artifact_uri.split('/')[-1] # 'xxxyyy_21.2.17.123.deb' 
    break

```

With the uri + the file name, you can retrieve the file easily enough now.

Hopefully this added something to the discussion.

-Kelly