简体   繁体   English

如何使用 Drive API 设置文件的元数据?

[英]How to set the metadata of file using Drive API?

I'm looking for a solution to uploading the files with some specific metadata!我正在寻找一种解决方案来上传具有某些特定元数据的文件! I tried to set canDownload to false, but there is no reflection in the result!我尝试将canDownload设置为false,但结果没有反映! And I want to set the file to Everybody can access.我想将文件设置为每个人都可以访问。 Here is my code:这是我的代码:

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!");
            }
        });

  • You don't want to make users download the created file.您不想让用户下载创建的文件。
  • You want to share with anyone as reader.您想作为读者与任何人分享。
  • You want to achieve this using googleapis with Node.js.您想使用 googleapis 和 Node.js 来实现这一点。
    • Your script is typescript.你的脚本是打字稿。 And you build it and run it with Node.js.然后你构建它并使用 Node.js 运行它。
  • You have already been able to create a file to Google Drive using Drive API.您已经能够使用 Drive API 在 Google Drive 中创建文件。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

About capabilities :关于capabilities

Unfortunately, the property of capabilities.canDownload is not writable .不幸的是, capabilities.canDownload的属性是不可writable By this, as a method, how about using copyRequiresWriterPermission ?这样,作为一种方法,如何使用copyRequiresWriterPermission Using this, the file is set as "Disable options to download, print and copy for commenters and viewers".使用此功能,该文件被设置为“禁用评论者和查看者的下载、打印和复制选项”。

About shared :关于shared

The property of shared is also not writable . shared的属性也是不可writable So when you want to share with anyone as reader, please use the method of Permissions: create in Drive API.所以当你想与任何人作为读者分享时,请使用 Permissions: 在 Drive API 中创建的方法。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From:从:

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!");
           }
       });

To:到:

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);
    });
  }
);
  • In order to not make users download the uploaded file, copyRequiresWriterPermission: true is added to the request body of Files: create.为了不让用户下载上传的文件,在 Files: create 的请求体中添加copyRequiresWriterPermission: true
  • In order to share with anyone, {role: "reader", type: "anyone"} is used to the request body of Permissions: create.为了与任何人共享, {role: "reader", type: "anyone"}用于 Permissions: create 的请求正文。

Note:笔记:

  • In your script, the service account is used.在您的脚本中,使用了服务帐户。 So if you want to see the uploaded file in your Google Drive, please share it with your Google account.因此,如果您想在 Google Drive 中查看上传的文件,请与您的 Google 帐户共享。 By this, you can see it using your browser.这样,您可以使用浏览器查看它。 Please be careful this.请注意这一点。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用API​​对元数据进行迭代的Google Drive文件 - Iterating Google Drive files using API for metadata 如何设置 api 请求到谷歌驱动器 api 使用 axios - how to set api request to google drive api using axios 如何使用 fetch 通过 api 发送文件元数据 - How to send file metadata over api using fetch 如何使用其API在Google驱动器中打开文件? - How do I open a file in google drive using its api? 如何使用Google Drive API使文件可共享 - How to make the file shareable using google drive api 如何使用 JS API 将文件上传到 Google 云端硬盘中的特定位置? - How to upload a file into a specific location in the Google Drive using the JS API? 如何使用具有自己扩展名的驱动器api在Google驱动器中创建空白文件 - How to create blank file in google drive using drive api with our own extension Google Drive API v3未列出文件/文件夹的指定元数据 - Google Drive API v3 doesn't list specified metadata of file/folder 如何使用JavaScript检索文档集的元数据 - How to retrieve metadata of document set using JavaScript 通过Javascript API将文件上传到Google云端硬盘时,如何设置权限? - How to set permissions when uploading a file to Google Drive through the Javascript API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM