0

Download a file

by
Published Aug 8, 2022

Download a file in gdrive as a base64 in gdrive to be used in another step

Script gdrive Verified

The script

Submitted by hugo989 Bun
Verified 7 days ago
1
type Gdrive = {
2
  token: string;
3
};
4
export async function main(gdrive_auth: Gdrive, fileId: string) {
5
  const DOWNLOAD_FILE_URL = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
6

7
  const token = gdrive_auth["token"];
8

9
  const response = await fetch(DOWNLOAD_FILE_URL, {
10
    method: "GET",
11
    headers: {
12
      Authorization: "Bearer " + token,
13
      Accept: "application/json",
14
    },
15
  });
16

17
  if (response) {
18
    const arrayBuffer = await response.arrayBuffer();
19
    return Buffer.from(arrayBuffer).toString("base64");
20
  }
21
}
22

Other submissions
  • Submitted by fatonramadani Deno
    Created 399 days ago
    1
    import { encode } from "https://deno.land/[email protected]/encoding/base64.ts";
    2
    
    
    3
    type Gdrive = {
    4
      token: string;
    5
    };
    6
    export async function main(gdrive_auth: Gdrive, fileId: string) {
    7
      const DOWNLOAD_FILE_URL = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
    8
    
    
    9
      const token = gdrive_auth["token"];
    10
    
    
    11
      const response = await fetch(DOWNLOAD_FILE_URL, {
    12
        method: "GET",
    13
        headers: {
    14
          Authorization: "Bearer " + token,
    15
          Accept: "application/json",
    16
        },
    17
      });
    18
    
    
    19
      if (response) {
    20
        const arrayBuffer = await response.arrayBuffer();
    21
        return encode(arrayBuffer);
    22
      }
    23
    }
    24