Drive Api

A Versatile Approach to Uploading Files with Node.js: Integrating Gemini, Drive, YouTube, and Other APIs

Gists

Abstract

A script using resumable upload with file streams is proposed to enhance file handling within the Gemini Generative AI API for Node.js. This script allows uploading from web URLs and local storage, efficiently handles large files, and offers potential reusability with other Google APIs.

Description

The @google/generative-ai library provides a powerful way to interact with the Gemini Generative AI API using Node.js. This enables developers to programmatically generate creative text formats, translate languages, write different kinds of creative content, and answer your questions in an informative way, all powered by Gemini’s advanced AI models. Ref

Analyzing Folder Structures with Google Apps Script

Gists

Abstract

This report provides a Google Apps Script to retrieve all files, including those within subfolders, for a designated folder. It addresses the challenges of retrieving files within deeply nested folder structures and obtaining complete file paths.

Introduction

Google Apps Script empowers developers to interact with Google Drive data programmatically, unlocking a wide range of functionalities. A core component of this interaction is the Drive service (DriveApp) and Drive API. These services provide programmatic control over files and folders within your Drive.

Move Folder including Files and Folders in Google Drive using Google Apps Script

Gists

Abstract

This script automates moving a folder including files and folders between Google Drives (personal or shared) by recreating the folder structure and transferring files, overcoming limitations for shared drives.

Introduction

This report addresses a common challenge: efficiently moving folders, including their subfolders and files, between Google Drives, encompassing both personal and shared drives using a script. While Google Drive offers straightforward methods for moving individual files between any drives, directly moving entire folders containing subfolders presents limitations, particularly when shared drives are involved. This script bridges that gap by providing a reliable Google Apps Script solution for such scenarios.

Comparing File Contents of Files on Google Drive using Google Apps Script

Gists

This is a sample script for comparing the file contents of files on Google Drive using Google Apps Script.

Sample script

Before you use this script, please enable Drive API at Advanced Google services. And also, please set the file IDs you want to check whether the file contents of the files are the same.

function checkFiles_(f, checks = ["md5Checksum", "sha1Checksum", "sha256Checksum"]) {
  files = f.map(id => DriveApp.getFileById(id));
  const fields = [...checks, "id"].join(",");
  const o = files.reduce((o, f) => {
    const mimeType = f.getMimeType();
    if (["FOLDR", "SHORTCUT"].some(m => mimeType == MimeType[m])) {
      throw new Error("Folders cannot be checked.");
    }
    let obj;
    if (mimeType.includes("application/vnd.google-apps")) {
      const name = f.getName();
      f.setName("temp");
      Utilities.sleep(2000);
      obj = Drive.Files.insert({ title: "temp", mimeType: MimeType.PDF }, f.getBlob(), { supportsAllDrives: true, fields });
      f.setName(name);
      Drive.Files.remove(obj.id); // If an error occurs, please use DriveApp.getFileById(obj.id).setTrashed(true);
    } else {
      obj = Drive.Files.get(f.getId(), { supportsAllDrives: true, fields });
    }
    checks.forEach(e => o[e] = o[e] ? [...o[e], obj[e]] : [obj[e]]);
    return o;
  }, {});
  return Object.values(o).every(e => [...new Set(e)].length == 1);
}

// Please run this function.
function main() {
  const file1 = "###fileId1###"; // Please set your file ID of file 1.
  const file2 = "###fileId2###"; // Please set your file ID of file 2.

  const check = checkFiles_([file1, file2]);
  const res = `"${file1}" and "${file2}" are${
    check ? " " : " not "
  }the same data.`;
  console.log(res);
}
  • When this script is run, when the file contents of your inputted files are the same, true is returned.

Resumable Download of File from Google Drive using Drive API with Python

Gists

This is a sample script for achieving the resumable download of a file from Google Drive using Dive API with Python.

There might be a case in that you want to achieve the resumable download of a file from Google Drive using Dive API with Python. For example, when a large file is downloaded, the downloading might be stopped in the middle of downloading. At that time, you might want to resume the download. In this post, I would like to introduce the sample script of python.

