I’m working on a project where I need to upload files directly to our Team Drive using Google Drive API V3. I keep running into issues when trying to specify the correct destination folder.
I’ve been experimenting with different approaches but can’t seem to get the file to land in the right Team Drive location. Here’s my current implementation:
function saveFileToTeamDrive(fileStream, fileSize, mimeType, filename, folderId, onComplete) {
// Track upload progress
this.emit('uploadProgress', {
category: 'document',
filename: filename,
bytesUploaded: 0,
totalBytes: fileSize
});
console.log('Starting upload for %s to folder: %s', filename, folderId);
const driveService = google.drive({ version: 'v3', auth: this.authClient });
const documentMetadata = {
name: filename,
mimeType: mimeType,
'parents': [
"0BFxxwdVdxetuUk9PVB"
],
'driveId': "0BFxxwdVdxetuUk9PVB"
};
if (folderId) {
documentMetadata['parents'] = [folderId];
}
const uploadRequest = driveService.files.create({
resource: documentMetadata,
media: {
mimeType: mimeType,
body: fileStream
}
}, (error, response) => {
console.log('File %s uploaded successfully', filename);
this.emit("uploadComplete", {
totalBytes: fileSize,
filename: filename,
error: error
});
if (onComplete)
onComplete(error, response);
});
const progressTimer = setInterval(() => {
this.emit("uploadProgress", {
category: 'document',
filename: filename,
bytesUploaded: uploadRequest.req.connection.bytesWritten,
totalBytes: fileSize
});
if (uploadRequest.req.connection.bytesWritten >= fileSize) {
clearInterval(progressTimer);
}
}, UPDATE_INTERVAL);
return uploadRequest;
}
But I’m getting this error message:
at IncomingMessage.emit (events.js:208:7)
status: 404,
errors: [
{
domain: 'global',
reason: 'notFound',
message: 'File not found: 0BFxxwdVdxetuUk9PVB.',
locationType: 'parameter',
location: 'fileId'
}
]
What am I missing here? How should I properly specify the Team Drive destination?