[英]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: true
从resource
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.