Goal:
A repository page on Bitbucket has a download section. Via the web browser you can easily upload and download files which you be centraly shared with your team.
But how about downloading such a file programmatic like with cURL?
Maybe as part of a simple deployment pipeline, where the pipeline process stores artifacts in the repositories download directy so it can be later downloaded from a remote host…
Problem:
Uploading the files from within a bitbucket pipeline using curl wasnt a big deal, everything worked as described by Atlassian.
However, the download part from a remote machine got me stuck.
Atlassian explains, you must create an App Password and then you will be able to download files with a curl command this way:
$ curl --verbose -O -L --user <username>:<app_password> https://bitbucket.org/<team>/<repo>/downloads/<filename.ext>
This did fail with error „401 Unauthorized“.
How come, we used a proper App Password – didnt we?!
Solution:
In contrast to the inconsistent desriptions of Atlassian, one must use the Bitbucket API to fetch a download file instead.
Instead of:
=> „https://bitbucket.org/<team>/<repo>/downloads/<filename.ext>“
It should be:
=> „https://api.bitbucket.org/2.0/repositories/<team>/<repo>/downloads/<filename.ext>“
How To:
- Create a new app password with „Read“ permission to the repository

- Take note of your Team name, the repository name and the actual filename you want to download
- Go to your remote host where you want to download the file to
- execute the curl command
$ curl -O -L --user <username>:<app_password> https://api.bitbucket.org/2.0/repositories/<team>/<repo>/downloads/<filename.ext>
This way, it worked download any file from the repositories download directory.