Javascript http get请求后将文件上载到google驱动器
我在单独的文件中有两个函数来分割工作流Javascript http get请求后将文件上载到google驱动器,javascript,node.js,api,google-drive-api,Javascript,Node.js,Api,Google Drive Api,我在单独的文件中有两个函数来分割工作流 const download = function(url){ const file = fs.createWriteStream("./test.png"); const request = https.get(url, function(response) { response.pipe(file); }); } 我的fileHelper.js中的这个函数应该获取一个包含图像的URL,然后将其本地保存到test
const download = function(url){
const file = fs.createWriteStream("./test.png");
const request = https.get(url, function(response) {
response.pipe(file);
});
}
我的fileHelper.js
中的这个函数应该获取一个包含图像的URL,然后将其本地保存到test.png
function uploadFile(filePath) {
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), function (auth) {
const drive = google.drive({version: 'v3', auth});
const fileMetadata = {
'name': 'testphoto.png'
};
const media = {
mimeType: 'image/png',
body: fs.createReadStream(filePath)
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, (err, file) => {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ', file.id);
}
});
});
});
}
我的googleDriveHelper.js
中的这个函数应该获取调用的文件路径,然后将该流上传到我的google驱动器中。这两个函数各自工作,但https.get似乎是异步工作的,如果我尝试在下载后调用googleDriveHelper.uploadFile(filePath)
函数,它没有时间上传完整的文件,因此会将一个空白文件上传到我的驱动器
- 我想找到一种方法,当调用
时,它会自动上传到我的驱动器中fileHelper.download(url)
- 我也不知道是否有办法直接从
函数到下载
函数创建readStream,这样我就不必在本地保存文件来上传它李>上传
- 我相信你的目标如下
- 您希望将从URL检索的文件上载到Google Drive
- 从URL下载文件时,您希望在不创建文件的情况下将其上载到Google Drive
- 您希望通过Node.js使用googleapis实现这一点
- 您已经能够使用驱动器API上载文件
- 在
功能中,检索到的缓冲区转换为流类型,并返回流数据download
- 在
功能中,检索到的流数据用于上传uploadFile
- 从驱动器API的响应值检索文件ID时,请使用
而不是file.data.ID
file.ID
download()
参考资料:uploadFile()
- 您希望将从URL检索的文件上载到Google Drive
- 从URL下载文件时,您希望在不创建文件的情况下将其上载到Google Drive
- 您希望通过Node.js使用googleapis实现这一点
- 您已经能够使用驱动器API上载文件
- 在
功能中,检索到的缓冲区转换为流类型,并返回流数据download
- 在
功能中,检索到的流数据用于上传uploadFile
- 从驱动器API的响应值检索文件ID时,请使用
而不是file.data.ID
file.ID
- 我相信你的目标如下
download()
参考资料:uploadFile()
const download = function (url) { return new Promise(function (resolve, reject) { request( { method: "GET", url: url, encoding: null, }, (err, res, body) => { if (err && res.statusCode != 200) { reject(err); return; } const stream = require("stream"); const bs = new stream.PassThrough(); bs.end(body); resolve(bs); } ); }); };
function uploadFile(data) { // <--- Modified fs.readFile("drive_credentials.json", (err, content) => { if (err) return console.log("Error loading client secret file:", err); authorize(JSON.parse(content), function (auth) { const drive = google.drive({ version: "v3", auth }); const fileMetadata = { name: "testphoto.png", }; const media = { mimeType: "image/png", body: data, // <--- Modified }; drive.files.create( { resource: fileMetadata, media: media, fields: "id", }, (err, file) => { if (err) { console.error(err); } else { console.log("File Id: ", file.data.id); // <--- Modified } } ); }); }); }
async function run() { const url = "###"; const data = await fileHelper.download(url); googleDriveHelper.uploadFile(data); }