public listObjects ( string $bucket, array $options = NULL ) : OSS\Model\ObjectListInfo | ||
$bucket | string | |
$options | array | 其中options中的参数如下 $options = array( 'max-keys' => max-keys用于限定此次返回object的最大数,如果不设定,默认为100,max-keys取值不能大于1000。 'prefix' => 限定返回的object key必须以prefix作为前缀。注意使用prefix查询时,返回的key中仍会包含prefix。 'delimiter' => 是一个用于对Object名字进行分组的字符。所有名字包含指定的前缀且第一次出现delimiter字符之间的object作为一组元素 'marker' => 用户设定结果从marker之后按字母排序的第一个开始返回。 ) 其中 prefix,marker用来实现分页显示效果,参数的长度必须小于256字节。 |
리턴 | OSS\Model\ObjectListInfo |
public function listObjects($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_HEADERS] = array(self::OSS_DELIMITER => isset($options[self::OSS_DELIMITER]) ? $options[self::OSS_DELIMITER] : '/', self::OSS_PREFIX => isset($options[self::OSS_PREFIX]) ? $options[self::OSS_PREFIX] : '', self::OSS_MAX_KEYS => isset($options[self::OSS_MAX_KEYS]) ? $options[self::OSS_MAX_KEYS] : self::OSS_MAX_KEYS_VALUE, self::OSS_MARKER => isset($options[self::OSS_MARKER]) ? $options[self::OSS_MARKER] : '');
$query = isset($options[self::OSS_QUERY_STRING]) ? $options[self::OSS_QUERY_STRING] : array();
$options[self::OSS_QUERY_STRING] = array_merge($query, array(self::OSS_ENCODING_TYPE => self::OSS_ENCODING_TYPE_URL));
$response = $this->auth($options);
$result = new ListObjectsResult($response);
return $result->getData();
}
/** * 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; }