Skip to content

Commit

Permalink
修复Storage::files()列出指定目录所有文件不全的问题
Browse files Browse the repository at this point in the history
修复Storage::files()列出指定目录所有文件最多只能拉取前1000个的问题。更正后可以正确列出所有问题。
  • Loading branch information
jacobcyl authored Aug 11, 2016
1 parent c96afc8 commit b562dc1
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions src/AliOssAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,57 +302,67 @@ public function listDirObjects($dirname = '', $recursive = false)
$nextMarker = '';
$maxkeys = 1000;

$options = array(
'delimiter' => $delimiter,
'prefix' => $dirname,
'max-keys' => $maxkeys,
'marker' => $nextMarker,
);

try {
$listObjectInfo = $this->client->listObjects($this->bucket, $options);
} catch (OssException $e) {
$this->logErr(__FUNCTION__, $e);
return false;
}

$objectList = $listObjectInfo->getObjectList(); // 文件列表
$prefixList = $listObjectInfo->getPrefixList(); // 目录列表
//存储结果
$result = [];

if (!empty($objectList)) {
foreach ($objectList as $objectInfo) {

$object['Prefix'] = $dirname;
$object['Key'] = $objectInfo->getKey();
$object['LastModified'] = $objectInfo->getLastModified();
$object['eTag'] = $objectInfo->getETag();
$object['Type'] = $objectInfo->getType();
$object['Size'] = $objectInfo->getSize();
$object['StorageClass'] = $objectInfo->getStorageClass();

$result['objects'][] = $object;
while(true){
$options = [
'delimiter' => $delimiter,
'prefix' => $dirname,
'max-keys' => $maxkeys,
'marker' => $nextMarker,
];

try {
$listObjectInfo = $this->client->listObjects($this->bucket, $options);
} catch (OssException $e) {
$this->logErr(__FUNCTION__, $e);
return false;
}
}else{
$result["objects"] = [];
}

if (!empty($prefixList)) {
foreach ($prefixList as $prefixInfo) {
$result['prefix'][] = $prefixInfo->getPrefix();

$nextMarker = $listObjectInfo->getNextMarker(); // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表
$objectList = $listObjectInfo->getObjectList(); // 文件列表
$prefixList = $listObjectInfo->getPrefixList(); // 目录列表

if (!empty($objectList)) {
foreach ($objectList as $objectInfo) {

$object['Prefix'] = $dirname;
$object['Key'] = $objectInfo->getKey();
$object['LastModified'] = $objectInfo->getLastModified();
$object['eTag'] = $objectInfo->getETag();
$object['Type'] = $objectInfo->getType();
$object['Size'] = $objectInfo->getSize();
$object['StorageClass'] = $objectInfo->getStorageClass();

$result['objects'][] = $object;
}
}else{
$result["objects"] = [];
}
}else{
$result['prefix'] = [];
}

if($recursive){

foreach( $result['prefix'] as $pfix){
$next = $this->listDirObjects($pfix , $recursive);
$result["objects"] = array_merge($result['objects'], $next["objects"]);

if (!empty($prefixList)) {
foreach ($prefixList as $prefixInfo) {
$result['prefix'][] = $prefixInfo->getPrefix();
}
}else{
$result['prefix'] = [];
}

//递归查询子目录所有文件
if($recursive){
foreach( $result['prefix'] as $pfix){
$next = $this->listDirObjects($pfix , $recursive);
$result["objects"] = array_merge($result['objects'], $next["objects"]);
}
}

//没有更多结果了
if ($nextMarker === '') {
break;
}

}

return $result;
}

Expand Down Expand Up @@ -630,4 +640,4 @@ protected function logErr($fun, $e){
Log::error($e->getMessage());
}
}
}
}

0 comments on commit b562dc1

Please sign in to comment.