示例#1
0
 /**
  * Upload an array of files to S3
  *
  * @param $files The PHP $_FILES array which references the files to upload
  * @param string $target The relative path in the S3 bucket to upload to
  * @param array $options An array of options to pass to the S3 upload client
  * @return array|bool If ok, will return an array of URLs for the uploaded items
  */
 public function upload($files, $target = '', array $options = array())
 {
     if (!is_array($files) || !empty($files['tmp_name'])) {
         $files = array($files);
     }
     $options = array_merge(array('acl' => AmazonS3::ACL_PUBLIC), $options);
     $individualFiles = array();
     foreach ($files as $k => $file) {
         if (is_array($file)) {
             $filename = basename($file['name']);
             $file = $file['tmp_name'];
         } else {
             $filename = basename($file);
         }
         $individualFiles[] = $filename;
         $options['fileUpload'] = $file;
         $this->s3->batch()->create_object($this->bucket, $target . $filename, $options);
     }
     $response = $this->s3->batch()->send();
     if ($response->areOK()) {
         $data = array();
         foreach ($individualFiles as $filename) {
             $data[] = $this->s3->get_object_url($this->bucket, $target . $filename);
         }
         return $data;
     }
     return false;
 }
 protected function syncToS3($arguments = array(), $options = array())
 {
     list($bucket, $prefix) = explode(':', $arguments['destination']);
     $file_list = sfFinder::type('file')->in($arguments['source']);
     $object_list_response = $this->s3->list_objects($bucket);
     if (!$object_list_response->isOk()) {
         throw new sfException($object_list_response->body->Message);
     }
     if (isset($object_list_response->body->Contents)) {
         foreach ($object_list_response->body->Contents as $object) {
             // var_dump($object->LastModified);
             $object_list[] = $object->Key;
         }
     }
     $files_queued = 0;
     foreach ($file_list as $file) {
         $filename = explode(DIRECTORY_SEPARATOR, $file);
         $filename = array_pop($filename);
         $offset = strpos($file, $arguments['source']);
         $s3_location = substr(str_replace($arguments['source'], '', substr($file, $offset)), 1);
         if (in_array($s3_location, $object_list)) {
             continue;
         }
         $this->s3->batch()->create_object($bucket, $s3_location, array('fileUpload' => $file));
         $files_queued++;
         $this->logSection('file+', $bucket . ':' . $s3_location);
     }
     if ($files_queued <= 0) {
         $this->log('All files have already been synced, no need to upload any files');
         return;
     }
     $upload_response = $this->s3->batch()->send();
     if (!$upload_response->areOk()) {
         throw new sfException($upload_response->body->Message);
     }
     $this->log('Files synced to bucket');
 }
 	defined below. This assumes that you have a directory called "test_files"
 	that actually contains some files you want to upload.
 */
 $list_of_files = filter_file_list(glob('./test_files/*'));
 // Prepare to hold the individual filenames
 $individual_filenames = array();
 // Loop over the list, referring to a single file at a time
 foreach ($list_of_files as $file) {
     // Grab only the filename part of the path
     $filename = explode(DIRECTORY_SEPARATOR, $file);
     $filename = array_pop($filename);
     // Store the filename for later use
     $individual_filenames[] = $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));
 }
 /* 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 $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. */
         echo $s3->get_object_url($bucket, $filename, '5 minutes') . PHP_EOL . PHP_EOL;