Return to Snippet

Revision: 48479
at July 2, 2011 09:03 by mpiccinato


Initial Code
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
        $individual_filenames = array();

        // 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
            $fileParts = pathinfo($file);
            $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. */
            $s3->batch()->create_object($bucket, $filename, array(
                                                            '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{
            error_log("batchUploadS3 failed");
        }   
    
        return $files;
    }

Initial URL


Initial Description


Initial Title
Codeigniter and S3

Initial Tags
codeigniter

Initial Language
PHP