Example #1
0
 /**
  * 上传目录
  * @param array $options
  * $options = array(
  *      'bucket'    =>  (Required) string
  *      'object'    =>  (Optional) string
  *      'directory' =>  (Required) string
  *      'exclude'   =>  (Optional) string
  *      'recursive' =>  (Optional) string
  *      'checkmd5'  =>  (Optional) boolean
  * )
  * @return bool
  * @throws OSS_Exception
  */
 public function batch_upload_file($options = NULL)
 {
     if (NULL == $options || !isset($options['bucket']) || empty($options['bucket']) || !isset($options['directory']) || empty($options['directory'])) {
         throw new OSS_Exception('Bad Request', 400);
     }
     $is_batch_upload_ok = true;
     $bucket = $this->get_value($options, 'bucket');
     $directory = $this->get_value($options, 'directory');
     //Windows系统下进行转码
     $directory = OSSUtil::encoding_path($directory);
     //判断是否目录
     if (!is_dir($directory)) {
         throw new OSS_Exception($directory . ' is not a directory, please check it');
     }
     $object = $this->get_value($options, 'object', '');
     $exclude = $this->get_value($options, 'exclude', '.|..|.svn', true);
     $recursive = $this->get_value($options, 'recursive', false, true, true);
     //read directory
     $file_list_array = $this->read_dir($directory, $exclude, $recursive);
     if (!$file_list_array) {
         throw new OSS_Exception($directory . ' is empty...');
     }
     $index = 1;
     $is_check_md5 = $this->is_check_md5($options);
     foreach ($file_list_array as $k => $item) {
         echo $index++ . ". ";
         echo "Upload file " . $item['path'] . " ";
         if (is_dir($item['path'])) {
             echo " skipped, because it is directory...\n";
         } else {
             $options = array(self::OSS_FILE_UPLOAD => $item['path'], self::OSS_PART_SIZE => self::OSS_MIN_PART_SIZE, self::OSS_CHECK_MD5 => $is_check_md5);
             $real_object = (!empty($object) ? $object . '/' : '') . $item['file'];
             $response = $this->create_mpu_object($bucket, $real_object, $options);
             if ($response->isOK()) {
                 echo " successful..\n";
             } else {
                 echo " failed..\n";
                 $is_batch_upload_ok = false;
                 continue;
             }
         }
     }
     return $is_batch_upload_ok;
 }