【问题标题】:Programatically uploading a file to Google Drive using javascript (no html form)使用 javascript(无 html 表单)以编程方式将文件上传到 Google Drive
【发布时间】:2017-03-17 23:58:16
【问题描述】:

编辑:我是个假人。文档在这里清楚地说明了这一天:https://developers.google.com/drive/v3/web/manage-uploads

上下文的项目概述: 我正在使用 node.js 编写一个部署在 Heroku 上的 twitter 机器人。我希望这个机器人能够从 Google Drive 下载 .txt 文件(或 .ini 或其他文件),使用该文件撰写推文,编辑文本文件的正文以反映生成的推文,然后覆盖 Google Drive 中的原始文件。它将始终下载、编辑和覆盖同一个文件,因此不需要文件选择对话框。

具体问题:我无法弄清楚如何在不使用 html 页面的情况下实际上传文本文件的正文。这一切都应该发生在服务器端,没有用户交互。我发现的大多数示例似乎都使用 html 页面来收集和发送文件信息,但我使用的是纯 javascript,所以这不适用于我的项目。我已经在 Google Drive API 文档中搜索了示例,但到目前为止还没有运气。

目前的代码:

中途的评论区是我遇到问题的地方。

authClientUp.authorize(function(err, tokens) {
    if (err) {
        console.log(err);
        return;                                                     
    } else {
        console.log('It authorized successfully!');
        drive.files.insert({           // ---- For the purposes of getting
            auth: authClientUp,        // ---- started, I'm just creating
            "title": "file1.txt",      // ---- a new file instead of overwriting
            "mimeType": "text/plain",  // ---- an existing one
            "description": "Just a test file"
        }, function(err,result){
            if (err) {
                console.log(err);
            } else {
                console.log(result);
                drive.files.put({      //----- files.put() is not correct, but
                                       //----- I'm unsure what the correct function is
                    /*
                    I know I need the following info here but I'm 
                    not sure how to pass it in correctly:
                    - the id of the file to overwrite (taken from the JSON file 'result'
                    - the body of the file to upload
                    - authentication? I'm not sure if I need that agian
                    */

                }, function(err, result){
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(result);
                    }
                });
            }
        });
    }
});

我找到的所有示例都将这一步格式化为:

PUT /upload/drive/v2/files/{id}?uploadType=media HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: mime/type

<file content here>

但在 html 文件的上下文之外,我认为这不起作用?我需要帮助才能为我的项目正确格式化。

注意事项:

  • 将此文件写入 Google 云端硬盘的目的是,我可以提交对 Heroku 存储库的更改,而不会覆盖和丢失 .txt 文件的内容
  • 我不是经验丰富的程序员,而且我是 javascript 新手。

我在 Stack Overflow 上搜索了许多其他问题,但它们似乎都取决于以下一项或多项:

  • html 表单
  • python,不是 javascript
  • Google Drive Android SDK,我没有使用(虽然如果涉及到它,我想我可以)

【问题讨论】:

  • 假设您的用例是“下载、编辑和覆盖”,您可能会发现电子表格 API 更合适。它允许您只上传更改的单元格,而文件的其余部分保持不变。
  • 感谢您的建议!我会调查一下,因为这听起来完全符合我的目的。

标签: javascript node.js google-drive-api


【解决方案1】:

查看此 Google Drive REST API 文档:

https://developers.google.com/drive/v3/web/manage-uploads

那里的例子,字面意思:

var fileMetadata = {
  'name': 'photo.jpg'
};
var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
   resource: fileMetadata,
   media: media,
   fields: 'id'
}, function(err, file) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    console.log('File Id: ', file.id);
  }
});

【讨论】:

  • 天啊,现在我感觉自己像个笨蛋。谢谢!我想我在项目的早些时候确实遇到过这个问题,但是我还没有确定身份验证,所以它不起作用,所以我把它解雇了。很抱歉浪费大家的时间!
  • 我们所有人都碰巧有时看不到我们面前的解决方案,所以不用担心
  • 只需要放入auth对象 drive.files.create({ auth: auth, resource: fileMetadata, media: media, fields: 'id' }, function (err, file) { if ( err) { // 处理错误 console.log("上传到 gdrive 时出错" + err); } else { console.log('gdrive File created with Id: ', file.id); } });跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多