0
Upload a download artifact
One script reply has been approved by the moderators Verified

Upload new download artifacts.

To upload files, perform a multipart/form-data POST containing one or more files fields:

$ echo Hello World > hello.txt
$ curl -s -u evzijst -X POST https://api.bitbucket.org/2.0/repositories/evzijst/git-tests/downloads -F files=@hello.txt

When a file is uploaded with the same name as an existing artifact, then the existing file will be replaced.

Created by hugo697 198 days ago Viewed 5901 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 198 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Upload a download artifact
7
 * Upload new download artifacts.
8

9
To upload files, perform a `multipart/form-data` POST containing one
10
or more `files` fields:
11

12
    $ echo Hello World > hello.txt
13
    $ curl -s -u evzijst -X POST https://api.bitbucket.org/2.0/repositories/evzijst/git-tests/downloads -F files=@hello.txt
14

15
When a file is uploaded with the same name as an existing artifact,
16
then the existing file will be replaced.
17
 */
18
export async function main(
19
  auth: Bitbucket,
20
  repo_slug: string,
21
  workspace: string
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/downloads`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.text();
39
}
40