如何使用google drive node.js恢复上传

时间:2018-03-01 17:10:46

标签: node.js google-api google-drive-api google-api-nodejs-client

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

知道我可以使用 drive.files.create

上传小于5MB的文件,这一点很重要

因此auth没有问题。

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

我写了这个POST请求(也没用PUT):

var userData = [{
    value: false,
    id: 1,
    title: 'Hello World',
    date: '17 February 2018 - 06:27:51 PM',
    status: 'Processing'
  },
  {
    value: false,
    id: 2,
    title: 'Hello People',
    date: '17 February 2018 - 06:27:48 PM',
    status: 'Active'
  },
  {
    value: false,
    id: 3,
    title: 'Hello Canary',
    date: '17 February 2018 - 06:27:44 PM',
    status: 'Expired'
  }
]

// Solution to your problem
let titles = userData.map(function(obj) {
  return obj.title;
});

console.log(titles);

但我得到身体结果:

 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 }
                         `)

                 }); 


 }); 

如果我将使用请求网址: 的 https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

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

所以任何人都有一个工作代码,用于上传具有可恢复上传文件的文件,或者可能再次使用基本上传文件?或者还有另一种上传大文件的方法吗?我愿意接受替代方案!谢谢

2 个答案:

答案 0 :(得分:0)

在其他api客户端(例如Python one)中,通过更改MediaFileUpload constructor with the parameter resumable=True来创建可恢复的上载。 node.js api客户端仅在alpha中,因此可能没有内置支持可恢复上传。您可以尝试提供驱动器a stream,或者只是扩展该示例media参数,例如

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

如果流和上面的resumable不起作用,那么您将无法使用node.js客户端库进行可恢复的上传,并且必须使用REST API直接

答案 1 :(得分:0)

问题本身与node.js的googleapi库有关。使用v27.0.0,基本上传工作再次适用于大文件。相关: https://github.com/google/google-api-nodejs-client/issues/1038

这不是如何使用node.js恢复上传的答案,因此您可以保持此主题开放,直到有人发布可恢复上传的示例代码,因为即使使用v27,我的POST请求仍然无效。也许你从上面看github链接,因为我在那里问了一个示例代码。

然而我的问题只是我无法上传超过5MB的文件。但是这个问题现在已经消失了:))!