Amazon s3 example

Here i give you a sample code , in php, for amazon s3 file upload. Read the code carefully and try to implement.
Best wishes.
// grab this with “pear install Crypt_HMAC”
require_once ‘HMAC.php’;
// grab this with “pear install –onlyreqdeps HTTP_Request”
require_once ‘Request.php’;
// Note that version HTTP_Request 1.3.0 has a BUG in it! Change line
// 765 from:
// (HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// to:
// (HTTP_REQUEST_METHOD_POST == $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// Without this change PUTs with non-empty content-type will fail!
//Click Read more to continue
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
class s3{
var $serviceUrl;
var $accessKeyId;
var $secretKey;
var $responseString;
var $responseCode;
var $parsed_xml;
/**
* Constructor
*
* Takes ($accessKeyId, $secretKey, $serviceUrl)
*
* - [str] $accessKeyId: Your AWS Access Key Id
* - [str] $secretKey: Your AWS Secret Access Key
* - [str] $serviceUrl: OPTIONAL: defaults: http://s3.amazonaws.com/
*
*/
function __construct($accessKeyId, $secretKey, $serviceUrl="http://s3.amazonaws.com/") {
$this->serviceUrl=$serviceUrl;
$this->accessKeyId=$accessKeyId;
$this->secretKey=$secretKey;
}
/**
* createBucket -- creates a bucket.
*
* Takes ($bucket, $acl)
*
* - [str] $bucket: the bucket you wish to create
* - [str] $acl: the access control policy (OPTIONAL: defaults to 'private')
*/
function createBucket($bucket, $acl = 'private') {
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "PUT\n\n\n$httpDate\nx-amz-acl:$acl\n/$bucket";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl . $bucket);
$req->setMethod("PUT");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->addHeader("x-amz-acl", $acl);
$req->sendRequest();
$this->responseCode=$req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* deleteBucket -- Deletes an empty bucket.
*
* Takes ($bucket)
*
* - [str] $bucket: the bucket you wish to delete
*/
function deleteBucket($bucket) {
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "DELETE\n\n\n$httpDate\n/$bucket";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl . $bucket);
$req->setMethod("DELETE");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 204) {
return true;
} else {
return false;
}
}
/**
* emptyBucket -- Deletes all keys in specified bucket.
*
* Takes ($bucket)
*
* - [str] $bucket: the bucket you wish to empty
*/
function emptyBucket($bucket) {
if($this->listKeys($bucket)){//get keys from S3 bucket
$more = $this->parsed_xml->IsTruncated; //determine of all keys in bucket are returned on this list call
$keys = $this->parsed_xml->Contents;
//set up array for catching keys that aren't successfully deleted and set the initial count of these to 0
$not_deleted_keys = array();
$not_deleted_count = 0;
foreach($keys as $current){//try up to 3 times to delete current key
$tries=1;
while(!$this->deleteObject($bucket, $current->Key) && $tries<=3){
$tries++;
}
if($tries>3){//capture any keys that aren't deleted
$not_deleted_keys[$not_deleted_count] = $current->Key;
$not_deleted_count++;
}
}
} else {
echo "listKeys() failed";
return false;
}
if($not_deleted_count > 0){
echo "Warning - The following keys were not deleted:
";
foreach($not_deleted_keys as $key){
echo $key."
";
}
}
if($more == "true") //call emptyBucket if not all keys were returned with last list call
$this->emptyBucket($bucket);
return true;
}
/**
* listBuckets -- Lists all buckets.
*/
function listBuckets() {
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign="GET\n\n\n$httpDate\n/";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl);
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if($this->responseCode == 200){
return true;
}
else{
return false;
}
}
/**
* listKeys -- Lists keys in a bucket.
*
* Takes ($bucket [,$marker][,$prefix][,$delimiter][,$maxKeys]) -- $marker, $prefix, $delimeter, $maxKeys are independently optional
*
* - [str] $bucket: the bucket whose keys are to be listed
* - [str] $marker: keys returned will occur lexicographically after $marker (OPTIONAL: defaults to false)
* - [str] $prefix: keys returned will start with $prefix (OPTIONAL: defaults to false)
* - [str] $delimiter: keys returned will be of the form "$prefix[some string]$delimeter" (OPTIONAL: defaults to false)
* - [str] $maxKeys: number of keys to be returned (OPTIONAL: defaults to 1000 - maximum allowed by service)
*/
function listKeys($bucket, $marker=FALSE, $prefix=FALSE, $delimiter=FALSE, $maxKeys='1000') {
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "GET\n\n\n$httpDate\n/$bucket";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$bucket."?max-keys={$maxKeys}&marker={$marker}&prefix={$prefix}&delimiter={$delimiter}");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if($this->responseCode == 200){
return true;
} else {
return false;
}
}
/**
* getBucketACL -- Gets bucket access control policy.
*
* Takes ($bucket)
*
* - [str] $bucket: the bucket whose acl you want
*/
function getBucketACL($bucket){
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "GET\n\n\n$httpDate\n/$bucket/?acl";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$bucket.'/?acl');
$req->setMethod("GET");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* setBucketACL -- Sets bucket access control policy to one of Amazon S3 canned policies.
*
* Takes ($bucket, $acl)
*
* - [str] $bucket: the bucket whose acl is to be set
* - [str] $acl: one of the Amazon S3 canned access policies
*/
function setBucketACL($bucket, $acl){
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "PUT\n\n\n{$httpDate}\nx-amz-acl:$acl\n/$bucket/?acl";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$bucket.'/?acl');
$req->setMethod("PUT");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->addHeader("x-amz-acl", $acl);
$req->sendRequest();
$this->responseCode=$req->getResponseCode();
$this->responseString=$req->getResponseBody();
$this->parsed_xml=simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* grantLoggingPermission -- allows logs to be written to the bucket.
*
* Takes ($bucket)
*
* - [str] $bucket
*/
function grantLoggingPermission($bucket){
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "PUT\n\ntext/plain\n{$httpDate}\n/$bucket/?acl";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$bucket.'/?acl');
$req->setMethod("PUT");
//The body below is meant in part as an example of how to grant specific groups certain permissions. In practice, use the getBucketACL method to obtain
//the owner Id and display name. The and elements will have to be filled in accordingly here. These can be obtained by using the Get
//Object ACL or Get Bucket ACL method on an existing object or bucket respectively.Note that this was written with See the the idea that a new bucket
//first be created with the sole purpose of housing logs. As is, this method will overwrite the ACP for an existing bucket. See the Developer Guide for
//more details on granting permissions.
$body = "
FULL_CONTROL
http://acs.amazonaws.com/groups/s3/LogDelivery
WRITE
http://acs.amazonaws.com/groups/s3/LogDelivery
READ_ACP
";
$req->setBody($body);
$req->addHeader("Date", $httpDate);
$req->addHeader("Content-Type", "text/plain");
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode=$req->getResponseCode();
$this->responseString=$req->getResponseBody();
$this->parsed_xml=simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* getLoggingStatus -- gets a bucket's logging status (is logging enabled?).
*
* Takes ($bucket)
*
* - [str] $bucket
*/
function getLoggingStatus($bucket){
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "GET\n\n\n$httpDate\n/$bucket?logging";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$bucket.'?logging');
$req->setMethod("GET");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* enableLogging -- Turns logging feature on/off for a given bucket.
*
* Takes ($bucket, $targetBucket, $targetPrefix, $switch)
*
* - [str] $bucket: Bucket for which logging is to be turned on.
* - [str] $targetBucket: Bucket to which logs will be sent.
* - [str] $targetPrefix: Prefix for key of each log entry.
* - [bool] $switch: True to turn logging on, False to turn it off.
*/
function enableLogging($bucket, $targetBucket, $targetPrefix, $switch) {
$httpDate = gmdate("D, d M Y G:i:s T");
$stringToSign = "PUT\n\ntext/xml\n{$httpDate}\n/$bucket?logging";
$signature = $this->constructSig($stringToSign);
$req = & new HTTP_Request($this->serviceUrl.$bucket.'?logging');
$req->setMethod("PUT");
$req->addHeader("Date", $httpDate);
$req->addHeader("Content-Type", "text/xml");
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
if($switch){
$body = "
$targetBucket
$targetPrefix
";
} else {
$body = "
";
}
$req->setBody($body);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* putObject -- Writes a file to a bucket.
*
* Takes ($bucket, $key, $filePath, $contentType, $contentLength [,$acl][, $metadataArray], [$md5])
*
* - [str] $bucket: the bucket into which file will be written
* - [str] $key: key of written file
* - [str] $contentType: file content type
* - [str] $contentLength: file content length
* - [str] $acl: access control policy of file (OPTIONAL: defaults to 'private')
* - [array] $metadataArray: associative array containing user-defined metadata (name=>value) (OPTIONAL)
* - [bool] $md5: includes the MD5 hash of the object if true (OPTIONAL)
*/
function putObject($bucket, $key, $filePath, $contentType, $contentLength, $acl, $metadataArray, $md5){
sort($metadataArray);
$resource = $bucket."/".urlencode($key);
$req = & new HTTP_Request($this->serviceUrl.$resource);
$req->setMethod("PUT");
$httpDate = gmdate("D, d M Y G:i:s T");
$req->addHeader("Date", $httpDate);
$req->addHeader("Content-Type", $contentType);
$req->addHeader("Content-Length", $contentLength);
$req->addHeader("x-amz-acl", $acl);
if($md5){
$MD5 = $this->hex2b64(md5_file($filePath));
$req->addHeader("Content-MD5", $MD5);
}
$req->setBody(file_get_contents($filePath));
$stringToSign="PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n";
foreach($metadataArray as $current){
if($current!=""){
$stringToSign.="x-amz-meta-$current\n";
$header = substr($current,0,strpos($current,':'));
$meta = substr($current,strpos($current,':')+1,strlen($current));
$req->addHeader("x-amz-meta-$header", $meta);
}
}
$stringToSign.="/$resource";
$signature = $this->constructSig($stringToSign);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* putObjectStream -- Streams data to a bucket. (Note: this method was written by Carson McDonald and can be found on the Mission Data Blog)
* Mission Data Blog: http://blogs.missiondata.com/linux/49/s3-streaming-with-php/
*
* Takes ($bucket, $key, $streamFunction, $contentType, $contentLength, $filePath [,$acl][,$metadataArray])
*
*
* - [str] $bucket: the bucket into which file will be written
* - [str] $key: key of written file
* - [str] $streamFunction: function to call for data to stream
* - [str] $contentType: file content type
* - [str] $contentLength: file content length
* - [str] $filePath: path of file to be PUT
* - [str] $acl: access control policy of file (OPTIONAL: defaults to 'private')
* - [str] $metadataArray: associative array containing user-defined metadata (name=>value) (OPTIONAL)
*/
function putObjectStream($bucket, $key, $contentType, $contentLength, $filePath, $acl, $metadataArray){
$stream_inst = new Stream();
$stream_inst->data = fopen($filePath, "r");
$streamFunction = array($stream_inst, "stream_function");
sort($metadataArray);
$resource = "$bucket/$key";
$resource = urlencode($resource);
$httpDate = gmdate("D, d M Y G:i:s T");
$curl_inst = curl_init();
curl_setopt ($curl_inst, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_TIME, 180);
curl_setopt ($curl_inst, CURLOPT_NOSIGNAL, 1);
curl_setopt ($curl_inst, CURLOPT_READFUNCTION, $streamFunction);
curl_setopt ($curl_inst, CURLOPT_URL, $this->serviceUrl . $resource);
curl_setopt ($curl_inst, CURLOPT_UPLOAD, true);
curl_setopt ($curl_inst, CURLINFO_CONTENT_LENGTH_UPLOAD, $contentLength);
$header[] = "Date: $httpDate";
$header[] = "Content-Type: $contentType";
$header[] = "Content-Length: $contentLength";
$header[] = "Expect: ";
$header[] = "Transfer-Encoding: ";
$header[] = "x-amz-acl: $acl";
$stringToSign="PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n";
foreach($metadataArray as $current){
if($current!=""){
$stringToSign.="x-amz-meta-$current\n";
$header = substr($current,0,strpos($current,':'));
$meta = substr($current,strpos($current,':')+1,strlen($current));
$header[] = "x-amz-meta-$header: $meta";
}
}
$stringToSign.="/$resource";
$signature = $this->constructSig($stringToSign);
$header[] = "Authorization: AWS $this->accessKeyId:$signature";
curl_setopt($curl_inst, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl_inst, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl_inst);
$this->responseString = $result;
$this->responseCode = curl_getinfo($curl_inst, CURLINFO_HTTP_CODE);
fclose($stream_inst->data);
curl_close($curl_inst);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* deleteObject -- Deletes an object.
*
* Takes ($bucket, $key)
*
* - [str] $bucket: the bucket from which file will be deleted
* - [str] $key: key of file to be deleted
*/
function deleteObject($bucket, $key) {
$httpDate = gmdate("D, d M Y G:i:s T");
$resource = $bucket."/".urlencode($key);
$stringToSign = "DELETE\n\n\n$httpDate\n/$resource";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$resource);
$req->setMethod("DELETE");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 204) {
return true;
} else {
return false;
}
}
/**
* getObjectACL -- gets an objects access control policy.
*
* Takes ($bucket, $key)
*
* - [str] $bucket
* - [str] $key
*/
function getObjectACL($bucket, $key){
$httpDate = gmdate("D, d M Y G:i:s T");
$resource = $bucket."/".urlencode($key);
$stringToSign = "GET\n\n\n$httpDate\n/$resource?acl";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$resource.'?acl');
$req->setMethod("GET");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* setObjectACL -- sets objects access control policy to one of Amazon S3 canned policies.
*
* Takes ($bucket, $key, $acl)
*
* - [str] $bucket
* - [str] $key
* - [str] $acl -- One of canned access control policies.
*/
function setObjectACL($bucket, $key, $acl){
$httpDate = gmdate("D, d M Y G:i:s T");
$resource = $bucket."/".urlencode($key);
$stringToSign = "PUT\n\n\n$httpDate\nx-amz-acl:$acl\n/$resource?acl";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$resource.'?acl');
$req->setMethod("PUT");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->addHeader("x-amz-acl", $acl);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
$this->parsed_xml = simplexml_load_string($this->responseString);
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* getMetadata -- Gets the metadata associated with an object.
*
* Takes ($bucket, $key)
*
* - [str] $bucket
* - [str] $key
*/
function getMetadata($bucket, $key){
$httpDate = gmdate("D, d M Y G:i:s T");
$resource = $bucket."/".urlencode($key);
$stringToSign = "HEAD\n\n\n$httpDate\n/$resource";
$signature = $this->constructSig($stringToSign);
$req =& new HTTP_Request($this->serviceUrl.$resource);
$req->setMethod("HEAD");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->headers = $req->getResponseHeader();
if ($this->responseCode == 200) {
return true;
} else {
return false;
}
}
/**
* getObjectAsString -- Returns object as a string.
*
* Takes ($bucket, $key)
*
* - [str] $bucket
* - [str] $key
*/
function getObjectAsString($bucket, $key) {
$httpDate = gmdate("D, d M Y G:i:s T");
$resource = $bucket."/".urlencode($key);
$stringToSign = "GET\n\n\n{$httpDate}\n/$resource";
$signature = $this->constructSig($stringToSign);
$req = & new HTTP_Request($this->serviceUrl.$resource);
$req->setMethod("GET");
$req->addHeader("Date", $httpDate);
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
$req->sendRequest();
$this->responseCode = $req->getResponseCode();
$this->responseString = $req->getResponseBody();
if ($this->responseCode == 200) {
return true;
} else {
$this->parsed_xml = simplexml_load_string($this->responseString);
return false;
}
}
/**
* queryStringGet -- returns a signed URL to get object
*
* Takes ($bucket, $key, $expires)
*
* - [str] $bucket
* - [str] $key
* - [str] $expires - signed URL with expire after $expires seconds
*/
function queryStringGet($bucket, $key, $expires){
$expires = time() + $expires;
$resource = $bucket."/".urlencode($key);
$stringToSign = "GET\n\n\n$expires\n/$resource";
$signature = urlencode($this->constructSig($stringToSign));
$queryString = "";
return $queryString;
}
function hex2b64($str) {
$raw = '';
for ($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
function constructSig($str) {
$hasher =& new Crypt_HMAC($this->secretKey, "sha1");
$signature = $this->hex2b64($hasher->hash($str));
return($signature);
}
}
class Stream{
var $data;
function stream_function($handle, $fd, $length){
return fread($this->data, $length);
}
} |
Get full code from http://www.sajithmr.com/downloads/s3-test-utility-php.rar


Get full code from http://www.sajithmr.com/downloads/s3-test-utility-php.rar
Hi Sajith , First of all thanks for posting such a good example code . I was searching for a code for S3 storage . Above is very useful to me. Also let me know how can we view the stored images in web page. I tried with but it seems to be not working . Please help me.
Thanks in advance ….
Sandeep,
you can view and manage your amazon s3 account using mozilla’s plugin.
http://www.rjonna.com/ext/s3fox.php
Check and revert me
I tried to install S3 organizer extension to mozilla. But it throws an error which says ‘ difference between request time and current time is too large’. The file upload using putObject method works fine and returns response code 200. I tried with below code
\’
The time difference i think is due to your system time error. Might be your cmos battery is not working or some virus may change your system time.
Thanks and Regards
Sajith.M.R
Thanks for the help, it was very useful for me and its most clear.
When i am trying to put an object into the bucket, your code is saying object is created at the specified location, but when i list then object is not listing.
Could you tell me the reason beyond this. Please….
Hi Sajith,
How do we check the image exist in the amazon s3 server?
Please suggest me to do this.
Save your favourite movie from any tube site using your Flvtube Player :http://smal.ly/flvtubes
Hey Sajith, this was very helpful thanks! Btw, if you are on PHP 5+ you don’t need to use PEAR to get your HMAC on, just use hash_hmac to generate your signature:
$sig = hash_hmac( "sha1", $stringToSign, $secretKey);
Thanks for your post.
link to download the rar file is not working anymore.
http://www.sajithmr.com/downloads/s3-test-utility-php.rar