【问题标题】:Google Drive API creates empty 'Untitled' file with browser versionGoogle Drive API 使用浏览器版本创建空的“无标题”文件
【发布时间】:2021-04-29 23:57:33
【问题描述】:

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

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

这是创建文件的sn-p,返回成功。

        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 文件。

【问题讨论】:

    标签: javascript browser google-drive-api


    【解决方案1】:

    修改点:

    • 现阶段,好像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 检索访问令牌。根据您的问题,我了解到您的访问令牌可用于上传文件。

    注意:

    • 在此修改中,文件和文件元数据使用multipart/form-data 上传。在这种情况下,最大内容大小为 5 MB。请注意这一点。
    • 当您要上传大文件时,请使用可续传。在这种情况下,ResumableUploadForGoogleDrive_js 的 Javascript 库可能会有用。

    参考资料:

    【讨论】:

    • 谢谢!这行得通。在使用字符串化内容指定的文件夹中有一个“config.json”文件。看起来这个答案是这个答案的一个更优雅的解决方案:stackoverflow.com/questions/34905363/…。在这两种情况下,它基本上看起来像 gapi.drive.client.file.create 所以你必须直接向 REST 接口发送请求。
    • @Benjamin Collins 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多