I'm trying to directly upload a file from a remote URL to Google Drive, but I don't want to download the file to my server and then upload it to drive. I also am concerned about keeping the entire file in memory and do the uploading as it will not work for large files.
我正在尝试从远程URL直接将文件上载到Google Drive,但我不想将文件下载到我的服务器,然后将其上载到Drive。我还担心将整个文件保存在内存中,并进行上传,因为它不适用于大文件。
So what I wanted to do is something like directly streaming the file content to the Google Drive, as Drive API accepts a readstream. For that, I have used fetch as follows.
因此,我想要做的是直接将文件内容流到Google Drive,因为Drive API接受Readstream。为此,我使用了FETCH,如下所示。
export async function uploadFileToGoogleDrive(mimeType: string, fileName: string, fileUrl: string) {
const oauth2Client = getGoogleOAuthClient();
oauth2Client.setCredentials({
refresh_token: getGDriveRefreshTokenByUser()
})
const drive = google.drive({
version: 'v3',
auth: oauth2Client
});
(fileUrl.substring(0, 5) === "https" ? https : http).get(fileUrl, async res => {
console.log("test")
const response = await drive.files.create(
{
requestBody: {
name: fileName,
mimeType,
},
media: {
mimeType,
body: res,
},
},
);
console.log(response.data);
})
}
How can I verify that the node.js server is actually streaming the file content to the Drive rather than waiting until the full file is downloaded and then uploaded to GDrive at once? Thanks in Advance for all help!
如何验证node.js服务器是否真的将文件内容传输到驱动器,而不是等待完整文件下载后立即上载到GDrive?提前感谢大家的帮助!
更多回答
Although I'm not sure whether I could correctly understand your expected result, I proposed an answer. Please confirm it. If I misunderstood your question, I apologize.
虽然我不确定我是否能正确理解您的预期结果,但我提出了一个答案。请确认一下。如果我误解了你的问题,我道歉。
优秀答案推荐
In your showing script, how about the following modification?
在您的显示脚本中,如何进行以下修改?
Modified script:
export async function uploadFileToGoogleDrive(mimeType: string, fileName: string, fileUrl: string) {
const oauth2Client = getGoogleOAuthClient();
oauth2Client.setCredentials({
refresh_token: getGDriveRefreshTokenByUser()
})
const drive = google.drive({
version: 'v3',
auth: oauth2Client
});
// I modified the below script.
(fileUrl.substring(0, 5) === "https" ? https : http).get(
fileUrl,
async (res) => {
res.on("data", async (data) => {
const response = await drive.files.create({
requestBody: {
name: fileName,
mimeType,
},
media: {
mimeType,
body: data,
},
});
console.log(response.data);
});
}
);
}
- When this script is run, the data is downloaded and the downloaded data is uploaded to Google Drive.
Note:
- In this modification, it supposes that your client of
drive
can be used for uploading the file to Google Drive. Please be careful about this.
更多回答
Thanks for the answer, but this too didn't work in my case. I tried using the url "212.183.159.230/512MB.zip" which has a 512MB file. no console.log inside the response function is printed until the file is fully downloaded it seems.
谢谢你的回答,但这对我来说也不起作用。我尝试使用url“212.183.159.230/512MB.zip”,它有一个512MB的文件。在文件完全下载之前,似乎不会打印响应函数中的console.log。
@Pavindu Thank you for replying. About your URL of http://212.183.159.230/512MB.zip
, unfortunately, I cannot download the file. So, I cannot test it. I apologize for this. Can you provide the sample URL for testing to download?
@Pavindu感谢您的回复。关于你的http://212.183.159.230/512MB.zip,网址,很遗憾,我无法下载该文件。所以,我不能测试它。我为此道歉。你能提供测试下载的示例URL吗?
我是一名优秀的程序员,十分优秀!