从轨道4使用袋鼠流中读取一个CSV文件 [英] Read a CSV file from a stream using Roo in Rails 4

查看:100
本文介绍了从轨道4使用袋鼠流中读取一个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个另外一个问题在这里打开从S3采用袋鼠在Heroku CSV文件,但我没有任何优势的话 - 这样一个改写:

I have another question on this here Open a CSV file from S3 using Roo on Heroku but I'm not getting any bites - so a reword:

我有一个S3桶CSV文件 我希望它用小豆在Heroku的基于应用程序(即没有本地文件访问)来读取 如何从一个流中打开CSV文件?

I have a CSV file in an S3 bucket I want to read it using Roo in a Heroku based app (i.e. no local file access) How do I open the CSV file from a stream?

或者有更好的工具,这样做呢?

Or is there a better tool for doing this?

我使用Rails 4,红宝石2。注意,我可以successfuly打开CSV阅读,如果我从形式发布。我怎样才能适应这从S3存储单元的文件?

I am using Rails 4, Ruby 2. Note I can successfuly open the CSV for reading if I post it from a form. How can I adapt this to snap the file from an S3 bucket?

推荐答案

简单的答案 - 。不要用小豆

Short answer - don't use Roo.

我结束了使用标准的CSV命令,以小CSV文件的工作,你可以使用这样的事情很简单的读取文件内容加载到内存:

I ended up using the standard CSV commands, working with small CSV files you can very simply read the file contents into memory using something like this:

body = file.read
CSV.parse(body, col_sep: ",", headers: true) do |row|
    row_hash = row.to_hash
    field = row_hash["FieldName"]

读取一种形式,通过的文件,只需引用PARAMS:

reading a file passed in from a form, just reference the params:

file = params[:file]
body = file.read

要读取的形式S3可以使用AWS宝石:

To read in form S3 you can use the AWS gem:

s3 = AWS::S3.new(access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])
bucket = s3.buckets['BUCKET_NAME']
# check each object in the bucket
bucket.objects.each do |obj|
    import_file = obj.key
    body = obj.read
    # call the same style import code as above...
end

这篇关于从轨道4使用袋鼠流中读取一个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