Created
June 6, 2014 18:23
-
-
Save jeremeamia/042254f6fe2982745b03 to your computer and use it in GitHub Desktop.
Shows how to read a CSV stored in S3 using the AWS SDK for PHP's S3 Stream Wrapper.
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
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
$s3 = Aws\S3\S3Client::factory($config); | |
$s3->registerStreamWrapper(); | |
$url = 's3://{$bucket}/{$key}'; | |
// Read CSV with fopen | |
$file = fopen($url, 'r'); | |
$keys = fgetcsv($file); | |
while (!feof($file)) { | |
$row = array_combine($keys, fgetcsv($file)); | |
print_r($row); | |
} | |
// Read CSV with SplFileObject | |
$file = new \SplFileObject($url, 'r'); | |
$keys = $file->fgetcsv(); | |
while (!$file->eof()) { | |
$row = array_combine($keys, $file->fgetcsv()); | |
print_r($row); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't it necessary to close the stream ? Is it fine if we don't close the stream ?