如何使用谷歌驱动器node.js恢复上传 [英] How to resumable upload with google drive node.js

查看:170
本文介绍了如何使用谷歌驱动器node.js恢复上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,因为谷歌驱动器正在改变他们的图书馆,我无法上传大于5MB的文件,并且基本上传 drive.files.create 。文档告诉我,我必须选择可恢复的上传文件。但谷歌驱动器没有提供任何示例代码,也无法在Google上找到任何内容。



也许重要的是要知道,我可以上传小于5MB与 drive.files.create



所以认证没有问题。



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



我写了这个POST请求(也不适用于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)
返回;
}




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


console.log(`
响应:$ {JSON.stringify(r)}
正文:$ {b}
`)

});


});

但我得到的结果是:

 < HTML> 
< HEAD>
< TITLE>请求实体太大< / TITLE>
< / HEAD>
< BODY BGCOLOR =#FFFFFFTEXT =#000000>
< H1>请求实体太大< / H1>
< H2>错误413< / H2>
< / BODY>
< / HTML>

如果我使用请求url来代替:
https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable



我得到一个与身体结果类似的消息:
请求太大。



因此,任何人都有一个工作代码,用于上传带有可恢复上传文件的文件,或者也可以再次使用基本上传文件?或者有另一种上传大文件的方法吗?我打开替代品!谢谢

解决方案

这个问题本身与node.js的googleapi库相关。随着v27.0.0的基本上传工作再次为大文件以及。关联到:
https://github.com/google / google-api-nodejs-client / issues / 1038

这并不是解决如何使用node.js恢复上传的答案,所以你可以保留这个主题打开,直到有人发布可恢复上载的示例代码,因为即使使用v27我的POST请求仍然无法正常工作。也许你会从上面看到github链接,因为我在那里也提供了示例代码。



然而,我的问题只是我无法上传大于5MB的文件。但是这个问题现在没有了:)!

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

解决方案

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 :)!

这篇关于如何使用谷歌驱动器node.js恢复上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