This is a quick wrapper around an S3 class by Donovan Schonknecht that will recursively upload your files in a subdirectory to an S3 bucket, applying Cache Control control headers as it goes.
The original intent was to have a quick way to sync images on a development machine with a live S3 bucket that was serving images on a site, without having to use any crappy third-party software. This was spawned out of the need for a simple uploader that actually works. So far it's been great.
<?
// quick config
$bucket = 'your.bucket.com';
$start_folder = 'images';
// settings
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
// include S3 class
include 'S3.php';
$s3 = new S3('[your key]', '[your secret]', false);
// get list of files. if you don't want a subdirectory, just change this line to not need one. hopefully you know PHP...
$files = recurse(array(), $start_folder);
// loop over files and upload
for($i = 0, $n = count($files); $i < $n; $i++)
{
$ext = preg_replace('/.*\./', '', $files[$i]);
$type = 'image/jpeg';
if($ext == 'jpg')
{
$type = 'image/jpeg';
}
else if($ext == 'gif')
{
$type = 'image/gif';
}
else if($ext == 'png')
{
$type = 'image/png';
}
if(
!$s3->putObject(
$s3->inputFile($files[$i]),
$bucket,
$files[$i],
S3::ACL_PUBLIC_READ,
array(),
array('Cache-Control' => 'max-age=31536000', 'Content-Type' => $type)
)
)
{
echo '<span style="color:green;">Failed: upload of '. $files[$i] . '';
}
else
{
echo '<span style="color:green;">Succeeded: upload of '. $files[$i] .'';
unlink($files[$i]);
}
}
function recurse($files, $dir)
{
$d = scandir($dir);
for($i = 0, $n = count($d); $i < $n; $i++)
{
if(!preg_match('/^\./', $d[$i]))
{
if(is_dir($dir . '/' . $d[$i]))
{
$files = recurse($files, $dir . '/' . $d[$i]);
}
else
{
$files[] = $dir . '/' . $d[$i];
}
}
}
return $files;
}
?>
Enjoy.