Retrieving Total File Sizes in Specific Folder of Google Drive using Google Apps Script

Gists

This is a sample script for retrieving the total file sizes in the specific folder of Google Drive using Google Apps Script.

There is a case where you want to retrieve the total file sizes in the specific folder of Google Drive using Google Apps Script. In this post, I would like to introduce a sample script for achieving this.

Sample script

Before you use this script, please enable Drive API at Advanced Google services. And please install FilesApp of a Google Apps Script library.

Transferring Owner of File to Other User using Google Apps Script

Gists

This is a sample script for transferring the ownership of a file to another user using Google Apps Script.

In the current stage, about the consumer account (gmail.com) the specification for transferring the ownership of a file has been changed as follows. Ref

  1. The current owner initiates an ownership transfer by creating or updating the prospective new owner’s file permission. The permission must include these settings: role=writer, type=user, and pendingOwner=true. If the new owner is creating a permission for the prospective owner, an email notification is sent to the prospective new owner indicating that they’re being asked to assume ownership of the file.
  1. The new owner accepts the ownership transfer request by creating or updating their file permission. The permission must include these settings: role=owner and transferOwnership=true. If the new owner is creating a new permission, an email notification is sent to the previous owner indicating that ownership has been transferred.

When this flow is reflected in the sample script, it becomes as follows.

Workaround: createdDate cannot be used with searchFiles of DriveApp in Google Apps Script

Gists

Unfortunately, in the current stage, in order to retrieve the file list using the created date, the search query of createdDate cannot be used with searchFiles method of DriveApp in Google Apps Script. This has already been reported at this issue tracker In this post, I would like to introduce a workaround for searching the files using the created date.

Issue and workaround

  • The parameter of “searchFiles” method of DriveApp uses the search query for Drive API v2. When I tested createdDate > '####-##-##' for “searchFiles” and “Files: list” of Drive API v2, I confirmed errors like Invalid argument: q and Invalid query occurred, respectively.

Sample Script for Resumable Upload to Google Drive using Axios with Node.js

Gists

This is a sample script for the resumable upload using Axios with Node.js.

Sample script

In this sample script, as a sample situation in order to explain the resumable upload, the file data is loaded from the local PC, and the data is uploaded to Google Drive with the resumable upload.

const axios = require("axios");
const fs = require("fs").promises;

async function sample() {
  const filepath = "./###"; // Please set the filename and file path of the upload file.

  const new_access_token = "###"; // Please set your access token.
  const name = "###"; // Please set the filename on Google Drive.
  const mimeType = "###"; // Please set the mimeType of the uploading file. I thought that when this might not be required to be used.

  // 1. Prepare chunks from loaded file data.
  const split = 262144; // This is a sample chunk size.
  const data = await fs.readFile(filepath);
  const fileSize = data.length;
  const array = [...new Int8Array(data)];
  const chunks = [...Array(Math.ceil(array.length / split))].map((_) =>
    Buffer.from(new Int8Array(array.splice(0, split)))
  );

  // 2. Retrieve endpoint for uploading a file.
  const res1 = await axios({
    method: "POST",
    url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
    headers: {
      Authorization: `Bearer ${new_access_token}`,
      "Content-Type": "application/json",
    },
    data: JSON.stringify({ name, mimeType }),
  });
  const { location } = res1.headers;

  // 3. Upload the data using chunks.
  let start = 0;
  for (let i = 0; i < chunks.length; i++) {
    const end = start + chunks[i].length - 1;
    const res2 = await axios({
      method: "PUT",
      url: location,
      headers: { "Content-Range": `bytes ${start}-${end}/${fileSize}` },
      data: chunks[i],
    }).catch(({ response }) =>
      console.log({ status: response.status, message: response.data })
    );
    start = end + 1;
    if (res2?.data) console.log(res2?.data);
  }
}

sample();

Testing

