如何使用Google Drive API V3将文件上传到Group Drive(共享驱动器)?

时间:2019-06-04 23:25:17

标签: node.js google-api google-drive-api google-drive-team-drive

我有一个疑问,如何使用Google Drive API将文件上传到Group Drive(共享驱动器)?

我已经尝试了该文件夹(组驱动器)的父ID,但这似乎不起作用。

public uploadFile(stream, totalSize, mime, fileName, parentId?, callback?) {
    //Init upload
    this.emit('progress', {
        type: 'file',
        name: fileName,
        uploaded: 0,
        size: totalSize
    });
    debug('Uploading file %s with parentId: %s', fileName, parentId);
    //start upload
    var drive = google.drive({ version: 'v3', auth: this.oauth2Client });
    var fileMetadata = {
        name: fileName,
        mimeType: mime,
        'parents': [
            "0AFiiwdVdxetuUk9PVA"
        ],
        'teamDriveId': "0AFiiwdVdxetuUk9PVA"
    }
    if (parentId) {
        fileMetadata['parents'] = [parentId];
    }
    var req = drive.files.create({
        resource: fileMetadata,
        media: {
            mimeType: mime,
            body: stream
        }
    }, (err, resp) => {
        debug('Uploaded %s to Drive Successfully', fileName);
        this.emit("fileUploaded", {
            size: totalSize,
            name: fileName,
            error: err
        });
        if (callback)
            callback(err, resp);
    });
    var interval = setInterval(() => {
        this.emit("progress", {
            type: 'file',
            name: fileName,
            uploaded: req.req.connection.bytesWritten,
            size: totalSize
        });
        if (req.req.connection.bytesWritten >= totalSize) {
            clearInterval(interval);
        }
    }, SPEED_TICK_TIME);
    return req;
}

这是我尝试过的,但出现此错误:

    at IncomingMessage.emit (events.js:208:7)   code: 404,   errors:    [ { domain: 'global',
       reason: 'notFound',
       message: 'File not found: 0AFiiwdVdxetuUk9PVA.',
       locationType: 'parameter',
       location: 'fileId' } ] }

1 个答案:

答案 0 :(得分:0)

我知道了。原来,我只需要添加由teamDriveId插入的supportAllDrives和driveId。

public uploadFile(stream, totalSize, mime, fileName, parentId?, callback?) {
    //Init upload
    this.emit('progress', {
        type: 'file',
        name: fileName,
        uploaded: 0,
        size: totalSize
    });
    debug('Uploading file %s with parentId: %s', fileName, parentId);
    //start upload
    var drive = google.drive({ version: 'v3', auth: this.oauth2Client });
    var fileMetadata = {
        name: fileName,
        driveId: "0AFiiwdVdxetuUk9PVA",
        mimeType: mime
    }
    if (parentId) {
        fileMetadata['parents'] = [parentId];
    } else {
        fileMetadata['parents'] = ["0AFiiwdVdxetuUk9PVA"];
    }
    var req = drive.files.create({
        resource: fileMetadata,
        media: {
            mimeType: mime,
            body: stream
        },
        supportsAllDrives: true,
    }, (err, resp) => {
        debug('Uploaded %s to Drive Successfully', fileName);
        this.emit("fileUploaded", {
            size: totalSize,
            name: fileName,
            error: err
        });
        if (callback)
            callback(err, resp);
    });
    var interval = setInterval(() => {
        this.emit("progress", {
            type: 'file',
            name: fileName,
            uploaded: req.req.connection.bytesWritten,
            size: totalSize
        });
        if (req.req.connection.bytesWritten >= totalSize) {
            clearInterval(interval);
        }
    }, SPEED_TICK_TIME);
    return req;
}