/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public function batchUploadS3($files, $bucket, $bucketPath = '') { // Now we should push these off to S3 // Include the SDK require_once(APPPATH . "/libraries/amazon_sdk/sdk.class.php"); $s3 = new \AmazonS3(); // Prepare to hold the individual filenames // Loop over the list, referring to a single file at a time foreach ($files as $id=>$file) { // Grab only the filename part of the path $filename = $bucketPath.'/'.$fileParts['basename']; // Store the filename for later use $individual_filenames[$id] = $filename; /* Prepare to upload the file to our new S3 bucket. Add this request to a queue that we won't execute quite yet. */ 'fileUpload' => $file, 'acl' => AmazonS3::ACL_PUBLIC )); } /* Execute our queue of batched requests. This may take a few seconds to a few minutes depending on the size of the files and how fast your upload speeds are. */ $file_upload_response = $s3->batch()->send(); /* Since a batch of requests will return multiple responses, let's make sure they ALL came back successfully using `areOK()` (singular responses use `isOK()`). */ if ($file_upload_response->areOK()) { // Loop through the individual filenames foreach ($individual_filenames as $id=>$filename) { /* Display a URL for each of the files we uploaded. Since uploads default to private (you can choose to override this setting when uploading), we'll pre-authenticate the file URL for the next 5 minutes. */ $files[$id] = $s3->get_object_url($bucket, $filename); } }else{ } return $files; }