When this sample script is run, the following result is obtained.

Uploading Files to Google Drive with Asynchronous Process using Python

Gists

This is a sample script for uploading files to Google Drive with asynchronous process using Python.

Sample script

import aiohttp
import asyncio
import json

folder_id = "###" # Please set the folder ID you want to put.
token = "###" # Please set your access token.
url = "https://www.googleapis.com/upload/drive/v3/files"


async def workers(file):

    async with aiohttp.ClientSession() as session:
        metadata = {"name": file["filename"], "parents": [folder_id]}
        data = aiohttp.FormData()
        data.add_field("metadata", json.dumps(metadata), content_type="application/json; charset=UTF-8")
        data.add_field("file", open(file["path"], "rb"))
        headers = {"Authorization": "Bearer " + token}
        params = {"uploadType": "multipart"}
        async with session.post(url, data=data, params=params, headers=headers) as resp:
            return await resp.json()


async def main():
    # Please set the filenames and the file paths as follows.
    fileList = [
        {"filename": "sample1", "path": "./sample1.png"},
        ,
        ,
        ,
    ]
    works = [asyncio.create_task(workers(e)) for e in fileList]
    res = await asyncio.gather(*works)
    print(res)


asyncio.run(main())
  • When this script is run, the files of fileList are uploaded to Google Drive with the asynchronous process.

Note

  • This sample supposes that your access token can be used for uploading files to Google Drive using Drive API. Please be careful about this.

Reference

Specification of Search Query for File List Method in Drive API

Gists

In this report, I would like to report about the current specification of the search query for the file list method in Drive API.

Recently, I noticed that the specification of the search query for the file list method in Drive API might have been changed. I thought that to know the change of specification of the search query is important for creating the application using Drive API. In this report, I would like to introduce the current specification of the search query.

Simple Script of Resumable Upload with Google Drive API for Axios

Gists

This is a simple sample script for achieving the resumable upload to Google Drive using Axios. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL.

In this sample, a text data is uploaded with the resumable upload using a single chunk.

Downloading and Uploading File to Google Drive without Saving File with Stream and Resumable Upload using Node.js

Gists

This is a sample script of Node.js for downloading the data and uploading the data to Google Drive with the resumable upload without saving it as a file. The downloaded data is uploaded to Google Drive with the stream.

Sample script

Before you use this, please set the variables of accessToken, url, fileSize, mimeType and filename. In this case, fileSize is required to set because the data is uploaded with the resumable upload.

GAS Library - GASProjectApp

Overview

This is a Google Apps Script library for creating, updating and exporting Google Apps Script project of the standalone type using Drive API. In this case, Apps Script API is not used.

Description

I had reported “Drive API cannot create Google Apps Script project no longer” before. Ref About this, I had reported the future request. Ref At July 30, 2020, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API again. Ref This is a good news for me. By this, in order to use this with Google Apps Script, I created this library. Because in this case, when the update method is used, the special scope of https://www.googleapis.com/auth/drive.scripts is required. So I thought that when this is published as the Google Apps Script library, this will be useful for users.

Drive API got to be able to create Google Apps Script project again

Gists

I have reported “Drive API cannot create Google Apps Script project no longer”. Ref About this, I had reported the future request. Ref Today, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API. This is a good news for me. By this, the following 2 patterns can be used from now.

Pattern 1:

  1. Create new standalone GAS project by Apps Script API.
  2. Put the local script to the created GAS project by updating the project with Apps Script API.
  3. Move the GAS project from the root folder to the specific folder using Drive API.

In this pattern, 3 API calls are required.

Uploading Files of multipart/form-data to Google Drive using Drive API with Node.js

Gists

These are the sample scripts for uploading files of multipart/form-data to Google Drive using Drive API with Node.js. In this case, googleapis for Node.js is not used.

In these sample script, the maximum file size is 5 MB. Please be careful this. When you want to upload the files more than 5 MB, please check this report.

Sample script 1

