简体   繁体   English

如何解析位于 Amazon S3 存储桶中的 CSV 文件

[英]How do I parse a CSV file located in a Amazon S3 bucket

Below is the code I'm using to parse the CSV from within the app, but I want to parse a file located in a Amazon S3 bucket.下面是我用来从应用程序中解析 CSV 的代码,但我想解析位于 Amazon S3 存储桶中的文件。 It needs to work when pushed to Heroku as well.当推送到 Heroku 时它也需要工作。

namespace :csvimport do
  desc "Import CSV Data to Inventory."
  task :wiwt => :environment do
    require 'csv'

    csv_file_path = Rails.root.join('public', 'wiwt.csv.txt')

    CSV.foreach(csv_file_path) do |row|
      p = Wiwt.create!({
        :user_id => row[0],
        :date_worn => row[1],
        :inventory_id => row[2],
      })
    end
  end
end

There are cases with S3, when permissions on S3 Object disallow public access. S3 在某些情况下,S3 对象的权限不允许公共访问。 In-built Ruby functions do assume a path is publicly accessible and don't account for AWS S3 specificity.内置的 Ruby 函数确实假定路径是可公开访问的,并且不考虑 AWS S3 的特殊性。

s3 = Aws::S3::Resource.new
bucket = s3.bucket("bucket_name_here")
str = bucket.object("file_path_here").get.body.string
content = CSV.parse(str, col_sep: "\t", headers: true).map(&:to_h)

Per-line explanation using AWS SDK: Line 1. Initialize Line 2. Choose a bucket.使用 AWS SDK 的每行解释:第 1 行。初始化第 2 行。选择一个存储桶。 Line 3. Choose an object and get it as a String.第 3 行。选择一个对象并将其作为字符串获取。 Line 4. Effectively CSV.parse('the string'), but I also added a options and map over it just in case it helps you.第 4 行。实际上是 CSV.parse('the string'),但我还添加了一个选项并在其上进行映射,以防万一它对您有帮助。

You can do it like this你可以这样做

CSV.new(open(path_to_s3)).each do |row|
  ...
end

This worked for me这对我有用

  open(s3_file_path) do |file|
    CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row|
      Model.create(row.to_hash)
    end
  end

You can get the csv file from S3 like this:您可以像这样从 S3 获取 csv 文件:

require 'csv'
require 'net/http'

CSV.parse(Net::HTTP.get(s3_file_url), headers: true).each do |row|
# code for processing row here
end

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

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