I've created a simple application using Google Drive API v3 that takes a file that already exists on my server and uploads it to google drive.
I've already been able to successfully upload a .json
file without much of an issue. However, when I try and upload any other file types, like a .pdf
or .jpeg
, it doesn't seem to work. The program will create a file in Google Drive with the appropriate name and file extension, but I can't seem to open the file.
Google Drive will typically tell me that there was an error previewing or converting the file, and downloading the file doesn't seem to help either. Any help to relieve this issue is much appreciated!
Here is my upload function:
function uploadFile(auth) {
var drive = google.drive('v3');
var fileMetadata = {
'name': name
};
var media = {
mimeType: mime,
body: fs.createReadStream(destination_path)
};
drive.files.create({
auth: auth,
resource: fileMetadata,
media: media,
fields: 'id',
}, function(err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ', file.id);
}
});
}
I'm using the scope: https://www.googleapis.com/auth/drive.file