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

前端 未结 3 1001
-上瘾入骨i
-上瘾入骨i 2021-01-05 06:08

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.

3条回答
  •  猫巷女王i
    2021-01-05 06:14

    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.

提交回复
热议问题