Binoa Mitrosh
Binoa Mitrosh

Reputation: 33

How to get md5Checksum field of my own Google Drive files (by working with classes and objects only, without http-requests)?

I want to check md5 checksum of my uploaded files on google drive (within the same google account).

I wanted to do this using Google Apps Script (script.google.com).

I have read the documentation thoroughly but still don't understand how to get this metadata of files.

I found many examples with using webAPI (v2,v3), but it needs handling OAuth tokens, parsing http responses... why then is there Google Apps Script?

This is my working example that fetches a file id = file name from a folder named 'test', but I want to call md5Checksum attr...

function testDrive(){
  var collectionFolder = DriveApp.getFoldersByName('test');
  if (collectionFolder.hasNext()){
    var itemFolder = collectionFolder.next();
    var collectionFile = itemFolder.getFiles();
    while(collectionFile.hasNext()){
      var itemFile = collectionFile.next();
      Logger.log(itemFile.getId(),'=',itemFile.getName());
    }
  }
}

P.S.: I ever did my own checksum (sha256) function that processing every fetched file blob(binary) but it has limit of ~25mb per blob.

Upvotes: 3

Views: 4269

Answers (1)

Alessandro
Alessandro

Reputation: 2998

Solution

In the documentation you can see that a File resource includes the md5Checksum in its JSON representation.

Since there is no md5Checksum wrapper for the DriveApp Class you have to use Advanced Google Services. This feature lets you use the same functionalities of the Drive API but through the Apps Script environment. In the Apps Script Editor click Resources>Advanced Google services...>Drive API (v2). The IDE will take care of authorization the first time you run a function.

Here is an example of using the Drive API advanced service to retrieve the md5Checksums of your Drive files:

let files = Drive.Files.list();
for (item of files.items) {
    Logger.log(item['md5Checksum']);
}

Note: An MD5 checksum for the content of this file. This field is only populated for files with content stored in Google Drive; it is not populated for Google Docs or shortcut files.

Reference

Drive API

Advanced Google Services

Upvotes: 8

Related Questions