This sample script uploads a file using the modules of fs and request. Before you use this script, please prepare your access token for uploading the file.

GAS Library - OwnershipTransfer

Overview

This is a Google Apps Script library for achieving the ownership-transfer of the specific folder including the files and sub-folders using Drive API.

IMPORTANT: PLEASE BE CAREFUL THIS.

At first, please read this section

I cannot take responsibility for the problems occurred by this library. So when you use this library, please use it by according to your own decision and at your own responsibility.

This GAS library transfers the ownership of files and folders. For example, when the ownership of a file is transferred to other account, the file cannot be managed by the original user. And also, when the ownership of the specific folder is transferred to other account, all files ans sub-folders in the specific folder cannot be managed by the original user. PLEASE BE CAREFUL THIS. So when you use this library, at first, please test this library using a sample file and folder. PLEASE BE CAREFUL THIS.

And also, the specification of ownership transfer might be updated by Google side in the future update. Because when I had tested this library, I could notice that the specification had been changed. Ref By this, I noticed that the transferred files had no parent folders, and also, the script was required to be modified. From this situation, when you use this script for your important files and folders, please be careful this. At first, PLEASE TEST THIS LIBRARY USING A SIMPLE FILE AND FOLDER.

Description

Recently, I had the situation that it is required to transfer the ownership of a folder including the files and sub-folders. When I manually transferred the ownership of the top folder from my account to other user’s account, although the ownership of top folder could be transferred, my account is left as the writer. And also, the ownership of the files and sub-folders in the top folder was not changed, while the user is the writer. Although I do the same situation using the method of create.permissions in Drive API, the same result was obtained. In the current stage, unfortunately, by the simple method, the complete ownership-transfer of the file including the files and sub-folders cannot be achieved. From this situation, I created the script for achieving the complete ownership-transfer of the files and folders as a Google Apps Script library.

Specification of Files: copy in Drive API was changed

I noticed that the specification of Files: copy in Drive API was changed. About the method of Files: copy in Drive API v3, this method could be used with the scope of https://www.googleapis.com/auth/drive.readonly until July 5, 2020. But I confirmed that in the current stage, the scope is required to be changed to the following scopes.

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/drive.appdata
  • https://www.googleapis.com/auth/drive.photos.readonly

Please be careful this.

Report: Processing to Create New File to Specific Folder using Drive API

Gists

In this report, I would like to report for processing to create new file to the specific folder using Drive API. When the new file is created to the specific folder using Drive API, the property of parents with the value of folder ID is included in the request body of the method “Files: create”. About this process, I had thought that the file is directly created to the specific folder. But, I could confirm that the new file is created by the following process.

Batch Requests for Drive API using Google Apps Script

Overview

These are the sample scripts of the batch requests for Drive API using Google Apps Script.

Description

When we want to manage the files and folders on Google Drive, we have 2 ways. One is the use of Drive service. Another is the use of Drive API. In the case of them, when we want to manage a lot of files and folders, unfortunately, both ways have no batch requests. Namely, for example, the metadata of a lot of files are changed, the methods in Class File and Files: update are required to be used in the loop. In this case, the process cost will be high. On the other hand, Drive API can use the batch requests. But in this case, in order to use this batch requests with Google Apps Script, it is required to create the request body of multipart/mixed by each user. Because there are no methods for automatically requests the batch requests. From this situation, here, I would like to introduce the simple sample scripts for creating, updating and deleting the files and folders on Google Drive using the batch requests with Google Apps Script.

Creating Shortcut on Google Drive using Google Apps Script

Gists

This is a sample script for creating a shortcut on Google Drive using Google Apps Script.

Sample script

Before you run the script, please enable Drive API at Advanced Google services.

function createShortcut(targetId, name, folderId) {
  const resource = {
    shortcutDetails: { targetId: targetId },
    title: name,
    mimeType: "application/vnd.google-apps.shortcut",
  };
  if (folderId) resource.parents = [{ id: folderId }];
  const shortcut = Drive.Files.insert(resource);
  return shortcut.id;
}

