0

Download Attachment

by
Published 4 days ago

Download an attachment by sys_id and write it to Windmill object storage, returning the S3 file reference.

Script servicenow Verified

The script

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

4
function authHeader(auth: RT.Servicenow) {
5
  return auth.token
6
    ? `Bearer ${auth.token}`
7
    : `Basic ${btoa(`${auth.username}:${auth.password}`)}`
8
}
9

10
/**
11
 * Download Attachment
12
 * Download an attachment's binary content by its sys_id and write it to Windmill object storage, returning the S3 file reference.
13
 */
14
export async function main(
15
  auth: RT.Servicenow,
16
  attachment_sys_id: string,
17
  output_s3_path: string
18
) {
19
  const response = await fetch(
20
    `${auth.instance_url}/api/now/attachment/${attachment_sys_id}/file`,
21
    {
22
      method: "GET",
23
      headers: {
24
        Authorization: authHeader(auth),
25
      },
26
    }
27
  )
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  const output: S3Object = { s3: output_s3_path }
34
  await wmill.writeS3File(output, await response.blob())
35
  return output
36
}
37