How to read csv from S3 bucket and insert into database

Author - Webner

While working on a project, we wanted to read csv from s3 bucket, store this data in another local file and insert it into database. We had S3 bucket url where csv was kept.

Samp S3 URL -‘http://www.xyz.com/1234.csv’;

Below is the sample code which reads the csv from S3 bucket and inserts the data into local file.

$file=file_put_contents(‘localFile.csv’, file_get_contents($url));

Csv file was tab separated so need to separate with /t

Below is the sample code:

$url='http://www.xyz.com/1234.csv';
$csv_file = "localFile.csv";
//put the code data in a local CSV from AWS S3 url
$file=file_put_contents('localFile.csv', file_get_contents($url));
//now read local csv record by record and update database
if (($handle = fopen($csv_file, "r")) !== FALSE) 
      {
	fgetcsv($handle);
	while (($data = fgetcsv($handle,10000,"\t")) !== FALSE) 
          {
            //code here to save data in database
          }
       }
fclose($csv_file);

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message