列舉檔案(PHP SDK V1)
本文介紹如何在開啟版本控制狀態下列舉儲存空間下(Bucket)的所有檔案(Object)、指定個數的檔案、指定首碼的檔案等。
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見地區和Endpoint。
本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OssClient。
要列舉檔案,您必須有
oss:ListObjectVersions許可權。具體操作,請參見為RAM使用者授予自訂的權限原則。
列舉Bucket中所有Object的版本資訊
以下代碼用於列舉examplebucket中包括刪除標記(Delete Marker)在內的所有Object的版本資訊。
<?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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$provider = new EnvironmentVariableCredentialsProvider();
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
$endpoint = "yourEndpoint";
// 填寫Bucket名稱,例如examplebucket。
$bucket= "examplebucket";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try{
$option = array(
OssClient::OSS_KEY_MARKER => null,
OssClient::OSS_VERSION_ID_MARKER => null
);
$bool = true;
while ($bool){
$result = $ossClient->listObjectVersions($bucket,$option);
// 查看Object的版本資訊。
foreach ($result->getObjectVersionList() as $key => $info){
printf("key name: {$info->getKey()}\n");
printf("versionid: {$info->getVersionId()}\n");
printf("Is latest: {$info->getIsLatest()}\n\n");
}
// 查看刪除標記的版本資訊。
foreach ($result->getDeleteMarkerList() as $key => $info){
printf("del_maker key name: {$info->getKey()}\n");
printf("del_maker versionid: {$info->getVersionId()}\n");
printf("del_maker Is latest: {$info->getIsLatest()}\n\n");
}
if($result->getIsTruncated() === 'true'){
$option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
$option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
}else{
$bool = false;
}
}
} catch(OssException $e) {
printf($e->getMessage() . "\n");
return;
}列舉指定首碼Object的版本資訊
以下代碼用於列舉examplebucket中以test為首碼Object的版本資訊。
<?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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$provider = new EnvironmentVariableCredentialsProvider();
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
$endpoint = "yourEndpoint";
// 填寫Bucket名稱,例如examplebucket。
$bucket= "examplebucket";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try{
$option = array(
OssClient::OSS_KEY_MARKER => null,
OssClient::OSS_VERSION_ID_MARKER => null,
// 指定列舉以"test"為首碼Object的版本資訊。
OssClient::OSS_PREFIX => "test"
);
$bool = true;
while ($bool){
$result = $ossClient->listObjectVersions($bucket,$option);
// 查看Object的版本資訊。
foreach ($result->getObjectVersionList() as $key => $info){
printf("key name: {$info->getKey()}\n");
printf("versionid: {$info->getVersionId()}\n");
printf("Is latest: {$info->getIsLatest()}\n\n");
}
// 查看刪除標記的版本資訊。
foreach ($result->getDeleteMarkerList() as $key => $info){
printf("del_maker key name: {$info->getKey()}\n");
printf("del_maker versionid: {$info->getVersionId()}\n");
printf("del_maker Is latest: {$info->getIsLatest()}\n");
}
if($result->getIsTruncated() === 'true'){
$option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
$option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
}else{
$bool = false;
}
}
} catch(OssException $e) {
printf($e->getMessage() . "\n");
return;
}列舉指定個數的Object版本資訊
以下代碼用於列舉examplebucket中指定個數的Object版本資訊。
<?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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$provider = new EnvironmentVariableCredentialsProvider();
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
$endpoint = "yourEndpoint";
// 填寫Bucket名稱,例如examplebucket。
$bucket= "examplebucket";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try{
$option = array(
OssClient::OSS_KEY_MARKER => null,
OssClient::OSS_VERSION_ID_MARKER => null,
OssClient::OSS_MAX_KEYS => 200 // 指定最多列舉200個Object版本資訊。
);
$result = $ossClient->listObjectVersions($bucket,$option);
// 查看Object的版本資訊。
foreach ($result->getObjectVersionList() as $key => $info){
printf("key name: ".$info->getKey().PHP_EOL);
printf("versionid: ".$info->getVersionId().PHP_EOL);
printf("Is latest: ".$info->getIsLatest().PHP_EOL.PHP_EOL);
}
// 查看刪除標記的版本資訊。
foreach ($result->getDeleteMarkerList() as $key => $info){
printf("del_maker key name: ".$info->getKey().PHP_EOL);
printf("del_maker versionid: ".$info->getVersionId().PHP_EOL);
printf("del_maker Is latest: ".$info->getIsLatest().PHP_EOL.PHP_EOL);
}
} catch(OssException $e) {
printf($e->getMessage() . "\n");
return;
}檔案夾功能
OSS沒有檔案夾的概念,所有元素都是以檔案來儲存。建立檔案夾本質上來說是建立了一個大小為0並以正斜線(/)結尾的檔案。這個檔案可以被上傳和下載,控制台會對以正斜線(/)結尾的檔案以檔案夾的方式展示。
通過delimiter和prefix兩個參數可以類比檔案夾功能:
如果設定prefix為某個檔案夾名稱,則會列舉以此prefix開頭的檔案,即該檔案夾下所有的檔案和子檔案夾(目錄)均顯示為Object。
如果在設定了prefix的情況下,將delimiter設定為正斜線(/),則只列舉該檔案夾下的檔案和子檔案夾(目錄),該檔案夾下的子檔案夾(目錄)顯示為CommonPrefixes,子檔案夾下的檔案和檔案夾不顯示。
列舉根目錄下Object的版本資訊
以下代碼用於列舉examplebucket中根目錄下Object的版本資訊。
<?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\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 $provider = new EnvironmentVariableCredentialsProvider(); // yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。 $endpoint = "yourEndpoint"; // 填寫Bucket名稱,例如examplebucket。 $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ $option = array( OssClient::OSS_KEY_MARKER => null, OssClient::OSS_VERSION_ID_MARKER => null, OssClient::OSS_DELIMITER => "/", // 指定分隔字元為正斜線(/)。 ); $bool = true; while ($bool){ $result = $ossClient->listObjectVersions($bucket,$option); // 查看Object的版本資訊。 foreach ($result->getObjectVersionList() as $key => $info){ printf("key name: {$info->getKey()}\n"); printf("versionid: {$info->getVersionId()}\n"); printf("Is latest: {$info->getIsLatest()}\n\n"); } // 查看刪除標記的版本資訊。 foreach ($result->getDeleteMarkerList() as $key => $info){ printf("del_maker key name: {$info->getKey()}\n"); printf("del_maker versionid: {$info->getVersionId()}\n"); printf("del_maker Is latest: {$info->getIsLatest()}\n\n"); } if($result->getIsTruncated() === 'true'){ $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker(); $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker(); }else{ $bool = false; } } } catch(OssException $e) { printf($e->getMessage() . "\n"); return; }列舉指定目錄下Object的版本資訊
以下代碼用於列舉examplebucket中test目錄下Object的版本資訊。
<?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\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 $provider = new EnvironmentVariableCredentialsProvider(); // yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。 $endpoint = "yourEndpoint"; // 填寫Bucket名稱,例如examplebucket。 $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ $option = array( OssClient::OSS_KEY_MARKER => null, OssClient::OSS_VERSION_ID_MARKER => null, OssClient::OSS_DELIMITER => "/", // 指定分隔字元為正斜線(/)。 OssClient::OSS_PREFIX => "test/", // 指定列舉test目錄下Object的版本資訊。 ); $bool = true; while ($bool){ $result = $ossClient->listObjectVersions($bucket,$option); // 查看Object的版本資訊。 foreach ($result->getObjectVersionList() as $key => $info){ printf("key name: {$info->getKey()}\n"); printf("versionid: {$info->getVersionId()}\n"); printf("Is latest: {$info->getIsLatest()}\n\n"); } // 查看刪除標記的版本資訊。 foreach ($result->getDeleteMarkerList() as $key => $info){ printf("del_maker key name: {$info->getKey()}\n"); printf("del_maker versionid: {$info->getVersionId()}\n"); printf("del_maker Is latest: {$info->getIsLatest()}\n\n"); } if($result->getIsTruncated() === 'true'){ $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker(); $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker(); }else{ $bool = false; } } } catch(OssException $e) { printf($e->getMessage() . "\n"); return; }