Amazon S3 (Simple Storage Service) – Delete Buckets using PHP
This article will give you brief tutorial on deleting buckets on Amazon S3 Servers using PHP code.
Software Requirements:
- Crypt_HMAC
Download URL: CRYPT_HMAC - HTTP_REQUEST
Download URL: HTTP_REQUEST - PEAR
Download URL: PEAR
Below is the PHP Code Snippet that allows you to delete the bucket.
< ? require_once 'Crypt/HMAC.php'; require_once 'HTTP/Request.php';</code> define ('DATE_RFC822',"D, d M Y G:i:s O"); define ('S3_URL',"http://s3.amazonaws.com/"); define ('KEYID',"Amazon Key Id"); define ('SECRETKEY',"Amazon Secret Key"); define ('PRIVATE',"private"); define ('DELETE',"DELETE"); function delete_bucket($bucketname, $method = DELETE, $acl = PRIVATE) { $httpDate = gmdate(DATE_RFC822); $stringToSign = "$method\n\n\n$httpDate\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->sendRequest(); if ($req->getResponseCode() == 200) { echo "Bucket deleted successfully"; } else { echo "Bucket Not deleted Successfully"; } } delete_bucket("bucket name",DELETE,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 delete_bucket function to perform the job of deleting the bucket.
Custom Search


































