0

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

Rick
  • 4,030
  • 9
  • 24
  • 35
  • What is the value of `mime`? – pinoyyid Jul 18 '18 at 18:59
  • when I test the code using a jpeg image file, it's set as 'image/jpeg' and when I test it using a pdf it's set as 'application/pdf' – Parker Simpson Jul 18 '18 at 19:09
  • That´s correct. Next step is to make sure the created file has the correct mime type. You can either set `fields:*` or check with the online tool at https://developers.google.com/drive/api/v3/reference/files/get . Then also download the binary file and check that it is the same (eg. md5sum) as the original. – pinoyyid Jul 18 '18 at 19:21
  • Alright, so I used the online tool to check the mime types, and it seems to be okay. The mime types do indeed correspond to the file type. I used md5sums to compare the uploaded file to the original, and it is not the same. – Parker Simpson Jul 18 '18 at 19:51
  • 1
    then I'd guess that the problem is in `body: fs.createReadStream(destination_path)` It's more complex, but way better to use the resumable upload approach, so I suggest Google for some examples of that – pinoyyid Jul 18 '18 at 21:08
  • When the files of pdf and jpeg you uploaded are downloaded, what can you see in the file? If the error message is included, can you provide it? And also can I ask you about the file size you want to upload? I think that they will help users think of your solution. – Tanaike Jul 19 '18 at 02:18
  • When I upload the files to the server, I can view the files just fine. The pdf is roughly 340 KB and the jpeg is roughly 290 KB. Once uploaded to Google Drive, the file can't be viewed. The files seem to be incomplete with some having only 5KB of data and some have 0 KB of data. I will try using a resumable upload approach and see how that works. – Parker Simpson Jul 19 '18 at 15:33
  • I thought that the file with the size of 5 kb may be a text file. Can I ask you about the value? Because the text file might include the error message. – Tanaike Jul 19 '18 at 22:41
  • When opened up using a text editor, it seems to be a whole bunch of random data(as is the case with the pdf files). Some pdf's have some data to them, no images do. – Parker Simpson Jul 20 '18 at 15:07
  • It seems like using a resumable upload approach did the trick. Thanks for your help! – Parker Simpson Jul 20 '18 at 17:02
  • @ParkerSimpson consider adding your solution as a [good answer](https://stackoverflow.com/help/how-to-answer) – tehhowch Jul 20 '18 at 23:06

0 Answers0