OSS\OssClient::uploadDir PHP Method

uploadDir() public method

上传本地目录内的文件或者目录到指定bucket的指定prefix的object中
public uploadDir ( string $bucket, string $prefix, string $localDirectory, string $exclude = '.|..|.svn|.git', boolean $recursive = false, boolean $checkMd5 = true ) : array
$bucket string bucket名称
$prefix string 需要上传到的object的key前缀,可以理解成bucket中的子目录,结尾不能是'/',接口中会补充'/'
$localDirectory string 需要上传的本地目录
$exclude string 需要排除的目录
$recursive boolean 是否递归的上传localDirectory下的子目录内容
$checkMd5 boolean
return array 返回两个列表 array("succeededList" => array("object"), "failedList" => array("object"=>"errorMessage"))
    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;
    }

Usage Example

/**
 * 按照目录上传文件
 *
 * @param OssClient $ossClient OssClient
 * @param string $bucket 存储空间名称
 *
 */
function uploadDir($ossClient, $bucket)
{
    $localDirectory = ".";
    $prefix = "samples/codes";
    try {
        $ossClient->uploadDir($bucket, $prefix, $localDirectory);
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    printf(__FUNCTION__ . ": completeMultipartUpload OK\n");
}