Posted By

mpiccinato on 07/02/11


Tagged

amazon aws codeigniter


Versions (?)

Codeigniter and S3


 / Published in: PHP
 

  1. public function batchUploadS3($files, $bucket, $bucketPath = '')
  2. {
  3. // Now we should push these off to S3
  4. // Include the SDK
  5. require_once(APPPATH . "/libraries/amazon_sdk/sdk.class.php");
  6.  
  7. $s3 = new \AmazonS3();
  8.  
  9. // Prepare to hold the individual filenames
  10. $individual_filenames = array();
  11.  
  12. // Loop over the list, referring to a single file at a time
  13. foreach ($files as $id=>$file)
  14. {
  15. // Grab only the filename part of the path
  16. $fileParts = pathinfo($file);
  17. $filename = $bucketPath.'/'.$fileParts['basename'];
  18.  
  19. // Store the filename for later use
  20. $individual_filenames[$id] = $filename;
  21.  
  22. /* Prepare to upload the file to our new S3 bucket. Add this
  23.   request to a queue that we won't execute quite yet. */
  24. $s3->batch()->create_object($bucket, $filename, array(
  25. 'fileUpload' => $file,
  26. 'acl' => AmazonS3::ACL_PUBLIC
  27. ));
  28. }
  29.  
  30. /* Execute our queue of batched requests. This may take a few seconds to a
  31.   few minutes depending on the size of the files and how fast your upload
  32.   speeds are. */
  33. $file_upload_response = $s3->batch()->send();
  34.  
  35. /* Since a batch of requests will return multiple responses, let's
  36.   make sure they ALL came back successfully using `areOK()` (singular
  37.   responses use `isOK()`). */
  38. if ($file_upload_response->areOK())
  39. {
  40. // Loop through the individual filenames
  41. foreach ($individual_filenames as $id=>$filename)
  42. {
  43. /* Display a URL for each of the files we uploaded. Since uploads default to
  44.   private (you can choose to override this setting when uploading), we'll
  45.   pre-authenticate the file URL for the next 5 minutes. */
  46. $files[$id] = $s3->get_object_url($bucket, $filename);
  47. }
  48. }else{
  49. error_log("batchUploadS3 failed");
  50. }
  51.  
  52. return $files;
  53. }

Report this snippet  

You need to login to post a comment.