// Please run this function.
function main() {
  const targetId = "###"; // Please set the ID of target file or folder.
  const shortcutName = "###"; // Please set the shortcut name.
  const folderId = "###"; // Please set the folder ID for putting the created shortcut.
  const id = createShortcut(targetId, shortcutName, folderId);
  console.log(id);
}

References

GAS Library - CopyFolder

Overview

This is Google Apps Script library for copying folder on Google Drive.

Description

I have sometimes the situation that it is required to back up the folder on Google Drive to Google Drive. But unfortunately, the method of makeCopy() of Class File and the method of Files: copy of Drive API cannot be used for directly copying the folder on Google Drive. So I created this as a library. This library can copy the folder on Google Drive. All files in the folder can be copied to Google Drive with keeping the folder structure. When there are the files with newer modified time in the source folder than those in the destination folder, the files in the destination folder are overwritten by the newer files in the source folder. Also, in this library, the folders in the shared Drive and the publicly shared folders can be used as the source and destination folder.

Updating a File with Resumable Upload using Drive API

Gists

This is a sample flow for updating a file with the resumable upload using Drive API.

Sample situation:

In this answer, as a sample situation, it supposes that a text file in Google Drive is updated by the resumable upload with the multiple chunks. And as the method for requesting, I use the curl command.

I prepared 2 files for 2 chunks. As the test situation, the 2 chunks of 262,144 bytes and 37,856 bytes are uploaded. So total upload size is 300,000 bytes. Those filenames are data1.txt and data2.txt, respectively.

Retrieving Files and Folders without Parents in Google Drive

Gists

This is a sample script for retrieving the files and folders which have no parents in own Google Drive.

When you use this script, please enable Drive API at Advanced Google services.

Sample script

const myFunction = () => {
  const token = ScriptApp.getOAuthToken();
  const fields = decodeURIComponent(
    "nextPageToken,files(name,id,mimeType,parents)"
  );
  const q = decodeURIComponent("'me' in owners and trashed = false");
  let files = [];
  let pageToken = "";
  do {
    const res = UrlFetchApp.fetch(
      `https://www.googleapis.com/drive/v3/files?pageSize=1000&fields=${fields}&q=${q}&pageToken=${pageToken}`,
      { headers: { authorization: `Bearer ${token}` } }
    );
    const obj = JSON.parse(res);
    Array.prototype.push.apply(files, obj.files);
    pageToken = obj.nextPageToken;
  } while (pageToken);
  const result = files.filter(({ parents }) => !parents);
  console.log(result);
};

When you run the script, the files and folders which have no parents in own Google Drive are retrieved.

Drive API cannot create Google Apps Script project no longer

Gists

Today, I noticed that new Google Apps Script project of the standalone script type cannot be created by the method of Files: create in Drive API. From now, in order to manage the Google Apps Script project, only Google Apps Script API is required to be used. By this, the following issues are brought.

  • When the new standalone GAS project is created in the specific folder by uploading the local script, the following flow is required to be run.
    1. Create new standalone GAS project by Apps Script API.
    2. Put the local script to the created GAS project by updating the project with Apps Script API.
    3. Move the GAS project from the root folder to the specific folder using Drive API.

From now, 3 API calls are required to be used like above. By the way, this had been able to be achieved by one API call of the method of files.create in Drive API before.

Simple Script of Resumable Upload with Google Drive API for Python

Gists

This is a simple sample script for achieving the resumable upload to Google Drive using Python. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL.

In this sample, a PNG file is uploaded with the resumable upload using a single chunk.

Simple Script of Resumable Upload with Google Drive API for Node.js

Gists

This is a simple sample script for achieving the resumable upload to Google Drive using Node.js. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL.

In this sample, a PNG file is uploaded with the resumable upload using a single chunk.

Music Streaming Player for Discord with Google Drive using Node.js

Gists

Overview

This is a sample script for the music streaming player for Discord with Google Drive using Node.js.

Description

