Amazon S3 (Simple Storage Service) - Create Buckets using PHP

This article will give you brief tutorial on creating buckets on Amazon S3 Servers using PHP code.

Software Requirements:






Below is the PHP Code Snippet that allows you to creating buckets.

< ?
require_once 'Crypt/HMAC.php';
require_once 'HTTP/Request.php';
define ('DATE_RFC822',"D, d M Y G:i:s O");
define ('S3_URL',"http://s3.amazonaws.com/");
define ('KEYID',"Amazon Account Key ID");
define ('SECRETKEY',"Amazon Account Secret Key");
define ('PRIVATE',"private");
define ('PUT',"PUT");
 
function create_bucket($bucketname, $method = PUT, $acl = PRIVATE)
{
$httpDate = gmdate(DATE_RFC822);
$stringToSign = "$method\n\n\n$httpDate\nx-amz-acl:$acl\n/$bucketname";
$hasher =& new Crypt_HMAC(SECRETKEY, "sha1");
$signature = hex2b64($hasher->hash($stringToSign));
$req =& new HTTP_Request(S3_URL . $bucketname);
$req->setMethod($method);
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . KEYID . ":" . $signature);
$req->addHeader("x-amz-acl", $acl);
$req->sendRequest();
if ($req->getResponseCode() == 200) 
{
	echo "<br />Bucket created Successfully";
} 
else 
{
	echo "<br />Bucket Not Created Successfully";
}
}
create_bucket("Bucket Name",PUT,PRIVATE);
?>

If you see the above code snippet first two lines is compulsory. It tells the PHP to load the CRYPT_HMAC and HTTP_REQUEST library required for uploading files to Amazon Simple Storage Service. After that defined all the constants. Then the create_bucket function to perform the job of creating bucket.

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

hello…

wonderful…

Leave a comment

(required)

(required)