从 AWS S3 读取 CSV 文件时获取“Errno::ENOENT: No such file or directory @ rb_sysopen”

2024-02-04

我有部署到 Heroku 的应用程序。我添加了通过 CSV 上传用户的功能。为此,我提供了 CSV 上传功能(Used Paperclip gem)。

这是我用于读取文件和创建新用户的代码

def import(file)
  CSV.foreach(file.path, headers: true) do |row|
      row_hash = row.to_hash.values
      data = row_hash[0].split("\t")
      .
      .
      .
end

在本地它运行良好。但在heroku上它给了我以下错误

Errno::ENOENT: No such file or directory @ rb_sysopen - https://s3.amazonaws.com/..../..../sample_csv(2).csv

我参考了以下链接amazon-s3 中的 Errno::ENOENT (没有此类文件或目录) https://stackoverflow.com/questions/15220428/errnoenoent-no-such-file-or-directory-in-amazon-s3

从 Amazon 服务器读取文件,Ruby on Rails,没有匹配路线 https://stackoverflow.com/questions/30410951/file-reading-from-amazon-server-ruby-on-rails-no-match-route

但没有任何成功。为了进行更多调试,我从本地 Rails 控制台尝试了相同的 url,它给了我同样的错误。

2.2.2 :008 > cp = "https://s3.amazonaws.com/..../..../sample_csv(2).csv"
2.2.2 :008 > f = File.open(cp, "r")
Errno::ENOENT: No such file or directory @ rb_sysopen - https://s3.amazonaws.com

也尝试过打开urihttp://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html http://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html.

我可以从浏览器下载相同的文件。

任何人都可以让我知道如何解决此错误。是否存在存储桶权限问题(我已经提供了存储桶的开放访问权限)。


尝试这个

require 'open-uri'
require 'csv'

def import(file)
  CSV.new(open(file), :headers => :true).each do |row|  #First open the file using open
      row_hash = row.to_hash.values
      data = row_hash[0].split("\t")
      .
      .
      .
end

更多信息您可以参考这个链接 https://stackoverflow.com/questions/6005139/ruby-1-9-2-read-and-parse-a-remote-csv

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 AWS S3 读取 CSV 文件时获取“Errno::ENOENT: No such file or directory @ rb_sysopen” 的相关文章

随机推荐