简体   繁体   English

Firebase云功能:如何从FTP帐户检索文件并将其下载到浏览器中

[英]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.

相关问题 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 从使用 Cloud Functions 上传的文件中获取下载 URL for Firebase - Get Download URL from file uploaded with Cloud Functions for Firebase 如何通过带有firebase功能的FTP下载文件? - How to download a file through FTP with a firebase function? Cloud Functions for Firebase - 未配置结算帐号 - Cloud Functions for Firebase - Billing account not configured Firebase Cloud功能-设置服务帐户 - Firebase Cloud Functions - Setting up a service account 使用 Firebase Cloud Functions 创建 Stripe Connect 帐户 - Creating a Stripe Connect account with Firebase Cloud Functions Firebase的云功能图像下载功能错误 - Cloud Functions for Firebase Image download function Error 使用云功能创建pdf文件并将其上传到firebase存储? (可以上传本地文件,但不能从云功能上传) - Create and upload pdf file to firebase storage using cloud functions? (can upload locally file, but not from cloud functions) 使用http cloud云功能从Firebase云存储下载文件时,没有此类文件或目录 - no such file or directory when downloading a file from firebase cloud storage using http cloud cloud functions 如何使用 Cloud Functions 中的“onFinalize”将 300Mb JSON 文件从 Firebase 存储导入数据库? - How to import 300Mb JSON file from Firebase Storage to Database using 'onFinalize' in Cloud Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM