This topic describes how to list all objects, a specific number of objects, and objects whose names contain a specific prefix in an Object Storage Service (OSS) bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To list objects, you must have the
oss:ListObjectspermission. For more information, see Attach a custom policy to a RAM user.
List objects
You can call the listObjects or listObjectsV2 method to list objects in the root directory of a specified bucket. Subdirectories and objects in the subdirectories are not listed.
Use the listObjects method
The following code shows how to call the listObjects method to list objects in the root directory of a bucket named examplebucket. Subdirectories and objects in the subdirectories are not listed. By default, 100 objects are returned.
<?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; }The listObjectsV2 method
The following code shows how to use the listObjectsV2 method to list objects in the root directory of a bucket named examplebucket. This operation does not list subdirectories or the objects within them. By default, up to 100 objects are returned.
<?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; }
List a specified number of objects
You can call the listObjects or listObjectsV2 method to list a specified number of objects in a bucket.
Use the listObjects method
The following code shows how to call the listObjects method to list 200 objects in a bucket named examplebucket.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list 200 objects in a bucket named examplebucket.
<?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"); } }
List objects with a specified prefix
You can call the listObjects or listObjectsV2 method to list objects with a specified prefix in a bucket.
Use the listObjects method
The following code shows how to call the listObjects method to list objects with the prefix `dir` in a bucket named examplebucket.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list objects with the prefix `dir` in a bucket named examplebucket.
<?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"); } }
List objects alphabetically after a specified object name
You can call the listObjects or listObjectsV2 method to list objects that are alphabetically after a specified object name.
Use the listObjects method
The following code shows how to call the listObjects method to list objects that are alphabetically after `test.txt` in a bucket named examplebucket.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list objects that are alphabetically after `test.txt` in a bucket named examplebucket.
<?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"); } }
List all objects by page
You can call the listObjects or listObjectsV2 method to list all objects in a paginated manner. Use the maxKeys parameter to specify the number of objects to return on each page.
Use the listObjects method
The following code shows how to call the listObjects method to list all objects in a bucket named examplebucket in a paginated manner.
<?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');Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list all objects in a bucket named examplebucket in a paginated manner.
<?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');
List objects with a specified prefix by page
You can call the listObjects or listObjectsV2 method to list objects with a specified prefix in a paginated manner.
Use the listObjects method
The following code shows how to call the listObjects method to list objects with the prefix `dir` in a bucket named examplebucket in a paginated manner.
<?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');Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list objects with the prefix `dir` in a bucket named examplebucket in a paginated manner.
<?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');
Specify the encoding of object names
If an object name contains special characters, you must encode the object name for transmission. OSS supports only URL encoding.
Single quotation marks (')
Double quotation marks (")
Ampersands (&)
Angle brackets (< >)
Enumeration commas (、)
Chinese characters
You can call the listObjects or listObjectsV2 method to specify the encoding of object names.
Use the listObjects method
The following code shows how to call the listObjects method to specify the encoding of object names.
<?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');Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to specify the encoding of object names.
<?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');
Simulate directories
OSS uses a flat structure where all data is stored as objects. To simulate a directory, you can create a 0-byte object whose name ends with a forward slash (/). You can upload and download this object. The OSS console displays objects whose names end with a forward slash (/) as directories.
You can specify the delimiter and prefix parameters to list objects by directory.
If you set prefix to a directory name in the request, the objects and subdirectories whose names contain the prefix are listed.
If you specify a prefix and set delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in CommonPrefixes. The objects and directories in these subdirectories are not listed.
For example, a bucket contains the following objects: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is specified as the directory delimiter. The following examples describe how to list objects in simulated directories.
List all objects in a bucket
You can call the listObjects or listObjectsV2 method to list all objects in a specified bucket.
Use the listObjects method
The following code shows how to call the listObjects method to list all objects in a bucket named examplebucket.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list all objects in a bucket named examplebucket.
<?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"); } }Response
The following response is returned when you call the preceding methods to list all objects in the examplebucket bucket.
objectList: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg
List all objects in a specified directory
You can call the listObjects or listObjectsV2 method to list all objects in the fun/ directory.
Use the listObjects method
The following code shows how to call the listObjects method to list all objects in the fun/ directory.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list all objects in the fun/ directory.
<?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"); } }Results
The following response is returned when you call the preceding methods to list all objects in the fun/ directory.
objectList: fun/movie/001.avi fun/movie/007.avi fun/test.jpg
List objects and subdirectories in a directory
You can call the listObjects or listObjectsV2 method to list objects and subdirectories in the fun/ directory.
Use the listObjects method
The following code shows how to call the listObjects method to list objects and subdirectories in the fun/ directory.
<?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"); } }Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to list objects and subdirectories in the fun/ directory.
<?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"); } }Return value
The following response is returned when you call the preceding methods to list objects and subdirectories in the fun/ directory.
objectList: fun/test.jpg prefixList: fun/movie/
Obtain the size of objects in a specified directory
You can call the listObjects or listObjectsV2 method to obtain the size of objects in the fun/ directory.
Use the listObjects method
The following code shows how to call the listObjects method to obtain the size of objects in the fun/ directory.
<?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);Use the listObjectsV2 method
The following code shows how to call the listObjectsV2 method to obtain the size of objects in the fun/ directory.
<?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);Return value
The following response is returned when you call the preceding methods to obtain the object sizes.
object namefun/movie/001.avi:0.01953125KB object namefun/movie/007.avi:290.71875KB object namefun/test.jpg:144.216796875KB fun/:434.955078125KB
References
For the complete sample code that shows how to list objects, see the GitHub sample.
For more information about the API operations that you can call to list objects, see GetBucket (ListObjects) and ListObjectsV2 (GetBucketV2).