Google Drive API 使用浏览器版本创建空的“无标题”文件

2023-12-28

我正在尝试使用 Google Drive API 的“浏览器”版本,它似乎主要遵循 Nodejs 语法。但除了浏览器的第一个 hello world 示例之外,似乎没有太多示例。

现在我正在尝试创建一个文件夹,然后在该文件夹内创建一个简单的 JSON 配置文件作为概念证明。然而,当我运行此代码时,我只在我的 Google 云端硬盘中得到一个标有“无标题”的空文件。

这是文件创建的片段,返回成功。

        this.MEM.config = {
            categories : {},
            createdOn : Date.now()
        }

        let fileMetadata = {
            name : 'config.json',
            parents : [this.MEM.parent]
        }

        let media = {
            mimeType : 'application/json',
            body : JSON.stringify(this.MEM.config)
        }

        query = {
            resource: fileMetadata,
            media: media,
            fields: 'id'
        }

        console.log(query);

        try {
            res = await gapi.client.drive.files.create(query);
        } catch(err) {
            throw err;
        }

        console.log(res);

结果似乎不会产生任何错误:

{
  "result": {
    "id": "1OI0ttFr11UH1__XAlSnyUil5hpp6mScB"
  },
  "body": "{\n \"id\": \"1OI0ttFr11UH1__XAlSnyUil5hpp6mScB\"\n}\n",
  "headers": {
    "cache-control": "no-cache, no-store, max-age=0, must-revalidate",
    "content-encoding": "gzip",
    "content-length": "67",
    "content-type": "application/json; charset=UTF-8",
    "date": "Thu, 29 Apr 2021 20:26:13 GMT",
    "expires": "Mon, 01 Jan 1990 00:00:00 GMT",
    "pragma": "no-cache",
    "server": "GSE",
    "vary": "Origin, X-Origin"
  },
  "status": 200,
  "statusText": "OK"
} 

除了在我用字符串化对象指定的文件夹中创建“config.json”文件之外,我只是在 Google Drive 的根目录中获得一个 Untitled 文件。


修改要点:

  • 目前阶段看来,当gapi.client.drive.files.create使用时,不能包含文件内容。例如,当您使用以下内容时query通过删除文件内容。可以使用以下值创建该文件fileMetadata。但是,不包括文件内容。

      query = {
          resource: fileMetadata,
          fields: 'id'
      }
    
  • 当您想通过包含文件内容来创建新文件时,直接向端点请求上传文件怎么样?

当你的脚本被修改后,它会变成如下所示。

修改后的脚本:

this.MEM.config = {
  categories : {},
  createdOn : Date.now()
}
let fileMetadata = {
  name : 'config.json',
  parents : [this.MEM.patent]
}
const form = new FormData();
form.append('metadata', new Blob([JSON.stringify(fileMetadata)], {type: 'application/json'}));
form.append('file', new Blob([JSON.stringify(this.MEM.config)], {type: 'application/json'}));
fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id', {
  method: 'POST',
  headers: new Headers({'Authorization': 'Bearer ' + gapi.auth.getToken().access_token}),
  body: form
})
.then((res) => res.json())
.then((res) => console.log(res));
  • 在这种情况下,可以使用以下命令检索访问令牌gapi.auth.getToken().access_token。从您的问题中,我了解到您的访问令牌可用于上传文件。

Note:

  • 在此修改中,文件和文件元数据上传为multipart/form-data。在本例中,最大内容大小为 5 MB。请小心这一点。
  • 当您要上传大文件时,请使用断点续传。在这种情况下,Javascript 库ResumableUploadForGoogleDrive_js https://github.com/tanaikech/ResumableUploadForGoogleDrive_js可能有用。

参考:

  • 上传文件数据 https://developers.google.com/drive/api/v3/manage-uploads
  • ResumableUploadForGoogleDrive_js https://github.com/tanaikech/ResumableUploadForGoogleDrive_js
  • 使用获取 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Drive API 使用浏览器版本创建空的“无标题”文件 的相关文章

随机推荐