我一直在使用谷歌驱动API和teamdrives的一些问题.我不能为我的生活,弄清楚如何将文件上传到团队驱动器.
我可以使用此功能将文件上传到我的个人驱动器:
function insertFile(fileData, callback, desc) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType,
'description': desc,
"parents": [
{
"id": "1hBdtlAFrL2zljEcq2QVbqj14v_-SJarc"
}
]
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何为Team Drives调整这个,我能够在团队驱动器中查看文件.我确实有团队驱动器ID,以及我想要插入文件的folderID.
javascript中的一个例子将非常感激,我似乎无法找到一个.
编辑:
我可以通过添加teamdrivesupport布尔值在团队驱动器上创建新文件,我甚至可以创建新文件,但是我不确定如何使用以下命令上传文件数据:
gapi.client.drive.files.create({
'supportsTeamDrives':"true",
'teamDriveId': 'TEAMDRIVEID',
"name" : 'test',
"mimeType" : "application/vnd.google-apps.folder",
'parents': ['FILEID']
}).then(function(response) {
console.log(response)
});
Run Code Online (Sandbox Code Playgroud)
我已经阅读了所有文档,并尝试了无数不同的方法,但没有运气.任何帮助将不胜感激.
小智 4
在尝试了很多不同的方法之后,我无法找到解决这个问题的直接方法。相反,我决定使用我的个人驱动器并将文件移动到我的团队驱动器。
可以将文件从个人云端硬盘移动到团队云端硬盘,但无法移动文件夹。首先,我使用以下代码在团队驱动器上创建了文件夹:
gapi.client.drive.files.create({
'supportsTeamDrives':"true",
'teamDriveId': 'teamID',
"name" : 'test',
"mimeType" : "application/vnd.google-apps.folder",
'parents': ['folderID']
}).then(function(response) {
console.log(response)
}
Run Code Online (Sandbox Code Playgroud)
);
这将在您的团队驱动器上创建一个空文件夹。
然后您可以使用以下命令将文件上传到您的个人驱动器:
function insertFile(fileData, callback, desc, folderID) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType,
'description': desc,
"parents": [
{
"id": folderID
}
]
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
Run Code Online (Sandbox Code Playgroud)
这会将文件上传到您的个人驱动器,然后您可以从响应中获取文件 ID 并更改其父级以将其移动到团队驱动器上:
gapi.client.drive.files.update({
fileId: fileId,
'supportsTeamDrives':"true",
'corpora':"teamDrive",
'teamDriveId': teamdriveID,
addParents: folderID,
removeParents: previousParents,
fields: 'id, parents'
}).then(function(response) {
console.log(response.result.parents)
});
Run Code Online (Sandbox Code Playgroud)
然后,这会将您上传到个人驱动器的文件移动到您在团队驱动器上创建的文件夹中。
我知道这是一个草率的解决方案,但迄今为止我找不到其他解决方案。
我希望有人觉得这很有用。
编辑:解决方案,写为异步函数
/* This function reads the filedata asynchronously for insert*/
async function readFile(file){
let reader = new FileReader();
reader.readAsBinaryString(file);
return await new Promise(resolve => {
reader.onload = e => {
resolve(btoa(e.target.result));
};
});
}
/* This function inserts the file into the root of your personal drive, again this happens asynchronously */
async function insertFile(fileData) {
try {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
let base64Data = null;
await readFile(fileData).then(function(e) {
base64Data = e;
});
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType,
"parents": [
{
"id": 'root'
}
]
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
const fulfilledValue = await gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
let result = await fulfilledValue.result;
return result;
}
catch (rejectedValue) {
console.log("Failed to insert file into folder", rejectedValue);
}
}
/*This function ties everything together and moves the file to your team drive, and removes it from your personal drive, you need to provide the file data, team drive ID, and the ID of the folder on the team drive, again this is asynchronous*/
async function insertTeamDrive(file, teamdriveID, folderID) {
try {
let id = null;
await insertFile(file).then(function(e) {
id = e.id;
});
await gapi.client.drive.files.update({
fileId: id,
'supportsTeamDrives':"true",
'corpora':"teamDrive",
'teamDriveId': teamdriveID,
addParents: folderID,
removeParents: 'root',
fields: 'id, parents'
}).then(function(response) {
console.log(response.result)
});
}
catch (rejectedValue) {
console.log("Failed to insert into team drive", rejectedValue);
}
}
Run Code Online (Sandbox Code Playgroud)
调用insertTeamDrive会将给定文件上传到给定团队驱动器上指定的文件夹。
| 归档时间: |
|
| 查看次数: |
3928 次 |
| 最近记录: |