Example #1
0
 /**
  * 上传本地目录内的文件或者目录到指定bucket的指定prefix的object中
  *
  * @param string $bucket bucket名称
  * @param string $prefix 需要上传到的object的key前缀,可以理解成bucket中的子目录,结尾不能是'/',接口中会补充'/'
  * @param string $localDirectory 需要上传的本地目录
  * @param string $exclude 需要排除的目录
  * @param bool $recursive 是否递归的上传localDirectory下的子目录内容
  * @param bool $checkMd5
  * @return array 返回两个列表 array("succeededList" => array("object"), "failedList" => array("object"=>"errorMessage"))
  * @throws OssException
  */
 public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.svn|.git', $recursive = false, $checkMd5 = true)
 {
     $retArray = array("succeededList" => array(), "failedList" => array());
     if (empty($bucket)) {
         throw new OssException("parameter error, bucket is empty");
     }
     if (!is_string($prefix)) {
         throw new OssException("parameter error, prefix is not string");
     }
     if (empty($localDirectory)) {
         throw new OssException("parameter error, localDirectory is empty");
     }
     $directory = $localDirectory;
     $directory = OssUtil::encodePath($directory);
     //判断是否目录
     if (!is_dir($directory)) {
         throw new OssException('parameter error: ' . $directory . ' is not a directory, please check it');
     }
     //read directory
     $file_list_array = OssUtil::readDir($directory, $exclude, $recursive);
     if (!$file_list_array) {
         throw new OssException($directory . ' is empty...');
     }
     foreach ($file_list_array as $k => $item) {
         if (is_dir($item['path'])) {
             continue;
         }
         $options = array(self::OSS_PART_SIZE => self::OSS_MIN_PART_SIZE, self::OSS_CHECK_MD5 => $checkMd5);
         $realObject = (!empty($prefix) ? $prefix . '/' : '') . $item['file'];
         try {
             $this->multiuploadFile($bucket, $realObject, $item['path'], $options);
             $retArray["succeededList"][] = $realObject;
         } catch (OssException $e) {
             $retArray["failedList"][$realObject] = $e->getMessage();
         }
     }
     return $retArray;
 }