0

Download content from a file item in a file array column

by
Published Apr 8, 2025

Retrieves file content from an array by file ID

Script xata Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Xata = {
3
	apiKey: string
4
	workspaceUrl: string
5
}
6
/**
7
 * Download content from a file item in a file array column
8
 * Retrieves file content from an array by file ID
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	table_name: string,
14
	record_id: string,
15
	column_name: string,
16
	file_id: string
17
) {
18
	const url = new URL(
19
		`${auth.workspaceUrl}/db/${db_branch_name}/tables/${table_name}/data/${record_id}/column/${column_name}/file/${file_id}`
20
	)
21

22
	const response = await fetch(url, {
23
		method: 'GET',
24
		headers: {
25
			Authorization: 'Bearer ' + auth.apiKey
26
		},
27
		body: undefined
28
	})
29
	if (!response.ok) {
30
		const text = await response.text()
31
		throw new Error(`${response.status} ${text}`)
32
	}
33
	return await response.text()
34
}
35