0

Creates a multipart external upload

by
Published Oct 17, 2025

Creates a multipart upload in the external storage provider, storing a temporary reference to the external upload similar to /get-presigned-put. You must have the correct permissions and CORS settings configured in your external provider. We support AWS S3 as the default. See: https://meta.discourse.org/t/-/210469#s3-multipart-direct-uploads-4. An external file store must be set up and `enable_direct_s3_uploads` must be set to true for this endpoint to function.

Script discourse Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Discourse = {
3
  apiKey: string;
4
  defaultHost: string;
5
  apiUsername: string;
6
};
7
/**
8
 * Creates a multipart external upload
9
 * Creates a multipart upload in the external storage provider, storing
10
a temporary reference to the external upload similar to /get-presigned-put.
11

12
You must have the correct permissions and CORS settings configured in your
13
external provider. We support AWS S3 as the default. See:
14

15
https://meta.discourse.org/t/-/210469#s3-multipart-direct-uploads-4.
16

17
An external file store must be set up and `enable_direct_s3_uploads` must
18
be set to true for this endpoint to function.
19

20

21
 */
22
export async function main(
23
  auth: Discourse,
24
  body: {
25
    upload_type:
26
      | "avatar"
27
      | "profile_background"
28
      | "card_background"
29
      | "custom_emoji"
30
      | "composer";
31
    file_name: string;
32
    file_size: number;
33
    metadata?: { "sha1-checksum"?: string };
34
  },
35
) {
36
  const url = new URL(`https://${auth.defaultHost}/uploads/create-multipart.json`);
37

38
  const response = await fetch(url, {
39
    method: "POST",
40
    headers: {
41
      "Content-Type": "application/json",
42
      "API-KEY": auth.apiKey,
43
      "API-USERNAME": auth.apiUsername,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53