本SDK提供ListObjectsV2
类用于获取目录文件列表。具体内容详见 官方文档 。
参数说明
Bucket
: 文件所在的存储空间MaxKeys
: 请求返回的最大键数
示例
执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
try {
$s3Client = new S3Client([
'endpoint' => $endpoint,
'region' => $region,
'signature_version' => $signatureVersion,
'force_path_style' => $forcePathStyle,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey,
],
]);
echo "Client initialized successfully!" . PHP_EOL;
} catch (InvalidArgumentException $e) {
die("Failed to initialize Client: " . $e->getMessage());
}
try {
echo "Your bucket contains the following objects:" . PHP_EOL;
$totalCount = 0;
$continuationToken = null;
do {
$params = [
'Bucket' => $bucketName,
'MaxKeys' => $maxKeys ,
'ContinuationToken' => $continuationToken,
];
$result = $s3Client->listObjectsV2($params);
$isTruncated = $result['IsTruncated'];
$continuationToken = $result['NextContinuationToken'] ?? null;
foreach ($result['Contents'] as $object) {
echo $object['Key'] . PHP_EOL;
$totalCount++;
if ($totalCount >= $maxKeys) {
$isTruncated = false;
break;
}
}
} while ($isTruncated && $totalCount < $maxKeys);
} catch (S3Exception $e) {
die("Error: " . $e->getMessage());
}