/**
* List contents of a directory.
*
* @param string $directory
* @param bool $recursive
* @return array
*/
public function listContents($directory = '', $recursive = false)
{
$directory = $this->applyPathPrefix($directory);
$bucket = $this->bucket;
$delimiter = '/';
$nextMarker = '';
$maxkeys = 1000;
$options = ['delimiter' => $delimiter, 'prefix' => $directory, 'max-keys' => $maxkeys, 'marker' => $nextMarker];
$listObjectInfo = $this->client->listObjects($bucket, $options);
$objectList = $listObjectInfo->getObjectList();
// 文件列表
$prefixList = $listObjectInfo->getPrefixList();
// 目录列表
$result = [];
foreach ($objectList as $objectInfo) {
if ($objectInfo->getSize() === 0 && $directory === $objectInfo->getKey()) {
$result[] = ['type' => 'dir', 'path' => $this->removePathPrefix(rtrim($objectInfo->getKey(), '/')), 'timestamp' => strtotime($objectInfo->getLastModified())];
continue;
}
$result[] = ['type' => 'file', 'path' => $this->removePathPrefix($objectInfo->getKey()), 'timestamp' => strtotime($objectInfo->getLastModified()), 'size' => $objectInfo->getSize()];
}
foreach ($prefixList as $prefixInfo) {
if ($recursive) {
$next = $this->listContents($this->removePathPrefix($prefixInfo->getPrefix()), $recursive);
$result = array_merge($result, $next);
} else {
$result[] = ['type' => 'dir', 'path' => $this->removePathPrefix(rtrim($prefixInfo->getPrefix(), '/')), 'timestamp' => 0];
}
}
return $result;
}