Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Created June 6, 2014 18:23
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jeremeamia/042254f6fe2982745b03 to your computer and use it in GitHub Desktop.
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.
<?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);
}
@SarthakShahCC
Copy link

SarthakShahCC commented Feb 5, 2020

Isn't it necessary to close the stream ? Is it fine if we don't close the stream ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment