How to resumable upload with google drive node.js

会有一股神秘感。 提交于 2019-12-19 11:26:56

问题


Hey since google drive was changing their library I´am not able to upload anymore files bigger than 5MB with the basic upload drive.files.create. The docs told me that I have to choose resumable uploads instead. But google drive didn´t provide any sample code and aswell I can´t find anything on google.

Maybe it´s important to know that I can upload files smaller than 5MB with the drive.files.create

So there is no problem with the auth.

https://developers.google.com/drive/v3/web/resumable-upload

I wrote this POST request(Also not working with PUT):

 var fs = require('fs')
 var request = require('request')
 var file = 'C:\\test\\sample.container'
 var uploadUrl = 'https://www.googleapis.com/drive/v3/files?uploadType=resumable'


 var stats = fs.statSync(file)
 var fileSizeInBytes = stats["size"]


  fs.readFile(file, function read(e, f) {
             if (e) {
             console.log(e)
             return;
             } 




                 request.post({
                     url: uploadUrl,
                     headers: {
                         'Authorization': 'xxxxxxxxxxxxxxxxxxxxxxx',
                         'Content-Length': fileSizeInBytes,
                         'Content-Type': 'application/octet-stream'
                     },
                     body: f,
                 }, function(e, r, b) {
                     if (e) {
                      console.log(e)
                      return;
                     }

                       console.log(`
                         Response: ${ JSON.stringify(r) }
                         Body: ${ b }
                         `)

                 }); 


 }); 

but I get as body result:

<HTML>
<HEAD>
<TITLE>Request Entity Too Large</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Request Entity Too Large</H1>
<H2>Error 413</H2>
</BODY>
</HTML>

If I would use as request url instead: https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

I get aswell an similiar message as body result: Request is too large.

So anybody has a working code for uploading files with the resumable upload or maybe again with the basic upload? Or is there another way for uploading big files? I´am open for alternatives! Thank you


回答1:


In other api clients (e.g. the Python one), resumable uploads are created by altering the MediaFileUpload constructor with the parameter resumable=True. The node.js api client is only in alpha, so it may not have built-in support for resumable uploads. You can try feeding drive a stream, or simply extending that example media parameter, e.g.

media: {
  mimeType: 'some mimetype',
  body: 'some body',
  resumable: true
}

If stream and the above resumable don't work, then you won't be able to use the node.js client library to do resumable uploads, and will have to use the REST API directly.




回答2:


The problem itself was related to the googleapi library for node.js. With v27.0.0 the basic upload was working again for big files aswell. Related to: https://github.com/google/google-api-nodejs-client/issues/1038

This is not an answer for how to resumable upload with node.js so you may keep this topic open until somebody post a sample code for resumable upload because even with v27 my POST request is still not working. Maybe you watch the github link from above because I asked there aswell for an sample code.

However my problem was only that I was not able to upload bigger files than 5MB. But this problem is now gone for me :)!



来源:https://stackoverflow.com/questions/49055157/how-to-resumable-upload-with-google-drive-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!