BaiduBCS::upload_directory PHP Method

upload_directory() public method

将目录中的所有文件进行上传,每个文件为单独object,object命名方式下详: 如有 /home/worker/a/b/c.txt 需上传目录为$dir=/home/worker/a object命令方式为 1. object默认命名方式为 “子目录名 +文件名”,如上述文件c.txt,默认为 '/b/c.txt' 2. 增强命名模式,在$opt中有可选参数进行配置 举例说明 :prefix . has_sub_directory?"/b":"" . '/c.txt'
public upload_directory ( string $bucket, string $dir, $opt = [] ) : array
$bucket string (Required)
$dir string (Required)
return array 数组形式的上传结果 'success' => int 上传成功的文件数目 'skipped' => int 被跳过的文件 'failed' => array() 上传失败的文件
    public function upload_directory($bucket, $dir, $opt = array())
    {
        $this->assertParameterArray($opt);
        if (!is_dir($dir)) {
            throw new BCS_Exception("{$dir} is not a dir!", -1);
        }
        $result = array("success" => 0, "failed" => array(), "skipped" => 0);
        $prefix = "";
        if (isset($opt['prefix'])) {
            $prefix = $opt['prefix'];
        }
        $has_sub_directory = true;
        if (isset($opt['has_sub_directory']) && is_bool($opt['has_sub_directory'])) {
            $has_sub_directory = $opt['has_sub_directory'];
        }
        //获取文件树和构造object名
        $file_tree = self::get_filetree($dir);
        $objects = array();
        foreach ($file_tree as $file) {
            $object = $has_sub_directory == true ? substr($file, strlen($dir)) : "/" . basename($file);
            $objects[$prefix . $object] = $file;
        }
        $objectCount = count($objects);
        $before_upload_log = "Upload directory: bucket[{$bucket}] upload_dir[{$dir}] file_sum[{$objectCount}]";
        if (isset($opt["seek_object_id"])) {
            $before_upload_log .= " seek_object_id[" . $opt["seek_object_id"] . "/{$objectCount}]";
        }
        if (isset($opt["seek_object"])) {
            $before_upload_log .= " seek_object[" . $opt["seek_object"] . "]";
        }
        $this->log($before_upload_log, $opt);
        //查看是否需要查询断点,进行断点续传
        if (isset($opt["seek_object_id"]) && isset($opt["seek_object"])) {
            throw new BCS_Exception("Can not set see_object_id and seek_object at the same time!", -1);
        }
        $num = 1;
        if (isset($opt["seek_object"])) {
            if (isset($objects[$opt["seek_object"]])) {
                foreach ($objects as $object => $file) {
                    if ($object != $opt["seek_object"]) {
                        //当非断点文件,该object已完成上传
                        $this->log("Seeking[" . $opt["seek_object"] . "]. Skip id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
                        //$result ['skipped'] [] = "[$num/$objectCount]  " . $file;
                        $result['skipped']++;
                        unset($objects[$object]);
                    } else {
                        //当找到断点文件,停止循环,从断点文件重新上传
                        //当非断点文件,该object已完成上传
                        $this->log("Found seek id[{$num}/{$objectCount}]object[{$object}]file[{$file}], begin from here.", $opt);
                        break;
                    }
                    $num++;
                }
            } else {
                throw new BCS_Exception("Can not find you seek object, please check!", -1);
            }
        }
        if (isset($opt["seek_object_id"])) {
            if (is_int($opt["seek_object_id"]) && $opt["seek_object_id"] <= $objectCount) {
                foreach ($objects as $object => $file) {
                    if ($num < $opt["seek_object_id"]) {
                        $this->log("Seeking object of [" . $opt["seek_object_id"] . "/{$objectCount}]. Skip  id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
                        //$result ['skipped'] [] = "[$num/$objectCount]  " . $file;
                        $result['skipped']++;
                        unset($objects[$object]);
                    } else {
                        break;
                    }
                    $num++;
                }
            } else {
                throw new BCS_Exception("Param seek_object_id not valid, please check!", -1);
            }
        }
        //上传objects
        $objectCount = count($objects);
        foreach ($objects as $object => $file) {
            $tmp_opt = array_merge($opt);
            if (isset($opt[self::IMPORT_BCS_PRE_FILTER]) && function_exists($opt[self::IMPORT_BCS_PRE_FILTER])) {
                $bolRes = $opt[self::IMPORT_BCS_PRE_FILTER]($bucket, $object, $file, $tmp_opt);
                if ($bolRes !== true) {
                    $this->log("User pre_filter_function return un-true. Skip id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
                    //$result ['skipped'] [] = "id[$num/$objectCount]object[$object]file[$file]";
                    $result['skipped']++;
                    $num++;
                    continue;
                }
            }
            try {
                $response = $this->create_object($bucket, $object, $file, $tmp_opt);
            } catch (Exception $e) {
                $this->log($e->getMessage(), $opt);
                $this->log("Upload Failed id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
                $num++;
                continue;
            }
            if ($response->isOK()) {
                $result["success"]++;
                $this->log("Upload Success id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
            } else {
                $result["failed"][] = "id[{$num}/{$objectCount}]object[{$object}]file[{$file}]";
                $this->log("Upload Failed id[{$num}/{$objectCount}]object[{$object}]file[{$file}].", $opt);
            }
            if (isset($opt[self::IMPORT_BCS_POST_FILTER]) && function_exists($opt[self::IMPORT_BCS_POST_FILTER])) {
                $opt[self::IMPORT_BCS_POST_FILTER]($bucket, $object, $file, $tmp_opt, $response);
            }
            $num++;
        }
        //打印日志并返回结果数组
        $result_str = "\r\n\r\nUpload {$dir} to {$bucket} finished!\r\n";
        $result_str .= "**********************************************************\r\n";
        $result_str .= "**********************Result Summary**********************\r\n";
        $result_str .= "**********************************************************\r\n";
        $result_str .= "Upload directory :  [{$dir}]\r\n";
        $result_str .= "File num :  [{$objectCount}]\r\n";
        $result_str .= "Success: \r\n\tNum: " . $result["success"] . "\r\n";
        $result_str .= "Skipped:\r\n\tNum:" . $result["skipped"] . "\r\n";
        //		foreach ( $result ["skipped"] as $skip ) {
        //			$result_str .= "\t$skip\r\n";
        //		}
        $result_str .= "Failed:\r\n\tNum:" . count($result["failed"]) . "\r\n";
        foreach ($result["failed"] as $fail) {
            $result_str .= "\t{$fail}\r\n";
        }
        if (isset($opt[self::IMPORT_BCS_LOG_METHOD])) {
            $this->log($result_str, $opt);
        } else {
            echo $result_str;
        }
        return $result;
    }

Usage Example

Esempio n. 1
0
    trigger_error("Can not get thread num");
    exit;
}
$thread_num = $argv[1];
$thread_id = $argv[2];
trigger_error("Upload dir thread start, thread_num=[{$thread_num}], thread_id=[{$thread_id}]");
//开始上传
$baidu_bcs = new BaiduBCS($ak, $sk, $host);
$opt = array(BaiduBCS::IMPORT_BCS_PRE_FILTER => 'pre_filter', BaiduBCS::IMPORT_BCS_POST_FILTER => 'post_filter', "prefix" => $prefix, "has_sub_directory" => $has_sub_directory);
if (isset($seek_object_id)) {
    $opt["seek_object_id"] = $seek_object_id;
}
if (isset($seek_object)) {
    $opt["seek_object"] = $seek_object;
}
$baidu_bcs->upload_directory($bucket, $upload_dir, $opt);
/*
 * 可定制化,发送前操作
 * 比如返回 boolean型
 */
function pre_filter($bucket, $object, $file, &$opt)
{
    //文件名的md5 % thread_num 得到的余数若等于thread_id,上传该文件
    global $thread_num, $thread_id;
    $crc32 = crc32($object);
    $mod = ($crc32 > 0 ? $crc32 : -$crc32) % $thread_num;
    if ($mod == $thread_id) {
        return true;
    } else {
        return false;
    }