How to upload file to Group Drive (Shared Drive) with Google Drive API V3?

我与影子孤独终老i 提交于 2020-05-15 03:39:32

问题


I have a question how do I upload a file to Group Drive (Shared Drive) with Google Drive API?

I have tried parent id for that folder (Group Drive) but that does not seem to work.

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;
}

This is what I have tried and I get this error:

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

回答1:


I have figured it out. Turns out I just needed to add supportsAllDrives and driveId insted of teamDriveId.

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;
}


来源:https://stackoverflow.com/questions/56452507/how-to-upload-file-to-group-drive-shared-drive-with-google-drive-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!