Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It needs to work when pushed to Heroku as well.

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
like image 951
Agans Avatar asked Jan 18 '16 13:01

Agans


People also ask

How do I read a csv file on Amazon S3?

According to the documentation, we can create the client instance for S3 by calling boto3. client("s3") . Then we call the get_object() method on the client with bucket name and key as input arguments to download a specific file.

How do I extract data from AWS S3?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


1 Answers

There are cases with S3, when permissions on S3 Object disallow public access. In-built Ruby functions do assume a path is publicly accessible and don't account for AWS S3 specificity.

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. Line 3. Choose an object and get it as a String. Line 4. Effectively CSV.parse('the string'), but I also added a options and map over it just in case it helps you.

like image 136
marzhaev Avatar answered Sep 29 '22 10:09

marzhaev