James Camilleri
James Camilleri

Reputation: 11

How can I check if a file has finished uploading before moving it with the Google Drive API v3?

I'm writing a small archiving script (in node.js) to move files on my Google Drive to a predetermined folder if they contain .archive.7z in the filename. The script is run periodically as a cron job, and the file movement has not caused any issues, but files still in the process of being uploaded by my desktop client are moved before they're finished. This terminates the upload and results in corrupted files in the destination folder.

Files still being uploaded from my desktop to Google Drive are returned by the following function anyway:

async function getArchivedFiles (drive) {
  const res = await drive.files.list({
    q: "name contains '.archive.7z'",
    fields: 'files(id, name, parents)',
  })

  return res.data.files
}

Once the files are moved and renamed with the following code, the upload terminates from my client (Insync) and the destination files are ruined.

drive.files.update({
  fileId: file.id,
  addParents: folderId,
  removeParents: previousParents,
  fields: 'id, parents',
  requestBody: {
    name: renameFile(file.name)
  }
})

Is there any way to check if a file is still being uploaded before moving it?

Upvotes: 0

Views: 1309

Answers (1)

James Camilleri
James Camilleri

Reputation: 11

It turns out that a tiny placeholder-type file is being created on uploads. I'm not sure if this is a Google Drive API behaviour or something unique to the Insync desktop client. This file seems to upload separately and thus can be freely renamed once it's complete.

I worked around this problem by including the file's md5 hash in the filename, and updating my script to only move files when the hash in their filename matches the md5Checksum retrieved from the Google Drive API.

Upvotes: 1

Related Questions