Edits history of script submission #4961 for ' Upload an image (cloudflare)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Upload an image
     * Upload an image with up to 10 Megabytes using a single HTTP POST (multipart/form-data) request.
    An image can be uploaded by sending an image file or passing an accessible to an API url.
    
     */
    export async function main(
      auth: Cloudflare,
      account_identifier: string,
      body: {
        metadata?: { [k: string]: unknown };
        requireSignedURLs?: boolean;
        [k: string]: unknown;
      } & (
        | { file: { [k: string]: unknown }; [k: string]: unknown }
        | { url: string; [k: string]: unknown }
      )
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v1`
      );
    
      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

  • nativets
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Upload an image
     * Upload an image with up to 10 Megabytes using a single HTTP POST (multipart/form-data) request.
    An image can be uploaded by sending an image file or passing an accessible to an API url.
    
     */
    export async function main(
      auth: Cloudflare,
      account_identifier: string,
      body: {
        metadata?: { [k: string]: unknown };
        requireSignedURLs?: boolean;
        [k: string]: unknown;
      } & (
        | { file: { [k: string]: unknown }; [k: string]: unknown }
        | { url: string; [k: string]: unknown }
      )
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v1`
      );
    
      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

  • nativets
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Upload an image
     * Upload an image with up to 10 Megabytes using a single HTTP POST (multipart/form-data) request.
    An image can be uploaded by sending an image file or passing an accessible to an API url.
    
     */
    export async function main(auth: Cloudflare, account_identifier: string) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v1`
      );
    
      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