バケット内のすべてのオブジェクトを一覧表示する
次のコードを実行して、バケット内のすべてのオブジェクトを一覧表示します。
<? 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;
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
while (true) {
try {
$listObjectInfo = $ossClient->listObjects($bucket, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
// Obtain the nextMarker. Obtain the object list from the object next to the last object read by 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 ($nextMarker === '') {
break;
}
}
指定された条件を持つオブジェクトの一覧表示
次のコードを実行して、指定した条件を持つオブジェクトを一覧表示します。
<? 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;
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
$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 は、区切り記号で終わり、同じプレフィックスを持つ一連のオブジェクトを示します。 | いいえ |
prefix | 必須オブジェクトを一覧表示するために構成するプレフィックスを指定します。 | いいえ |
max-keys | 一覧表示できるオブジェクトの最大数を指定します。 既定値は 100 です。 最大値は 1,000 です。 | いいえ |
marker | 一覧表示する最初のオブジェクトを指定します。 | いいえ |