Created
March 9, 2018 02:37
-
-
Save lastday154/3e875bb40f7bd92044bd696732b8cc31 to your computer and use it in GitHub Desktop.
Read from S3 aws
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Get data from S3 as Stream Wrapper | |
| * @param $catalogId | |
| * @param $fileName | |
| * @param bool $headerOnly | |
| * @param int $numRow | |
| * @return array | |
| */ | |
| public function getContentCsvS3($catalogId, $fileName, $headerOnly = false, $numRow = 0) { | |
| $s3Client = S3Client::factory(array('key' => S3KEY_FEED, 'secret' => S3SECRET_FEED)); | |
| $s3Client->registerStreamWrapper(); | |
| $key = S3PATH_FEED . "{$catalogId}/upload/$fileName"; | |
| $url = 's3://' . S3BUCKETNAME_FEED. "{$key}"; | |
| # Check delimiter here | |
| $delimiter = ','; | |
| if (($handle = fopen($url, "r")) !== FALSE) { | |
| #get delimiter for this file (csv, tsv) | |
| $helper = new ProductFeedHelper(); | |
| $delimiter = $helper->getCsvDelimiter($handle); | |
| fclose($handle); | |
| } | |
| #start read file content | |
| $csvRows = []; | |
| $currentRow = 0; | |
| if (($handle = fopen($url, "r")) !== FALSE) { | |
| try { | |
| $header = 1; | |
| while (($data = fgetcsv($handle, 0, $delimiter)) !== FALSE) { | |
| foreach ($data as &$col) { | |
| if ($header) { | |
| $col = strtolower($this->stdReadableCharacter(mb_convert_encoding($col, 'utf-8'))); | |
| } else { | |
| $col = mb_convert_encoding($col, 'utf-8', "ASCII,JIS,UTF-8,EUC-JP,SJIS"); | |
| $currentRow++; | |
| } | |
| } | |
| $header = 0; | |
| $csvRows[] = $data; | |
| if($headerOnly || ($numRow > 0 && $currentRow == $numRow)) { | |
| break; | |
| } | |
| } | |
| fclose($handle); | |
| } catch (Exception $e) { | |
| fclose($handle); | |
| } | |
| } | |
| return $csvRows; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment