如何使用 Google Drive API V3 将文件上传到组驱动器(共享驱动器)? [英] How to upload file to Group Drive (Shared Drive) with Google Drive API V3?

查看:16
本文介绍了如何使用 Google Drive API V3 将文件上传到组驱动器(共享驱动器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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' } ] }

推荐答案

我想通了.原来我只需要添加 supportAllDrives 和 driveId insted of teamDriveId.

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

这篇关于如何使用 Google Drive API V3 将文件上传到组驱动器(共享驱动器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