Bu konu, Object Storage Service (OSS) paketindeki tüm nesnelerin, belirli sayıda nesnenin ve adı belirli bir önek içeren nesnelerin nasıl listeleneceğini açıklar.
Kullanım notları
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç noktayı kullanın. Desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konu, bir OSS uç noktası kullanarak OssClient örneğinin nasıl oluşturulacağını gösterir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. OssClient oluşturma.
Nesneleri listelemek için
oss:ListObjectsiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. RAM kullanıcısına özel ilke ekleme.
Nesneleri listeleme
Belirtilen bir paketin kök dizinindeki nesneleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz. Alt dizinler ve alt dizinlerdeki nesneler listelenmez.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir paketin kök dizinindeki nesneleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir. Alt dizinler ve alt dizinlerdeki nesneler listelenmez. Varsayılan olarak 100 nesne döndürülür.
<?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\Core\OssException; try { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $listObjectInfo = $ossClient->listObjects($bucket); printf("Bucket Name: %s". "\n",$listObjectInfo->getBucketName()); printf("Prefix: %s". "\n",$listObjectInfo->getPrefix()); printf("Marker: %s". "\n",$listObjectInfo->getMarker()); printf("Next Marker: %s". "\n",$listObjectInfo->getNextMarker()); printf("Max Keys: %s". "\n",$listObjectInfo->getMaxKeys()); printf("Delimiter: %s". "\n",$listObjectInfo->getDelimiter()); printf("Is Truncated: %s". "\n",$listObjectInfo->getIsTruncated()); $objectList = $listObjectInfo->getObjectList(); $prefixList = $listObjectInfo->getPrefixList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { printf("Object Name: %s". "\n",$objectInfo->getKey()); printf("Object Size: %s". "\n",$objectInfo->getSize()); printf("Object Type: %s". "\n",$objectInfo->getType()); printf("Object ETag: %s". "\n",$objectInfo->getETag()); printf("Object Last Modified: %s". "\n",$objectInfo->getLastModified()); printf("Object Storage Class: %s". "\n",$objectInfo->getStorageClass()); if ($objectInfo->getRestoreInfo()){ printf("Restore Info: %s". "\n",$objectInfo->getRestoreInfo() ); } if($objectInfo->getOwner()){ printf("Owner Id:".$objectInfo->getOwner()->getId() . "\n"); printf("Owner Name:".$objectInfo->getOwner()->getDisplayName() . "\n"); } } } if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { printf("Common Prefix:%s\n",$prefixInfo->getPrefix()); } } } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } -
listObjectsV2 yöntemi
Aşağıdaki kod, examplebucket adlı bir paketin kök dizinindeki nesneleri listelemek için listObjectsV2 yönteminin nasıl kullanılacağını gösterir. Bu işlem alt dizinleri veya bunların içindeki nesneleri listelemez. Varsayılan olarak en fazla 100 nesne döndürülür.
<?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\Core\OssException; try { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $listObjectInfo = $ossClient->listObjectsV2($bucket); printf("Bucket Name: %s". "\n",$listObjectInfo->getBucketName()); printf("Prefix: %s". "\n",$listObjectInfo->getPrefix()); printf("Next Continuation Token: %s". "\n",$listObjectInfo->getNextContinuationToken()); printf("Continuation Token: %s". "\n",$listObjectInfo->getContinuationToken()); printf("Max Keys: %s". "\n",$listObjectInfo->getMaxKeys()); printf("Key Count: %s". "\n",$listObjectInfo->getKeyCount()); printf("Delimiter: %s". "\n",$listObjectInfo->getDelimiter()); printf("Is Truncated: %s". "\n",$listObjectInfo->getIsTruncated()); printf("Start After: %s". "\n",$listObjectInfo->getStartAfter()); $objectList = $listObjectInfo->getObjectList(); $prefixList = $listObjectInfo->getPrefixList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { printf("Object Name: %s". "\n",$objectInfo->getKey()); printf("Object Size: %s". "\n",$objectInfo->getSize()); printf("Object Type: %s". "\n",$objectInfo->getType()); printf("Object ETag: %s". "\n",$objectInfo->getETag()); printf("Object Last Modified: %s". "\n",$objectInfo->getLastModified()); printf("Object Storage Class: %s". "\n",$objectInfo->getStorageClass()); if ($objectInfo->getRestoreInfo()){ printf("Restore Info: %s". "\n",$objectInfo->getRestoreInfo() ); } if($objectInfo->getOwner()){ printf("Owner Id:".$objectInfo->getOwner()->getId() . "\n"); printf("Owner Name:".$objectInfo->getOwner()->getDisplayName() . "\n"); } } } if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { printf("Common Prefix:%s\n",$prefixInfo->getPrefix()); } } } catch (OssException $e) { printf($e->getMessage() . "\n"); return; }
Belirtilen sayıda nesneyi listeleme
Bir paketteki belirtilen sayıda nesneyi listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette 200 nesneyi listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the maximum number of objects to return to 200. $maxkeys = 200; $options = array( 'max-keys' => $maxkeys, ); try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette 200 nesneyi listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the maximum number of objects to return to 200. $maxkeys = 200; $options = array( 'max-keys' => $maxkeys, ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } }
Belirli bir öneke sahip nesneleri listeleme
Bir paketteki belirli bir öneke sahip nesneleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
dirönekine sahip nesneleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the prefix to dir/. $prefix = 'dir/'; $options = array( 'prefix' => $prefix, ); try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
dirönekine sahip nesneleri listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the prefix to dir/. $prefix = 'dir/'; $options = array( 'prefix' => $prefix, ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } }
Belirtilen bir nesne adından sonra alfabetik sırayla nesneleri listeleme
Belirtilen bir nesne adından sonra alfabetik sırayla gelen nesneleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
test.txtöğesinden sonra alfabetik sırayla gelen nesneleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set marker to test.txt. This parameter specifies that objects are returned in alphabetical order starting from the object specified by marker. $marker = "test.txt"; $options = array( OssClient::OSS_MARKER=>$marker ); try { $listObjectInfo = $ossClient->listObjects($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
test.txtöğesinden sonra alfabetik sırayla gelen nesneleri listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set startAfter to test.txt. This parameter specifies that objects are returned in alphabetical order starting from the object specified by startAfter. $startAfter = "test.txt"; $options = array( OssClient::OSS_START_AFTER=>$startAfter ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket, $options); } catch (OssException $e) { printf($e->getMessage() . "\n"); return; } $objectList = $listObjectInfo->getObjectList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } }
Tüm nesneleri sayfalayarak listeleme
Tüm nesneleri sayfalayarak listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz. Her sayfada döndürülecek nesne sayısını belirtmek için maxKeys parametresini kullanın.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir paketteki tüm nesneleri sayfalayarak listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( // Set the number of objects to return on each page to 200. OssClient::OSS_MAX_KEYS=>200 ); do{ $result = $ossClient->listObjects($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } $options[OssClient::OSS_MARKER] = $result->getNextMarker(); }while($result->getIsTruncated() === 'true'); -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir paketteki tüm nesneleri sayfalayarak listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( // Set the number of objects to return on each page to 200. OssClient::OSS_MAX_KEYS=>200 ); do{ $result = $ossClient->listObjectsV2($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken(); }while($result->getIsTruncated() === 'true');
Belirli bir öneke sahip nesneleri sayfalayarak listeleme
Belirli bir öneke sahip nesneleri sayfalayarak listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
dirönekine sahip nesneleri sayfalayarak listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the number of objects to return on each page to 100. $maxKeys = 100; // Specify the prefix. Example: dir/. $prefix = 'dir/'; $options = array( OssClient::OSS_MAX_KEYS =>$maxKeys, OssClient::OSS_PREFIX =>$prefix ); do{ $result = $ossClient->listObjects($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } $options[OssClient::OSS_MARKER] = $result->getNextMarker(); }while($result->getIsTruncated() === 'true'); -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir pakette
dirönekine sahip nesneleri sayfalayarak listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the number of objects to return on each page to 100. $maxKeys = 100; // Specify the prefix. Example: dir/. $prefix = 'dir/'; $options = array( OssClient::OSS_MAX_KEYS =>$maxKeys, OssClient::OSS_PREFIX =>$prefix ); do{ $result = $ossClient->listObjectsV2($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken(); }while($result->getIsTruncated() === 'true');
Nesne adlarının kodlamasını belirtme
Bir nesne adı özel karakterler içeriyorsa, iletim için nesne adını kodlamanız gerekir. OSS yalnızca URL kodlamasını destekler.
Tek tırnak işaretleri (')
Çift tırnak işaretleri (")
Ve işaretleri (&)
Açı parantezleri (< >)
Virgül ayraçları (、)
Çince karakterler
Nesne adlarının kodlamasını belirtmek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, nesne adlarının kodlamasını belirtmek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( // Set the encoding type of object names to URL. OssClient::OSS_ENCODING_TYPE=>'url', // Set the number of objects to return on each page to 20. OssClient::OSS_MAX_KEYS=>20 ); do{ $result = $ossClient->listObjects($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } print("prefix:\n"); $prefixList = $result->getPrefixList(); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } if($result->getDelimiter() != null){ printf("delimiter:".$result->getDelimiter().PHP_EOL); } if($result->getMarker() != null){ printf("marker:".$result->getMarker().PHP_EOL); } $options[OssClient::OSS_MARKER] = $result->getNextMarker(); }while($result->getIsTruncated() === 'true'); -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, nesne adlarının kodlamasını belirtmek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( // Set the encoding type of object names to URL. OssClient::OSS_ENCODING_TYPE=>'url', // Set the number of objects to return on each page to 20. OssClient::OSS_MAX_KEYS=>20 ); do{ $result = $ossClient->listObjectsV2($bucket,$options); $objectList = $result->getObjectList(); print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } print("prefix:\n"); $prefixList = $result->getPrefixList(); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } if($result->getDelimiter() != null){ printf("delimiter:".$result->getDelimiter().PHP_EOL); } if($result->getStartAfter() != null){ printf("start after:".$result->getStartAfter().PHP_EOL); } $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken(); }while($result->getIsTruncated() === 'true');
Dizinleri simüle etme
OSS, tüm verilerin nesne olarak depolandığı düz bir yapı kullanır. Bir dizini simüle etmek için adı eğik çizgi (/) ile biten 0 baytlık bir nesne oluşturabilirsiniz. Bu nesneyi yükleyebilir ve indirebilirsiniz. OSS konsolu, adı eğik çizgi (/) ile biten nesneleri dizin olarak görüntüler.
Dizine göre nesneleri listelemek için delimiter ve prefix parametrelerini belirtebilirsiniz.
İstekte prefix değerini bir dizin adına ayarlarsanız, adı bu öneki içeren nesneler ve alt dizinler listelenir.
İstekte bir önek belirtip delimiter değerini eğik çizgi (/) olarak ayarlarsanız, dizindeki adı belirtilen önekle başlayan nesneler ve alt dizinler listelenir. Her alt dizin, CommonPrefixes içinde tek bir sonuç öğesi olarak listelenir. Bu alt dizinlerdeki nesneler ve dizinler listelenmez.
Örneğin, bir paket aşağıdaki nesneleri içerir: oss.jpg, fun/test.jpg, fun/movie/001.avi ve fun/movie/007.avi. Eğik çizgi (/) dizin sınırlayıcı olarak belirtilmiştir. Aşağıdaki örneklerde, simüle edilmiş dizinlerdeki nesnelerin nasıl listeleneceği açıklanmaktadır.
Bir paketteki tüm nesneleri listeleme
Belirtilen bir paketteki tüm nesneleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir paketteki tüm nesneleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( 'delimiter' => '', ); 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(); $prefixList = $listObjectInfo->getPrefixList(); 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"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, examplebucket adlı bir paketteki tüm nesneleri listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); $options = array( 'delimiter' => '', ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket,$options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n"); $objectList = $listObjectInfo->getObjectList(); $prefixList = $listObjectInfo->getPrefixList(); 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"); } } -
Yanıt
Yukarıdaki yöntemleri çağırarak examplebucket paketindeki tüm nesneleri listelediğinizde aşağıdaki yanıt döndürülür.
objectList: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg
Belirtilen bir dizindeki tüm nesneleri listeleme
fun/ dizinindeki tüm nesneleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki tüm nesneleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory to fun/. $prefix = 'fun/'; $options = array( 'prefix' => $prefix, 'delimiter' => '', ); 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(); $prefixList = $listObjectInfo->getPrefixList(); 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"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki tüm nesneleri listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory name to fun/. $prefix = 'fun/'; $options = array( 'prefix' => $prefix, 'delimiter' => '', ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n"); $objectList = $listObjectInfo->getObjectList(); $prefixList = $listObjectInfo->getPrefixList(); 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"); } } -
Sonuçlar
Yukarıdaki yöntemleri çağırarak fun/ dizinindeki tüm nesneleri listelediğinizde aşağıdaki yanıt döndürülür.
objectList: fun/movie/001.avi fun/movie/007.avi fun/test.jpg
Bir dizindeki nesneleri ve alt dizinleri listeleme
fun/ dizinindeki nesneleri ve alt dizinleri listelemek için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki nesneleri ve alt dizinleri listelemek için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory name to fun/. $prefix = 'fun/'; $delimiter = '/'; $options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, ); 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(); $prefixList = $listObjectInfo->getPrefixList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } } // The commonPrefixs list contains all subdirectories in the fun/ directory. if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki nesneleri ve alt dizinleri listelemek için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory name to fun/. $prefix = 'fun/'; $delimiter = '/'; $options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, ); try { $listObjectInfo = $ossClient->listObjectsV2($bucket, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n"); $objectList = $listObjectInfo->getObjectList(); $prefixList = $listObjectInfo->getPrefixList(); if (!empty($objectList)) { print("objectList:\n"); foreach ($objectList as $objectInfo) { print($objectInfo->getKey() . "\n"); } } // The commonPrefixs list contains all subdirectories in the fun/ directory. if (!empty($prefixList)) { print("prefixList: \n"); foreach ($prefixList as $prefixInfo) { print($prefixInfo->getPrefix() . "\n"); } } -
Dönüş değeri
Yukarıdaki yöntemleri çağırarak fun/ dizinindeki nesneleri ve alt dizinleri listelediğinizde aşağıdaki yanıt döndürülür.
objectList: fun/test.jpg prefixList: fun/movie/
Belirtilen bir dizindeki nesnelerin boyutunu alma
fun/ dizinindeki nesnelerin boyutunu almak için listObjects veya listObjectsV2 yöntemini çağırabilirsiniz.
-
listObjects yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki nesnelerin boyutunu almak için listObjects yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory name to fun/. $prefix = 'fun/'; $delimiter = ''; $nextMarker = ''; $maxkeys = 1000; $options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $nextMarker, ); $bool = true; $size = 0; while ($bool){ $result = $ossClient->listObjects($bucket,$options); foreach ($result->getObjectList() as $objInfo){ printf("object name".$objInfo->getKey().":" . ($objInfo->getSize() / 1024) . "KB".PHP_EOL); $size+=$objInfo->getSize(); } if($result->getIsTruncated() === 'true'){ $options['marker'] = $result->getNextMarker(); }else{ $bool = false; } } printf($prefix.":" . ($size / 1024) . "KB".PHP_EOL); -
listObjectsV2 yöntemi kullanma
Aşağıdaki kod, fun/ dizinindeki nesnelerin boyutunu almak için listObjectsV2 yönteminin nasıl çağrılacağını gösterir.
<?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'; } require_once __DIR__ . '/Common.php'; use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the bucket name. Example: examplebucket. $bucket= "examplebucket"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the directory name to fun/. $prefix = 'fun/'; $delimiter = ''; $nextMarker = ''; $maxkeys = 1000; $options = array( 'delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, ); $bool = true; $size = 0; while ($bool){ $result = $ossClient->listObjectsV2($bucket,$options); foreach ($result->getObjectList() as $objInfo){ printf("object name".$objInfo->getKey().":" . ($objInfo->getSize() / 1024) . "KB".PHP_EOL); $size+=$objInfo->getSize(); } if($result->getIsTruncated() === 'true'){ $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken(); }else{ $bool = false; } } printf($prefix.":" . ($size / 1024) . "KB".PHP_EOL); -
Dönüş değeri
Nesne boyutlarını almak için yukarıdaki yöntemleri çağırdığınızda aşağıdaki yanıt döndürülür.
object namefun/movie/001.avi:0.01953125KB object namefun/movie/007.avi:290.71875KB object namefun/test.jpg:144.216796875KB fun/:434.955078125KB
İlgili bilgiler
Nesnelerin nasıl listeleneceğini gösteren tam örnek kod için GitHub örneğine
Nesneleri listelemek için çağırabileceğiniz API işlemleri hakkında daha fazla bilgi için bkz. GetBucket (ListObjects) ve ListObjectsV2 (GetBucketV2).