列举文件
本文介绍如何列举文件。
列举所有文件
以下代码用于列举指定存储空间下的所有文件:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$nextMarker = '';
while (true) {
try {
$options = array(
'delimiter' => '',
'marker' => $nextMarker,
);
$listObjectInfo = $ossClient->listObjects($bucket, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
// 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。
$nextMarker = $listObjectInfo->getNextMarker();
$listObject = $listObjectInfo->getObjectList();
$listPrefix = $listObjectInfo->getPrefixList();
if (!empty($listObject)) {
print("objectList:\n");
foreach ($listObject as $objectInfo) {
print($objectInfo->getKey() . "\n");
}
}
if (!empty($listPrefix)) {
print("prefixList: \n");
foreach ($listPrefix as $prefixInfo) {
print($prefixInfo->getPrefix() . "\n");
}
}
if ($listObjectInfo->getIsTruncated() !== "true") {
break;
}
}
说明 上述示例代码中的$options为可选参数,可以用来列举整个bucket或者某个目录下的所有文件。
列举指定条件的文件
以下代码用于列举指定条件的文件:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$prefix = 'dir/';
$delimiter = '/';
$nextMarker = '';
$maxkeys = 10;
$options = array(
'delimiter' => $delimiter,
'prefix' => $prefix,
'max-keys' => $maxkeys,
'marker' => $nextMarker,
);
try {
$listObjectInfo = $ossClient->listObjects($bucket, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
$objectList = $listObjectInfo->getObjectList(); // object list
$prefixList = $listObjectInfo->getPrefixList(); // directory list
if (!empty($objectList)) {
print("objectList:\n");
foreach ($objectList as $objectInfo) {
print($objectInfo->getKey() . "\n");
}
}
if (!empty($prefixList)) {
print("prefixList: \n");
foreach ($prefixList as $prefixInfo) {
print($prefixInfo->getPrefix() . "\n");
}
}
上述例子中的$options包含的参数如下:
参数 | 说明 | 是否必需 |
---|---|---|
delimiter | 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。 | 否 |
prefix | 本次查询结果的前缀。 | 否 |
max-keys | 列举文件的最大个数。默认为100,最大值为1000。 | 否 |
marker | 标明本次列举文件的起点。 | 否 |
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论