简体   繁体   English

无法从 Amazon EC2 实例下的一台 FTP 服务器使用 Ruby 获取二进制文件

[英]Cannot get binaryfile using Ruby from one FTP server under Amazon EC2 instance

I have part of the code which is to get the binary file stream from one FTP server.我有一部分代码是从一个 FTP 服务器获取二进制文件流。

It works on my Ubuntu but the code cannot get the binary file from the FTP server when I tried it on the Amazon EC2 instance.它适用于我的 Ubuntu,但当我在 Amazon EC2 实例上尝试时,代码无法从 FTP 服务器获取二进制文件。

I tried to switch to another gem called open-uri.我尝试切换到另一个名为 open-uri 的 gem。 It can get the binary file stream on both my local PC and remote EC2 instance.它可以在我的本地 PC 和远程 EC2 实例上获取二进制文件流。

I use the default VPC of EC2 instance.我使用 EC2 实例的默认 VPC。

I already opened port 20 and 21 on EC2.我已经在 EC2 上打开了端口 20 和 21。 I used dig ftp.cga.ct.gov and there is a answer on EC2.我使用dig ftp.cga.ct.gov并且在 EC2 上有答案。

If there is some point which is not clear for your guys, please point it out如果小伙伴们有什么不明白的地方,请指出

Here is my code in the initialize method of one Ruby class:这是我在一个 Ruby 类的 initialize 方法中的代码:

def initialize(session_id)
  @session_id = session_id
  @count  = 1
  tries   = 10
  begin
      ftp = Net::FTP.new("ftp.cga.ct.gov")
      ftp.read_timeout = 500
      ftp.login
      ftp.chdir('/pub/data/')
      bill_str= ftp.getbinaryfile("bill_info.csv", nil)
      @bill_array = bill_str.delete("\"").split("\r\n")[1..-1]
  rescue Exception => e
      if (tries -= 1) > 0
        sleep 10
        print "re-connect"
        retry
      else
        print "Cannot open FTP\nThe error message is #{e}\n#{e.backtrace.join("\n")}"
     end
  else
    return true
  end
  super
end 

I think it is totally because of Amazon internal subnet bugs.我认为这完全是因为亚马逊内部子网的错误。 Check my other question and the answer from myself.检查我的另一个问题和我自己的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM