Amazon S3 is one of those services that providing Amazon Web Services(AWS). S3 - Opens as Simple Storage Service. Sites like twitter.com and woots.com is using S3. I will not describe what is Amazon S3, to know what is S3 visit official page. I will only tell you about using AWS through PHP.
Firstly I want to say, that official Amazon API is not easy to use under PHP, it is using HTTP Requests to receive and send data. I know 3 API's for using S3(and not only S3). Here is API's and little description for each.
1. Official REST API, in which you straightly working with HTTP Requests. Here is sample code from official site. This code uploads file to AWS server.
Click here to show code
<?php
<pre>$aws_key = '_YOUR_AWS_KEY_000000';
$aws_secret = '_your_aws_secret_00000000000000000000000';
$source_file = 'logo.gif'; // file to upload to S3
$file_type = "image/gif"; // or other file type like "image/jpeg" for JPEG image,
// or "binary/octet-stream" for binary file
$aws_bucket = 'anyexample-test'; // AWS bucket
$aws_object = 'logo.gif'; // AWS object name (file name)
if (strlen($aws_secret) != 40) die("$aws_secret should be exactly 40 bytes long");
$file_data = file_get_contents($source_file);
if ($file_data == false) die("Failed to read file ".$source_file);
// opening HTTP connection to Amazon S3
$fp = fsockopen("s3.amazonaws.com", 80, $errno, $errstr, 30);
if (!$fp) {
die("$errstr ($errno)\n");
}
// Creating or updating bucket
$dt = gmdate('r'); // GMT based timestamp
// preparing String to Sign (see AWS S3 Developer Guide)
$string2sign = "PUT
{$dt}
/{$aws_bucket}";
// preparing HTTP PUT query
$query = "PUT /{$aws_bucket} HTTP/1.1
Host: s3.amazonaws.com
Connection: keep-alive
Date: $dt
Authorization: AWS {$aws_key}:".amazon_hmac($string2sign)."\n\n";
$resp = sendREST($fp, $query);
if (strpos($resp, '<Error>') !== false)
die($resp);
echo "BUCKET created\n";
// done
// Uploading object
$file_length = strlen($file_data); // for Content-Length HTTP field
$dt = gmdate('r'); // GMT based timestamp
// preparing String to Sign (see AWS S3 Developer Guide)
$string2sign = "PUT
{$file_type}
{$dt}
x-amz-acl:public-read
/{$aws_bucket}/{$aws_object}";
// preparing HTTP PUT query
$query = "PUT /{$aws_bucket}/{$aws_object} HTTP/1.1
Host: s3.amazonaws.com
x-amz-acl: public-read
Connection: keep-alive
Content-Type: {$file_type}
Content-Length: {$file_length}
Date: $dt
Authorization: AWS {$aws_key}:".amazon_hmac($string2sign)."\n\n";
$query .= $file_data;
$resp = sendREST($fp, $query);
if (strpos($resp, '<Error>') !== false)
die($resp);
echo "FILE uploaded\n";
// done
echo "Your file's URL is: http://s3.amazonaws.com/{$aws_bucket}/{$aws_object}\n";
fclose($fp);
// Sending HTTP query and receiving, with trivial keep-alive support
function sendREST($fp, $q, $debug = false)
{
if ($debug) echo "\nQUERY<<{$q}>>\n";
fwrite($fp, $q);
$r = '';
$check_header = true;
while (!feof($fp)) {
$tr = fgets($fp, 256);
if ($debug) echo "\nRESPONSE<<{$tr}>>";
$r .= $tr;
if (($check_header)&amp;amp;amp;amp;&amp;amp;amp;amp;(strpos($r, "\r\n\r\n") !== false))
{
// if content-length == 0, return query result
if (strpos($r, 'Content-Length: 0') !== false)
return $r;
}
// Keep-alive responses does not return EOF
// they end with \r\n0\r\n\r\n string
if (substr($r, -7) == "\r\n0\r\n\r\n")
return $r;
}
return $r;
}
// hmac-sha1 code START
// hmac-sha1 function: assuming key is global $aws_secret 40 bytes long
// read more at http://en.wikipedia.org/wiki/HMAC
// warning: key($aws_secret) is padded to 64 bytes with 0x0 after first function call
function amazon_hmac($stringToSign)
{
// helper function binsha1 for amazon_hmac (returns binary value of sha1 hash)
if (!function_exists('binsha1'))
{
if (version_compare(phpversion(), "5.0.0", ">=")) {
function binsha1($d) { return sha1($d, true); }
} else {
function binsha1($d) { return pack('H*', sha1($d)); }
}
}
global $aws_secret;
if (strlen($aws_secret) == 40)
$aws_secret = $aws_secret.str_repeat(chr(0), 24);
$ipad = str_repeat(chr(0x36), 64);
$opad = str_repeat(chr(0x5c), 64);
$hmac = binsha1(($aws_secret^$opad).binsha1(($aws_secret^$ipad).$stringToSign));
return base64_encode($hmac);
}
// hmac-sha1 code END
?>
Keep in mind this code, to compare with next API
2. Second API is Neurofuzzy API. I don't used it never, check it
here.
3. And finally last API -
Tarzan. Tarzan is for all AWS Services. It is big library, flexible and simple. Here is the sample code, which is do same what do first code.
Click here to show sample Tarzan AWS code
<code><?php
require_once('tarzan.class.php');
/**
* Instantiate a new AmazonS3 object using the settings from the config.inc.php file.
*/
$s3 = new AmazonS3();
/**
* Store a remote file in S3.
*/
$file_url = $s3->store_remote_file('http://yahoo.com', 'warpshare.test.eu', 'yahoo_home.html', array(
'acl' => S3_ACL_PUBLIC
));
/**
* Display the new S3 URL for the stored file. (http://warpshare.test.eu.s3.amazonaws.com/yahoo_home.html)
*/
echo $file_url;
?> </code>
August 13th, 2009 - 03:41
Hi, I can’t understand how to add your site in my rss reader. Can you Help me, please