新答案(此处修复了旧答案问题)
该问题可能是由于 Axios 未正确处理读取流引起的。您应该将读取流通过管道传输到 PassThrough 流并将其提供给 axios.put(),而不是将读取流直接传递给 axios.put()。
import axios from 'axios';
import { google } from 'googleapis';
import fs from 'fs';
import path from 'path';
import { PassThrough } from 'stream';
export async function uploadFile(auth) {
const fileMetadata = {
name: FILENAME,
};
const filePath = path.join(homedir(), 'Pictures', FILENAME);
const fileSize = fs.statSync(filePath).size;
const media = {
mimeType: FILETYPE,
};
const drive = google.drive({ version: 'v3', auth });
try {
const uploadUrlRes = await drive.files.create({
requestBody: fileMetadata,
media: media,
fields: 'id',
uploadType: 'resumable',
});
const uploadUrl = uploadUrlRes.data;
const fileWrite = fs.createReadStream(filePath);
const passThrough = new PassThrough();
fileWrite.pipe(passThrough);
const axiosConfig = {
headers: {
'Content-Type': media.mimeType,
'Content-Length': fileSize,
},
onUploadProgress: (evt) => {
const progress = Math.round((evt.loaded / fileSize) * 100);
console.log(progress);
},
};
const res = await axios.put(uploadUrl, passThrough, axiosConfig);
console.log(res.data);
} catch (error) {
console.error('Error uploading file:', error);
}
}
旧答案。
使用 Axios 处理上传及其进度。首先,通过调用drive.files.create(),并将uploadType 参数设置为“可恢复”,使用Google Drive API 创建可恢复上传会话。这将返回一个可恢复会话 URI,可与 Axios 一起使用来上传文件。
现在,使用适当的标头和 onUploadProgress 事件处理程序配置 Axios。然后,我们使用 axios.put() 方法将文件上传到可恢复会话 URI。
import axios from 'axios';
import { google } from 'googleapis';
import fs from 'fs';
import path from 'path';
export async function uploadFile(auth) {
const fileMetadata = {
name: FILENAME,
};
const fileWrite = fs.createReadStream(path.join(homedir(), 'Pictures', FILENAME));
const fileSize = fs.statSync(path.join(homedir(), 'Pictures', FILENAME)).size;
const media = {
mimeType: FILETYPE,
body: fileWrite,
};
const drive = google.drive({ version: 'v3', auth });
try {
const uploadUrlRes = await drive.files.create({
requestBody: fileMetadata,
media: media,
fields: 'id',
uploadType: 'resumable'
});
const uploadUrl = uploadUrlRes.data;
const axiosConfig = {
headers: {
'Content-Type': media.mimeType,
'Content-Length': fileSize,
},
onUploadProgress: (evt) => {
const progress = Math.round((evt.loaded / fileSize) * 100);
console.log(progress);
},
};
const res = await axios.put(uploadUrl, fileWrite, axiosConfig);
console.log(res.data);
} catch (error) {
console.error('Error uploading file:', error);
}
}