0

I have put together a script that creates a file in a folder on Google Drive, when I make amendments to that file and push it back up it will create a new version (which is obviously correct). What I would like to do is check that the file exists first (by name if I can) and then update that file if exists or create new if it does not.

# index.js
const google = require('googleapis');
const fs = require('fs');
const config = require('./creds.json');
const drive = google.drive('v3');
const targetFolderId = "123456789"


const jwtClient = new google.auth.JWT(
  config.client_email,
  null,
config.private_key,
  ['https://www.googleapis.com/auth/drive'],
  null
);

jwtClient.authorize((authErr) => {
  if (authErr) {
    console.log(authErr);
    return;
}


const fileMetadata = {
  name: './file.xlsx,
  parents: [targetFolderId]
};

const media = {
  mimeType: 'application/vnd.ms-excel',
  body: fs.createReadStream('./file.xlsx' )
};

drive.files.create({
  auth: jwtClient,
  resource: fileMetadata,
  media,
  fields: 'id'
}, (err, file) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log('Uploaded File Id: ', file.data.id);
 });
}); 

Update

As D Levine mentions I can use drive.files.list but I am having problems finding a file. I set up a service account so not sure if that makes a difference. My code is below but all I get at the moment is undefined returned. I would also like to search within specific folders but can't seem to find this option anywhere.

  // Check File exists
  drive.files.list({
    auth: jwtClient,
    spaces: 'drive',
    q: "name='liverpool_away.xlsx'",
    pageSize: 10,
    fields: "nextPageToken, files(id, name)"
 }, function(err, response) {
      if (err) {
      console.log('The API returned an error: ' + err);
      return;
 }
  var files = response.files;
  console.log("Files: " + files);
  if (files.length == 0) {
    console.log('No files found.');
 } else {
  console.log('Files:');
  for (var i = 0; i < files.length; i++) {
    var file = files[i];
    console.log('%s (%s)', file.name, file.id);
  }
 }
});
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

0

You can find if a file exists by searching by name with driveService.files().list(). If you receive 1 result, then that is the file that you were looking for and you can get the file ID. If you receive 0 results, then the file does not exist.

A page describing how to search is here (with example code at the bottom): https://developers.google.com/drive/v3/web/search-parameters

The REST endpoint is documented here: https://developers.google.com/drive/v3/reference/files/list

D. Levine
  • 319
  • 3
  • 5
  • Thank you, I have tried putting something together (updated question), don't suppose you can see anything immediately obvious that could be wrong ? thanks – Richlewis Feb 11 '18 at 17:30
  • Did you npm install the module async and wrap your entire call inside of that? If you look at https://developers.google.com/drive/v3/web/search-parameters and go to the Node.js sample code at the bottom, you will find that that is required. – D. Levine Feb 11 '18 at 18:09
  • Thanks, I've looked at the example, get `Cannot read property 'forEach' of undefined` – Richlewis Feb 11 '18 at 18:54
  • does it make a difference that i am using a service account ? – Richlewis Feb 11 '18 at 19:39