This sample script can achieve to play the music when uses the voice channel on Discord. The music files are put in the specific folder of Google Drive. This script downloads all music files and plays the music files at the voice channel with the stream.

Usage

1. Register BOT to Discord

At first, please register your BOT to Discord. And please retrieve the token.

Modifying Revisions of a File on Google Drive using Google Apps Script

Gists

This is a sample script for modifying the revisions of a file on Google Drive using Google Apps Script. This script can be used for not only Google Docs files, but also the files except for Google Docs.

Issue and workaround:

Unfortunately, in the current stage, at Google Docs files, the revision of Google Docs cannot be directly changed by APIs with a script. So as one of several workarounds, I would like to propose to overwrite the Google Docs file using the exported data. On the other hand, at the files except for Google Docs, the data can be directly retrieved with the revision ID. This can be used for overwriting the file. The flow of this script is as follows.

Creating New Google Docs and Overwriting Existing Google Docs by Text with Node.js without using googleapis

Gists

There are 2 sample scripts.

  1. Create new Spreadsheet using a text value as CSV data.
  2. Overwrite the existing Google Document using a text value.

When you use these script, please enable Drive API and retrieve your access token.

Create New Spreadsheet using Text Value

const request = require('request');

const textData = "a1, b1, c1, d1, e1"; // This is used as CSV data.
const orgMimeType = "text/csv";
const orgFileName = "sample.csv";
const accessToken = "###"; // Access token
const metadata = {
  name: "convertedSampleCSV",
  mimeType: "application/vnd.google-apps.spreadsheet",
};

const url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
const boundary = "xxxxxxxxxxx";
var data = "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"metadata\"\r\n";
  data += "Content-Type: application/json; charset=UTF-8\r\n\r\n";
  data += JSON.stringify(metadata) + "\r\n";
  data += "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + orgFileName + "\"\r\n";
  data += "Content-Type: " + orgMimeType + "\r\n\r\n";

var payload = Buffer.concat([
  Buffer.from(data, "utf8"),
  new Buffer(textData, 'binary'),
  Buffer.from("\r\n--" + boundary + "--", "utf8"),
]);

const options = {
  method: 'post',
  url: url,
  headers: {
    "Content-Type": "multipart/related; boundary=" + boundary,
    'Authorization': 'Bearer ' + accessToken,
  },
  body: payload,
};
request(options, (error, response, body) => {
  console.log(body);
});

Overwrite Existing Google Document using Text Value

This can be used when you don’t want to update the existing Google Docs without changing the file ID.

Upload Files to Google Drive using Javascript

Gists

News

At October 11, 2019, I published a Javascript library to to run the resumable upload for Google Drive. When this is used, the large file can be uploaded. You can also use this js library.

Description

This is a sample script for uploading files to Google Drive using Javascript. The files are uploaded by Drive API v3. gapi.client.drive.files.create() can create an empty file on Google Drive. But it cannot directly upload files including contents. I think that this might not be able to upload files and metadata with the multipart/related, although this might be resolved by the future update. So now, as one of workarounds, I use using XMLHttpRequest.

Create Folder Tree of Google Drive using Node.js

Gists

This is a sample script for retrieving a folder tree using Node.js. In this sample, you can set the top of folder for the folder tree. In generally, the folder tree is created by retrieving folders from the top folder in order. For example, when Google Apps Script is used, the script becomes like this. But when Drive API is used for this situation, if there are a lot of folders in the top folder, a lot of APIs are required to be called. So in this sample, I have tried to create the folder tree by a small number of API calls as possible.

Uploading Files From Local To Google Drive by Python without Quickstart

Gists

This is a sample script for uploading files from local PC to Google Drive using Python. In this sample, Quickstart is not used. So when you use this script, please retrieve access token.

Curl sample :

curl -X POST \
    -H "Authorization: Bearer ### access token ###" \
    -F "metadata={name : 'sample.png', parents: ['### folder ID ###']};type=application/json;charset=UTF-8" \
    -F "file=@sample.png;type=image/png" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

]

