简体   繁体   English

如何使用 Google Drive 在团队驱动器中创建文件 API

[英]How to create a file in a team drive using Google Drive API

I would like to figure how to create a file within a Team drive using Google's Drive API.我想弄清楚如何使用 Google 的驱动器 API 在团队驱动器中创建文件。

Here is a reference to for files and teamdrives in google drive api's documentation. Here is a reference to for files and teamdrives in google drive api's documentation.

https://developers.google.com/drive/api/v3/reference/files https://developers.google.com/drive/api/v3/reference/files

https://developers.google.com/drive/api/v3/reference/files/create https://developers.google.com/drive/api/v3/reference/files/create

https://developers.google.com/drive/api/v3/enable-shareddrives https://developers.google.com/drive/api/v3/enable-shareddrives

const resource = {
    name: fileName,
    supportsAllDrives: true,
    driveId: TEAMDRIVE_ID,
    mimeType: 'application/vnd.google-apps.spreadsheet'
};

drive.files.create(
   {
       resource,
       fields: 'id, name, webViewLink'
    },
    (err, file) => {
        if (err) {
            return({ msg: 'Failed Creating the file', err });
        } else {

            return file.data;
        }
     }
);

The code is able to create the file, but instead of it appearing in the team drive.该代码能够创建文件,但它不会出现在团队驱动器中。 It appears inside my personal drive.它出现在我的个人驱动器中。 I am able to read files within my team drive.我能够读取团队驱动器中的文件。 Creating files has been an issue for me though...创建文件对我来说一直是个问题......

I ended up finding an answer to my question, here is what worked for me.我最终找到了问题的答案,这对我有用。

const resource = {
    name: fileName,
    driveId: TEAMDRIVE_ID,
    mimeType: 'application/vnd.google-apps.spreadsheet',
    parents: [parent_id]
};

drive.files.create(
   {
       resource,
       fields: 'id, name, webViewLink'
       supportsAllDrives: true,
    },
    (err, file) => {
        if (err) {
            return({ msg: 'Failed Creating the file', err });
        } else {

            return file.data;
        }
     }
);

I had to move the supportsAllDrives: true out of the resource object and move it as an option in the drive.files.create paramter.我不得不将supportsAllDrives: trueresource object 中移出,并将其作为drive.files.create参数中的选项移动。

Also needed to add parents to the resource object. The parent_id can be a Team Drive Id or a folder Id within the Team Drive.还需要将parents项添加到resource object。parent_id 可以是团队驱动器 ID 或团队驱动器中的文件夹 ID。

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

相关问题 如何使用带有节点 js 的谷歌驱动器 api 列出谷歌团队驱动器中的所有文件 - How to List all the files from google team drive using google drive api with node js 我未登录Google Drive时使用Google Drive API打开Goog​​le Drive文件 - Opening a google drive file using the google drive api while I'm NOT signed in to google drive 使用 Google Drive API - Using Google Drive API Google Drive API - 如何提取 drive.file.list 的结果? - Google Drive API - How to Extract the Results of drive.file.list? 如何使用 API 从谷歌驱动器下载文件作为 Blob - How to download a file as Blob from google drive using API 如何使用 JS API 将文件上传到 Google 云端硬盘中的特定位置? - How to upload a file into a specific location in the Google Drive using the JS API? NodeJS Google Drive API 如何更新文件 - NodeJS Google Drive API how to update file 如何上传文件夹 GOOGLE DRIVE API 中的文件 - How to upload a file in a folder GOOGLE DRIVE API 使用 google drive-drive-api-v3 和 node js 将文件复制到共享的 google 驱动器时,是否可以专门针对共享驱动器 - Is there a way to specifically target a shared drive when copying a file to a shared google drive using google drive-drive-api-v3 and node js 如何使用Google Drive API V3 上传文件到Group Drive (Shared Drive)? - How to upload file to Group Drive (Shared Drive) with Google Drive API V3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM