type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Create authenticated direct upload URL V2
* Direct uploads allow users to upload images without API keys. A common use case are web apps, client-side applications, or mobile devices where users upload content directly to Cloudflare Images. This method creates a draft record for a future image. It returns an upload URL and an image identifier. To verify if the image itself has been uploaded, send an image details request (accounts/:account_identifier/images/v1/:identifier), and check that the `draft: true` property is not present.
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
body: {
expiry?: string;
id?: string;
metadata?: { [k: string]: unknown };
requireSignedURLs?: boolean;
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v2/direct_upload`
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
formData.append(k, String(v));
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 383 days ago
type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Create authenticated direct upload URL V2
* Direct uploads allow users to upload images without API keys. A common use case are web apps, client-side applications, or mobile devices where users upload content directly to Cloudflare Images. This method creates a draft record for a future image. It returns an upload URL and an image identifier. To verify if the image itself has been uploaded, send an image details request (accounts/:account_identifier/images/v1/:identifier), and check that the `draft: true` property is not present.
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
body: {
expiry?: string;
id?: string;
metadata?: { [k: string]: unknown };
requireSignedURLs?: boolean;
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v2/direct_upload`
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
formData.append(k, String(v));
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 810 days ago
type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Create authenticated direct upload URL V2
* Direct uploads allow users to upload images without API keys. A common use case are web apps, client-side applications, or mobile devices where users upload content directly to Cloudflare Images. This method creates a draft record for a future image. It returns an upload URL and an image identifier. To verify if the image itself has been uploaded, send an image details request (accounts/:account_identifier/images/v1/:identifier), and check that the `draft: true` property is not present.
*/
export async function main(auth: Cloudflare, account_identifier: string) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v2/direct_upload`
);
const response = await fetch(url, {
method: "POST",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 920 days ago