0
Upload videos from a URL
One script reply has been approved by the moderators Verified

Uploads a video to Stream from a provided URL.

Created by hugo697 151 days ago Viewed 3721 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 151 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Upload videos from a URL
8
 * Uploads a video to Stream from a provided URL.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  Upload_Creator: string,
14
  Upload_Metadata: string,
15
  body: {
16
    allowedOrigins?: string[];
17
    creator?: string;
18
    meta?: { [k: string]: unknown };
19
    requireSignedURLs?: boolean;
20
    scheduledDeletion?: string;
21
    thumbnailTimestampPct?: number;
22
    url: string;
23
    watermark?: { uid?: string; [k: string]: unknown };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream/copy`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Upload-Creator": Upload_Creator,
35
      "Upload-Metadata": Upload_Metadata,
36
      "X-AUTH-EMAIL": auth.email,
37
      "X-AUTH-KEY": auth.key,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49