使用 Google Drive API v3 将 .docx 或 .doc 文件转换为 google docs 文件

Using Google Drive API v3 to convert .docx or .doc file to google docs file

我需要使用 Google 驱动器 API v3 将 .doc 或 .docx 文件上传到 google 驱动器,然后也使用 Google 驱动器 [=23] 进行转换=] v3 这样文件的内容就可以在我自己的 Web 应用程序(托管在 node.js 中)中读取和编辑。我尝试使用 drive.files.copy 执行此操作,但是,我一直收到错误消息: API 返回错误:错误:不支持将上传的内容转换为请求的输出类型。谁能帮我弄清楚该怎么做?任何帮助将不胜感激。谢谢!

这是我用于转换的代码,但是它不起作用:

                drive.files.list({
                    q: "mimeType = 'application/msword'"
                    pageSize: 100,
                    fields: 'nextPageToken, files(id, name)',
                }, (err, res) => {
                    if (err) return console.log('The API returned an error: ' + err);
                    const files = res.data.files;
                    if (files.length) {
                    console.log('Files:');
                    files.map((file) => {
                        let result = (file.name).substring(0, (file.name).indexOf('.'));
                        console.log(file);
                        console.log(`${file.name} (${file.id})`);
                        drive.files.copy(
                            {
                            fileId: file.id,
                            requestBody: {
                                name: result,
                                mimeType: 'application/vnd.google-apps.document'
                            }
                            },
                            (err, res) => {
                                if (err) return console.log("The API returned an error: " + err);
                                console.log(res.data);  
                            }
                        );
                    });
                    }else {
                    console.log('No files found.');
                    }
                });

这是我用于上传文件的代码:

            const driveResponse = drive.files.create({
                requestBody: {
                    name: filename,
                    mimeType: mimetype
                },
                media: {
                    mimeType: mimetype,
                    body: Buffer.from(data).toString()
                }
            });
            driveResponse.then(data => {
                if(data.status == 200)
                    res.redirect('/notes');
                else
                    console.log("file not uploaded.");
            }).catch(err => { throw new Error(err) })

当我看到你上传application/msword文件的脚本时,我注意到了一个修改点。那么下面的修改呢?在这种情况下,需要转换为流类型。

此外,我确认当我测试buffer.toString()作为media的正文时,上传的文件是一个无效文件。

修改后的脚本:

const { Readable } = require("stream");
const stream = new Readable();
stream.push(Buffer.from(data));
stream.push(null);

const driveResponse = drive.files.create({
    requestBody: {
        name: filename,
    },
    media: {
        body: stream
    }
});
driveResponse.then(data => {
    if(data.status == 200)
        res.redirect('/notes');
    else
        console.log("file not uploaded.");
}).catch(err => { throw new Error(err) })

注:

  • 此修改后的脚本假定您的 Buffer.from(data) 值是 application/msword 的有效值。所以如果Buffer.from(data)的值无效为application/msword,请重新检查数据。