[英]Firebase cloud functions: how to retrieve a file from an FTP account and download it in the browser
I have an app which has Firebase as a back-end. 我有一个将Firebase作为后端的应用程序。 From within this app I connect to a FTP server.
从此应用程序中,我连接到FTP服务器。 The connection to the FTP server is done using Firebase Cloud functions and the jsftp module ( https://github.com/sergi/jsftp ).
使用Firebase Cloud函数和jsftp模块( https://github.com/sergi/jsftp )完成与FTP服务器的连接。 I have done tests and it works well to browse the files, create files or folder and upload new files.
我已经完成了测试,可以很好地浏览文件,创建文件或文件夹以及上传新文件。 I got stuck with how to retrieve files from the server.
我陷入了如何从服务器检索文件的困境。 In the jsftp module there is the get() method that is doing this and it is working like that:
在jsftp模块中,有get()方法正在执行此操作,并且它的工作方式如下:
var str = ""; // Will store the contents of the file
ftp.get("remote/path/file.txt", (err, socket) => {
if (err) {
return;
}
socket.on("data", d => {
str += d.toString();
});
socket.on("close", err => {
if (hadErr) console.error("There was an error retrieving the file.");
});
socket.resume();
});
The documentation says like this: Gives back a paused socket with the file contents ready to be streamed, or calls the callback with an error if not successful. 该文档是这样说的: 退回一个暂停的套接字,其中的文件内容准备好进行流传输,或者如果不成功,则返回一个错误回调。
As I have limited experience with Nodejs and socket my question is: How can I retrieve the file using this "socket" and make it available for download in the browser. 由于我对Node.js和套接字的经验有限,我的问题是:如何使用此“套接字”检索文件,并使其可在浏览器中下载。 So when I call the Firebase Cloud function it should return the file.
因此,当我调用Firebase Cloud函数时,它应该返回文件。 Is that possible?
那可能吗?
Thanks in advance! 提前致谢!
Here's a complete index.js with a function that downloads a readme from an Apache mirror using jsftp and sends it back to the client. 这是一个完整的index.js,其功能是使用jsftp从Apache镜像下载自述文件并将其发送回客户端。 Note that it's making an outgoing socket connection, which means you need to run it in a project on the paid Blaze plan.
请注意,它正在建立输出套接字连接,这意味着您需要在付费的Blaze计划的项目中运行它。
const functions = require('firebase-functions')
const JSFtp = require('jsftp')
exports.getFtp = functions.https.onRequest((req, res) => {
const ftp = new JSFtp({
host: 'mirrors.ocf.berkeley.edu'
});
ftp.get('/apache/commons/beanutils/RELEASE-NOTES.txt', (err, socket) => {
if (err) {
res.send(500).send(err)
}
socket.on('data', data => {
res.write(data.toString())
})
socket.on('close', err => {
if (err) {
console.error("There was an error retrieving the file", err)
}
else {
res.end()
}
})
socket.resume()
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.