0

Download Attachment

by
Published Nov 2, 2023

Download an attachment by its ID (from an issue's `fields.attachment[].id`) and write it to Windmill object storage, returning the S3 file reference.

Script jira Verified

The script

Submitted by hugo989 Bun
Verified 4 hours ago
1
import * as wmill from "windmill-client"
2
import { S3Object } from "windmill-client"
3

4
/**
5
 * Download Attachment
6
 * Download an attachment by its ID (from an issue's `fields.attachment[].id`) and write it to Windmill object storage, returning the S3 file reference.
7
 */
8
export async function main(
9
  auth: RT.Jira,
10
  attachment_id: string,
11
  output_s3_path: string
12
) {
13
  const url = new URL(
14
    `https://${auth.domain}.atlassian.net/rest/api/3/attachment/content/${attachment_id}`
15
  )
16
  const response = await fetch(url, {
17
    method: "GET",
18
    headers: {
19
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
20
    },
21
  })
22

23
  if (!response.ok) {
24
    throw new Error(`${response.status} ${await response.text()}`)
25
  }
26

27
  const output: S3Object = { s3: output_s3_path }
28
  await wmill.writeS3File(output, await response.blob())
29
  return output
30
}
31

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Get attachment content
    8
     * Returns the contents of an attachment.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      id: string,
    13
      redirect: string | undefined
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.domain}.atlassian.net/rest/api/2/attachment/content/${id}`
    17
      );
    18
      for (const [k, v] of [["redirect", redirect]]) {
    19
        if (v !== undefined && v !== "") {
    20
          url.searchParams.append(k, v);
    21
        }
    22
      }
    23
      const response = await fetch(url, {
    24
        method: "GET",
    25
        headers: {
    26
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36