Python sample :

When above curl sample is converted to Python, it becomes as follows.

Downloading Files Under Specific Folder using Node.js

Gists

This sample script is for downloading files under a specific folder using Node.js. It can download files with Google Docs and others.

This sample supposes as follows. So please confirm it.

  • quickstart is used and default quickstart works fine.

In order to use this sample, please carry out as follows.

  1. Replace listFiles() of the default quickstart to this sample.
  2. Set folderid. This script can retrieve files in the folder with folderid.
  3. Delete drive-nodejs-quickstart.json. I think that there is the file at .credentials in your home directory.
  4. Change the SCOPE from var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']; to var SCOPES = ['https://www.googleapis.com/auth/drive.readonly'];.
  5. Run script, retrieve the code and authorize.

Script :

function listFiles(auth) {
  var folderid = "### folder ID ###"; // Folder ID. This script downloads files in the folder with this folder ID.
  var outputExtension = "pdf"; // Extension of output file. This is adapted to only Google Docs.

  var outputMimeType = mime.lookup(outputExtension);
  var service = google.drive('v3');
  service.files.list({
    auth: auth,
    q: "'" + folderid + "' in parents and trashed=false",
    fields: "files(id, name, mimeType)"
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    response.files.forEach(function(e){
      if (e.mimeType.includes("application/vnd.google-apps")) {
        var dlfile = fs.createWriteStream(e.name + "." + outputExtension);
        service.files.export({
          auth: auth,
          fileId: e.id,
          mimeType: outputMimeType
        }).on('end', function() {
          console.log("'%s' was downloaded as %s.", e.name, outputExtension);
        }).on('error', function(err) {
          console.error(err);
          return process.exit();
        }).pipe(dlfile);
      } else {
        var dlfile = fs.createWriteStream(e.name);
        service.files.get({
          auth: auth,
          fileId: e.id,
          alt: 'media'
        }).on('end', function() {
          console.log("'%s' was downloaded as %s.", e.name, mime.extension(e.mimeType));
        }).on('error', function(err) {
          console.error(err);
          return process.exit();
        }).pipe(dlfile);
      }
    });
  });
}

Retrieving HTML File ID from Microsoft Docx File on Google Drive

This sample script converts from Microsoft Docx File on Google Drive to Google Spreadsheet, and converts to HTML file.

Drive APIs v2 and v3 are used for this. Please set as follows.

“Drive API v2” can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.

How to use it is as follows.

  1. In the script editor, select Resources > Advanced Google services

OCR using Google Drive API

This is a sample script for OCR using Google Drive API. A text file which converted by OCR can be retrieved by inputting an image file.

In this sample, Python Quickstart is used. The detail information is https://developers.google.com/drive/v3/web/quickstart/python. Please read “Step 1: Turn on the Drive API” and “Step 2: Install the Google Client Library”.

