Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

node.js - How to upload file to Group Drive (Shared Drive) with Google Drive API V3?

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' } ] }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...