Ruby-Rails serve ftp file direct to client

后端 未结 2 1754
死守一世寂寞
死守一世寂寞 2020-12-18 16:51

I am new to ruby and to rails, so excuse my question... . What i want to know is, how to take a file from a ftp server with ruby without saving the file on my rails applicat

2条回答
  •  醉酒成梦
    2020-12-18 17:31

    Do you want the following?

    1) Client (browser) sends a request to the Rails server

    2) Server should respond with the contents of a file that is located on an ftp server.

    Is that it?

    If so, then simply redirect the browser to the ftp location. Eg

     # in controller
     ftp_url = "ftp://someserver.com/dir_name/file_name.txt
     redirect_to ftp_url
    

    The above works if the ftp file has anonymous get access.

    If you really need to access the file from the server and stream it, try the following:

    # in controller
    render :text => proc {|response, output|
      ftp_session = FTP.open(host, user, passwd, acct)
      ftp_session.gettextfile(remotefile) {|data| output.write(data)}
      ftp_session.close
      }
    

    You should check the headers in the response to see if they're what you want.

    ps. Setting up the ftp connection and streaming from a second server will probably be relatively slow. I'd use JS to show a busy graphic to the user.

    I'd try alternatives to ftp. Can you set up an NFS connection or mount the remote disk? Would be much faster than ftp. Also investigate large TCP window sizes.

提交回复
热议问题