Upload CSV stream from Ruby to S3

前端 未结 3 637
陌清茗
陌清茗 2021-02-13 20:56

I am dealing with potentially huge CSV files which I want to export from my Rails app, and since it runs on Heroku, my idea was to stream these CSV files directly to S3 when gen

3条回答
  •  长发绾君心
    2021-02-13 21:07

    s3 = Aws::S3::Resource.new(region:'us-west-2')
    obj = s3.bucket.object("#{FOLDER_NAME}/#{file_name}.csv")
    file_csv = CSV.generate do |csv|
        csv << ActionLog.column_names
        ActionLog.all.each do |action_log|
          csv << action_log.attributes.values
        end
      end
      obj.put body: file_csv
    

    file_csv = CSV.generate is to create a string of CSV data in Ruby. After creating this string of CSV, we put to S3 using bucket, with the path

    #{FOLDER_NAME}/#{file_name}.csv
    

    In my code, I export all the data to an ActionLog model.

提交回复
热议问题