在Node.js中使用REST API将文件上传到Google Drive [英] Upload file to google drive using REST API in Node.js
本文介绍了在Node.js中使用REST API将文件上传到Google Drive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用node.js中的rest API(插入)将文件上传到谷歌驱动器,我有access_token和refresh_token。我如何在没有任何sdk的情况下上传。
解决方案
我成功地通过修改
I am trying to upload a file to google drive using rest API(insert) in node.js, I have access_token and refresh_token. how can I upload without any sdk.
解决方案
I was successful in testing this NodeJS upload by modifying the NodeJS basic upload sample a bit and mixing it with the already available NodeJS Quickstart
Here's the upload function:
function uploadFile(auth){
var drive = google.drive('v3');
var fileMetadata = {
'name': 'uploadImageTest.jpeg'
};
var media = {
mimeType: 'image/jpeg',
//PATH OF THE FILE FROM YOUR COMPUTER
body: fs.createReadStream('/usr/local/google/home/rrenacia/Downloads/noogui.jpeg')
};
drive.files.create({
auth: auth,
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ', file.id);
}
});
}
You can use the NodeJS Quickstart as reference for authorization. Change listFiles
function name:
authorize(JSON.parse(content), listFiles);
to the name of your upload function, in this case:
authorize(JSON.parse(content), uploadFile);
A sucessful upload on my Google drive:
这篇关于在Node.js中使用REST API将文件上传到Google Drive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文