Backup Amazon S3 with PHP

On August 19, 2009, in Amazon Cloud EC2 S3, PHP, Tips, Tricks, Web Services, by phpsolutions

This is the source code to backup/download Amazon S3 Bucket’s multimedia files to other server by PHP…

here $bucketurl is XML url of Amazon S3 bucket with full ACL.
$folder is path of the server where S3 files to store.

This script will download all files in Amazon bucket to other server

<?php
/* url for Amazon Bucket */
$bucketurl = “http://BUCKETNAME.s3.amazonaws.com/”;
$xml = file_get_contents($bucketurl);

/* folder name for server */
$folder = “/var/www/html/BUCKETNAME/”;

$content = getTag( ‘Key’, $xml );

foreach($content as $file)
{
exec( “wget -O “.$folder.$file.” “.$bucketurl.$file );
}

/* function to get node from Amazon Bucket XML */
function getTag( $tag, $xml )
{
$tag = preg_quote($tag);
preg_match_all(‘{<‘.$tag.'[^>]*>(.*?)</’.$tag.’>}’, $xml, $matches, PREG_PATTERN_ORDER);
return $matches[1];
}

?>

2 Responses to Backup Amazon S3 with PHP

  1. Anonymous says:

    I’m really thankful towards the author with this post for making this lovely and informative article live here for us. We really appreciate ur effort. Maintain the nice work. . . .

  2. Anonymous says:

    Excellent read, I simply passed this onto a colleague who had been doing a little research on that. And he actually bought me lunch because I discovered it for him smile So ok , i’ll rephrase that: Many thanks lunch!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.