Tous les produits
Search
Centre de documentation

Object Storage Service:List objects (PHP SDK V1)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment répertorier tous les objets, un nombre spécifique d'objets et les objets dont les noms contiennent un préfixe donné dans un compartiment Object Storage Service (OSS).

Notes d'utilisation

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud dans la même région, utilisez un point de terminaison interne. Pour plus de détails sur les régions et points de terminaison pris en charge, consultez Régions et points de terminaison.

  • Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer un OssClient.

  • Pour répertorier les objets, vous devez disposer de l'autorisation oss:ListObjects. Pour plus d'informations, consultez Attacher une politique personnalisée à un utilisateur RAM.

Répertorier les objets

Appelez la méthode listObjects ou listObjectsV2 pour répertorier les objets situés dans le répertoire racine d'un compartiment spécifié. Les sous-répertoires et les objets qu'ils contiennent ne sont pas répertoriés.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier les objets situés dans le répertoire racine d'un compartiment nommé examplebucket. Les sous-répertoires et les objets qu'ils contiennent ne sont pas répertoriés. Par défaut, 100 objets sont renvoyés.

    <?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;
    }
    
  • La méthode listObjectsV2

    Le code suivant montre comment utiliser la méthode listObjectsV2 pour répertorier les objets situés dans le répertoire racine d'un compartiment nommé examplebucket. Cette opération ne répertorie pas les sous-répertoires ni les objets qu'ils contiennent. Par défaut, jusqu'à 100 objets sont renvoyés.

    <?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;
    }
    

Répertorier un nombre spécifique d'objets

Appelez la méthode listObjects ou listObjectsV2 pour répertorier un nombre défini d'objets dans un compartiment.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier 200 objets dans un compartiment nommé 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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier 200 objets dans un compartiment nommé 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");
      }
    }

Répertorier les objets avec un préfixe donné

Appelez la méthode listObjects ou listObjectsV2 pour répertorier les objets portant un préfixe spécifique dans un compartiment.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier les objets dont le préfixe est dir dans un compartiment nommé 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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier les objets dont le préfixe est dir dans un compartiment nommé 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");
      }
    }

Répertorier les objets par ordre alphabétique après un nom d'objet donné

Appelez la méthode listObjects ou listObjectsV2 pour répertorier les objets qui viennent alphabétiquement après un nom d'objet spécifié.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier les objets venant alphabétiquement après test.txt dans un compartiment nommé 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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier les objets venant alphabétiquement après test.txt dans un compartiment nommé 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");
      }
    }

Répertorier tous les objets par page

Appelez la méthode listObjects ou listObjectsV2 pour répertorier tous les objets de manière paginée. Utilisez le paramètre maxKeys pour définir le nombre d'objets à renvoyer par page.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier tous les objets d'un compartiment nommé examplebucket de manière paginée.

    <?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');
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier tous les objets d'un compartiment nommé examplebucket de manière paginée.

    <?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');

Répertorier par page les objets avec un préfixe donné

Appelez la méthode listObjects ou listObjectsV2 pour répertorier de manière paginée les objets portant un préfixe spécifique.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier de manière paginée les objets dont le préfixe est dir dans un compartiment nommé 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 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');
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier de manière paginée les objets dont le préfixe est dir dans un compartiment nommé 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 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');

Spécifier l'encodage des noms d'objets

Si un nom d'objet contient des caractères spéciaux, vous devez l'encoder pour la transmission. OSS prend uniquement en charge l'encodage URL.

  • Guillemets simples (')

  • Guillemets doubles (")

  • Esperluettes (&)

  • Chevrons (< >)

  • Virgules d'énumération (、)

  • Caractères chinois

Appelez la méthode listObjects ou listObjectsV2 pour spécifier l'encodage des noms d'objets.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour spécifier l'encodage des noms d'objets.

    <?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');
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour spécifier l'encodage des noms d'objets.

    <?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');

Simuler des répertoires

OSS utilise une structure plate où toutes les données sont stockées sous forme d'objets. Pour simuler un répertoire, créez un objet de 0 octet dont le nom se termine par une barre oblique (/). Vous pouvez télécharger et téléverser cet objet. La console OSS affiche les objets dont le nom se termine par une barre oblique (/) comme des répertoires.

Spécifiez les paramètres delimiter et prefix pour répertorier les objets par répertoire.

  • Si vous définissez prefix sur un nom de répertoire dans la requête, les objets et sous-répertoires dont les noms contiennent ce préfixe sont répertoriés.

  • Si vous spécifiez un préfixe et définissez delimiter sur une barre oblique (/) dans la requête, les objets et sous-répertoires dont les noms commencent par le préfixe indiqué dans le répertoire sont répertoriés. Chaque sous-répertoire est listé comme un seul élément de résultat dans CommonPrefixes. Les objets et répertoires contenus dans ces sous-répertoires ne sont pas répertoriés.

Par exemple, un compartiment contient les objets suivants : oss.jpg, fun/test.jpg, fun/movie/001.avi et fun/movie/007.avi. La barre oblique (/) est spécifiée comme délimiteur de répertoire. Les exemples suivants décrivent comment répertorier les objets dans des répertoires simulés.

Répertorier tous les objets d'un compartiment

Appelez la méthode listObjects ou listObjectsV2 pour répertorier tous les objets d'un compartiment spécifié.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier tous les objets d'un compartiment nommé 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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier tous les objets d'un compartiment nommé 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");
      }
    }
  • Réponse

    La réponse suivante est renvoyée lorsque vous appelez les méthodes précédentes pour répertorier tous les objets du compartiment examplebucket.

    objectList:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg

Répertorier tous les objets d'un répertoire spécifié

Appelez la méthode listObjects ou listObjectsV2 pour répertorier tous les objets du répertoire fun/.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier tous les objets du répertoire fun/.

    <?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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier tous les objets du répertoire fun/.

    <?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");
      }
    }
  • Résultats

    La réponse suivante est renvoyée lorsque vous appelez les méthodes précédentes pour répertorier tous les objets du répertoire fun/.

    objectList:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg

Répertorier les objets et sous-répertoires d'un répertoire

Appelez la méthode listObjects ou listObjectsV2 pour répertorier les objets et sous-répertoires du répertoire fun/.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour répertorier les objets et sous-répertoires du répertoire fun/.

    <?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");
      }
    }
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour répertorier les objets et sous-répertoires du répertoire fun/.

    <?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");
      }
    }
  • Valeur de retour

    La réponse suivante est renvoyée lorsque vous appelez les méthodes précédentes pour répertorier les objets et sous-répertoires du répertoire fun/.

    objectList:
    fun/test.jpg
    prefixList:
    fun/movie/

Obtenir la taille des objets d'un répertoire spécifié

Appelez la méthode listObjects ou listObjectsV2 pour obtenir la taille des objets du répertoire fun/.

  • Utiliser la méthode listObjects

    Le code suivant montre comment appeler la méthode listObjects pour obtenir la taille des objets du répertoire fun/.

    <?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);
  • Utiliser la méthode listObjectsV2

    Le code suivant montre comment appeler la méthode listObjectsV2 pour obtenir la taille des objets du répertoire fun/.

    <?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);
  • Valeur de retour

    La réponse suivante est renvoyée lorsque vous appelez les méthodes précédentes pour obtenir la taille des objets.

    object namefun/movie/001.avi:0.01953125KB
    object namefun/movie/007.avi:290.71875KB
    object namefun/test.jpg:144.216796875KB
    fun/:434.955078125KB

Références