首页 > 解决方案 > 如何使用 Drive API 设置文件的元数据?

问题描述

我正在寻找一种解决方案来上传带有一些特定元数据的文件!我尝试将canDownload设置为false,但结果没有反映!我想将文件设置为每个人都可以访问。这是我的代码:

const jwToken = new google.auth.JWT(
            token.client_email,
            null,
            token.private_key,
            ["https://www.googleapis.com/auth/drive"],
            null
        );

        jwToken.authorize((err : any) => {
            if (err) {
                console.log(err);
            } else {
                console.log("Authentication Success!");
            }
        });

 const metaData = {
            'name': filename,
            "shared": true,
            "capabilities" : {
                    "canDownload": false
            },
            parents: [folderId]
        };

        const media = {
            mimeType: 'Application/Pdf',
            body: fileSystem.createReadStream(pathSystem.join(path))
        };

        drive.files.create({
            auth     : jwToken,
            resource : metaData,
            media    : media,
            fields   : 'id'
        }, (err : any, file : any) => {
            if (err) {
                console.log("coming!!");

                throw err;
            } else {
                console.log("File Upload Successfully!");
            }
        });

标签: javascriptnode.jstypescriptgoogle-drive-apijwt

解决方案


  • 您不想让用户下载创建的文件。
  • 您想与任何读者分享。
  • 您想使用 googleapis 和 Node.js 来实现这一点。
    • 您的脚本是打字稿。然后你构建它并使用 Node.js 运行它。
  • 您已经能够使用 Drive API 将文件创建到 Google Drive。

如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。

修改点:

关于capabilities

不幸的是, 的属性capabilities.canDownload不是writable。这样,作为一种方法,如何使用copyRequiresWriterPermission?使用它,该文件被设置为“禁用选项以供评论者和查看者下载、打印和复制”。

关于shared

的属性shared也不是writable。因此,当您想与任何读者分享时,请使用 Permissions: create in Drive API 的方法。

修改后的脚本:

当您的脚本被修改时,请进行如下修改。

从:

const metaData = {
           'name': filename,
           "shared": true,
           "capabilities" : {
                   "canDownload": false
           },
           parents: [folderId]
       };

       const media = {
           mimeType: 'Application/Pdf',
           body: fileSystem.createReadStream(pathSystem.join(path))
       };

       drive.files.create({
           auth     : jwToken,
           resource : metaData,
           media    : media,
           fields   : 'id'
       }, (err : any, file : any) => {
           if (err) {
               console.log("coming!!");

               throw err;
           } else {
               console.log("File Upload Successfully!");
           }
       });

至:

const metaData = {
  name: filename,
  parents: [folderId],
  copyRequiresWriterPermission: true
};

const media = {
  mimeType: "application/pdf",
  body: fileSystem.createReadStream(pathSystem.join(path))
};
drive.files.create(
  {
    auth: jwToken,
    resource: metaData,
    media: media,
    fields: "id"
  },
  (err: any, file: any) => {
    if (err) {
      console.log(err);
      return;
    }
    const fileId = file.data.id;
    console.log(fileId);
    const body = {
      auth: jwToken,
      fileId: fileId,
      requestBody: {
        role: "reader",
        type: "anyone"
      }
    };
    drive.permissions.create(body, (err: any, res: any) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log(res.data);
    });
  }
);
  • 为了不让用户下载上传的文件,copyRequiresWriterPermission: true在 Files 的请求体中添加了:create。
  • 为了与任何人共享,{role: "reader", type: "anyone"}用于请求正文的 Permissions:create。

笔记:

  • 在您的脚本中,使用了服务帐户。因此,如果您想在 Google Drive 中查看上传的文件,请与您的 Google 帐户共享。这样,您可以使用浏览器查看它。请注意这一点。

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。


推荐阅读