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;
}