from __future__ import print_function
import httplib2
import os
import io

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient.http import MediaFileUpload, MediaIoBaseDownload

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    credential_path = os.path.join("./", 'drive-python-quickstart.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    imgfile = 'sample.png'  # Image with texts (png, jpg, bmp, gif, pdf)
    txtfile = 'output.txt'  # Text file outputted by OCR

    mime = 'application/vnd.google-apps.document'
    res = service.files().create(
        body={
            'name': imgfile,
            'mimeType': mime
        },
        media_body=MediaFileUpload(imgfile, mimetype=mime, resumable=True)
    ).execute()

    downloader = MediaIoBaseDownload(
        io.FileIO(txtfile, 'wb'),
        service.files().export_media(fileId=res['id'], mimeType="text/plain")
    )
    done = False
    while done is False:
        status, done = downloader.next_chunk()

    service.files().delete(fileId=res['id']).execute()
    print("Done.")


if __name__ == '__main__':
    main()

Converting PDF to TXT

This is a sample script for converting a PDF file to a TXT file. 2 steps are required for this.

  1. Upload a PDF file as a Google Document
  2. Download a Google Document as a TXT file

In this sample, Python Quickstart is used. The detail information is https://developers.google.com/drive/v3/web/quickstart/python. Please read “Step 1: Turn on the Drive API” and “Step 2: Install the Google Client Library”.

from __future__ import print_function
import httplib2
import os
import io

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient.http import MediaFileUpload, MediaIoBaseDownload

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'


def get_credentials():
    credential_path = os.path.join("./", 'drive-python-quickstart.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    pdffile = 'sample.pdf'
    txtfile = 'sample.txt'

    mime = 'application/vnd.google-apps.document'
    res = service.files().create(
        body={
            'name': pdffile,
            'mimeType': mime
        },
        media_body=MediaFileUpload(pdffile, mimetype=mime, resumable=True)
    ).execute()

    dl = MediaIoBaseDownload(
        io.FileIO(txtfile, 'wb'),
        service.files().export_media(fileId=res['id'], mimeType="text/plain")
    )
    done = False
    while done is False:
        status, done = dl.next_chunk()
    print("Done.")


if __name__ == '__main__':
    main()

Creating Spreadsheet from Excel file

These scripts can be executed on Script Editor. But, in order to use these, you have to enable Drive API of Advanced Google services and of Google API Console. “Drive API v2” can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.

How to use it is as follows.

  1. In the script editor, select Resources > Advanced Google services

  2. In the dialog that appears, click the on/off switch for Drive API v2.

Creating Downloaded Excel file as Spreadsheet

This is a sample GAS script to create an Excel file, which was downloaded from web, as Spreadsheet. By using Drive API, it can be achieved without access token.

Script :

function downloadFile(fileURL, folder) {
  var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
  var response = UrlFetchApp.fetch(fileURL);
  var rc = response.getResponseCode();
  var blob = response.getBlob();
  var resource = {
    "mimeType": "application/vnd.google-apps.spreadsheet",
    "parents": [{id: folder}],
    "title": filename
  };
  var res = Drive.Files.insert(resource, blob);
  var fileInfo = [rc, res.title, blob.getBytes().length, res.id];
  return fileInfo;
}

Result :

[
    200,
    sample.xlsx,
    10000.0,
    ## file id ##
]

How to use "fields" of Drive APIs

There are a lot of APIs on Google. When we use Google Drive APIs, they usually have “fields” as a resource. The parameter “fields” gives various information which is selected to us. This is one of important parameters. And this can be used at Google Apps Script (GAS) although that version is v2. About how to use it, there are some documents. But it is difficult to find how to use it at GAS. So I would like to write down here as a memorandum. Most parameters for APIs have to be expressed as JSON. However, the expressions are different for each API. I would like to introduce this using some samples. This is for GAS.

Retrieving Access Token for Google Drive API using GAS

These GASs retrieve an access token for using Google Drive API. There are 3 parts. Before you use this, please retrieve client ID, client secret and redirect uri from Google , and choose scopes.

1. Retrieving code from web

This is a script to output URL for retrieving “code” from web. Please retrieve “code” by import this URL to your browser. After you run this script, using “url” got from this script, it retrieves “code”.

File Upload and Download with File Convert For curl using Drive API

It is necessary to retrieve access token on Google. Scope is as follows.

https://www.googleapis.com/auth/drive

Other mimetypes can be seen here.

Download and convert from Spreadsheet to Excel

curl -X GET -sSL \
        -H "Authorization: Bearer [Your access token]" \
        -o "Excel file name" \
        "https://www.googleapis.com/drive/v3/files/[File ID]/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Upload and convert from Excel to Spreadsheet

curl -X POST -sSL \
        -H "Authorization: Bearer [Your access token]" \
        -F "metadata={ \
                     name : '[File name on Google Drive]', \
                     mimeType : 'application/vnd.google-apps.spreadsheet' \
                     };type=application/json;charset=UTF-8" \
        -F "file=@[Your Excel file];type=application/vnd.ms-excel" \
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"