如何在Ruby中FTP时不先保存文本文件

时间:2022-02-14 21:45:44

Since Heroku does not allow saving dynamic files to disk, I've run into a dilemma that I am hoping you can help me overcome. I have a text file that I can create in RAM. The problem is that I cannot find a gem or function that would allow me to stream the file to another FTP server. The Net/FTP gem I am using requires that I save the file to disk first. Any suggestions?

由于Heroku不允许将动态文件保存到磁盘中,我遇到了一个难题,我希望您能帮助我克服这个难题。我有一个可以在RAM中创建的文本文件。问题是我找不到一个gem或函数来允许我将文件流到另一个FTP服务器。我正在使用的网络/ ftp gem要求我首先将文件保存到磁盘。有什么建议吗?

ftp = Net::FTP.new(domain)
ftp.passive = true
ftp.login(username, password)
ftp.chdir(path_on_server)
ftp.puttextfile(path_to_web_file)
ftp.close

The ftp.puttextfile function is what is requiring a physical file to exist.

ftp。puttextfile函数需要一个物理文件。

2 个解决方案

#1


19  

StringIO.new provides an object that acts like an opened file. It's easy to create a method like puttextfile, by using StringIO object instead of file.

StringIO。new提供了一个像打开的文件一样的对象。通过使用StringIO对象而不是文件,很容易创建像puttextfile这样的方法。

require 'net/ftp'
require 'stringio'

class Net::FTP
  def puttextcontent(content, remotefile, &block)
    f = StringIO.new(content)
    begin
      storlines("STOR " + remotefile, f, &block)
    ensure
      f.close
    end
  end
end

file_content = <<filecontent
<html>
  <head><title>Hello!</title></head>
  <body>Hello.</body>
</html>
filecontent

ftp = Net::FTP.new(domain)
ftp.passive = true
ftp.login(username, password)
ftp.chdir(path_on_server)
ftp.puttextcontent(file_content, path_to_web_file)
ftp.close

#2


5  

David at Heroku gave a prompt response to a support ticket I entered there.

Heroku的大卫迅速回复了我进入那里的一张支持票。

You can use APP_ROOT/tmp for temporary file output. The existence of files created in this dir is not guaranteed outside the life of a single request, but it should work for your purposes.

您可以对临时文件输出使用APP_ROOT/tmp。在这个目录中创建的文件的存在并不能在单个请求的生命周期之外得到保证,但是它应该适合您的目的。

Hope this helps, David

希望这有助于,大卫

#1


19  

StringIO.new provides an object that acts like an opened file. It's easy to create a method like puttextfile, by using StringIO object instead of file.

StringIO。new提供了一个像打开的文件一样的对象。通过使用StringIO对象而不是文件,很容易创建像puttextfile这样的方法。

require 'net/ftp'
require 'stringio'

class Net::FTP
  def puttextcontent(content, remotefile, &block)
    f = StringIO.new(content)
    begin
      storlines("STOR " + remotefile, f, &block)
    ensure
      f.close
    end
  end
end

file_content = <<filecontent
<html>
  <head><title>Hello!</title></head>
  <body>Hello.</body>
</html>
filecontent

ftp = Net::FTP.new(domain)
ftp.passive = true
ftp.login(username, password)
ftp.chdir(path_on_server)
ftp.puttextcontent(file_content, path_to_web_file)
ftp.close

#2


5  

David at Heroku gave a prompt response to a support ticket I entered there.

Heroku的大卫迅速回复了我进入那里的一张支持票。

You can use APP_ROOT/tmp for temporary file output. The existence of files created in this dir is not guaranteed outside the life of a single request, but it should work for your purposes.

您可以对临时文件输出使用APP_ROOT/tmp。在这个目录中创建的文件的存在并不能在单个请求的生命周期之外得到保证,但是它应该适合您的目的。

Hope this helps, David

希望这有助于,大卫