Just updated a PHP script I created to create test data files, such that it can stream the result directly to Amazon S3. Figured somebody else may want to do it at some point (useful for creating large files on S3 without having to store them locally first).
All I had to do was install the AWS S3 PHP SDK (I opted to use the .phar
file option). For our purposes, I require the user to set the AWS key and secret in the terminal environment before running the script, e.g.:
export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxx
export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxx
php my_script.php -o s3::/bucket_name/file_name.json
Within the PHP script, I check to see if the output is to S3, and if so, do the magic stuff:
<?php
// install the AWS SDK
require_once __DIR__ . '/aws.phar';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// $file gets set per command line args, then...
$fh = file_handle($file);
for($i=1; $i<=100; $i++) { // not what I actually do, but you get the idea ;)
fputs($fh, "Hello, World $i\n");
}
fclose($fh);
// where the magic happens:
function file_handle($file)
{
if (stripos($file, 's3://') === 0) { // stream to S3
if (empty($_ENV['AWS_ACCESS_KEY_ID']) or empty($_ENV['AWS_SECRET_ACCESS_KEY'])) {
die("ERROR: You must set shell environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to use S3\n");
}
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
$s3->registerStreamWrapper();
}
if (($fh = fopen($file, 'w')) == false) {
die("ERROR: failed to open file hande for '$file'\n");
}
return $fh;
}
Less code than I first expected when my manager asked if we could stream directly to S3. 🙂