gpt4 book ai didi

ruby - 如何在不先保存文本文件的情况下在 Ruby 中使用 FTP

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:49 24 4
gpt4 key购买 nike

由于 Heroku 不允许将动态文件保存到磁盘,我遇到了一个难题,希望您能帮助我克服。我有一个可以在 RAM 中创建的文本文件。问题是我找不到允许我将文件流式传输到另一个 FTP 服务器的 gem 或函数。我使用的 Net/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

ftp.puttextfile 函数要求物理文件存在。

最佳答案

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

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

关于ruby - 如何在不先保存文本文件的情况下在 Ruby 中使用 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5223763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com