使用javascript(无html表单)以编程方式将文件上传到Google云端硬盘

时间:2017-03-17 23:58:16

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

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

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

具体问题:我无法在不使用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,我没有使用(虽然如果涉及到它,我想我可以)

1 个答案:

答案 0 :(得分:3)

检查此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);
  }
});