Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Manage folders (PHP SDK V1)

Son Güncelleme:Oct 01, 2026

OSS, klasör yapısını simüle eden dizinler sağlar. Bu sayede çok sayıda nesneyi düzenlemek, bulmak ve toplu işlemler yapmak kolaylaşır.

Dikkat edilecek noktalar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda, bir OSS uç noktası kullanarak OssClient örneğinin nasıl oluşturulacağı gösterilmektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. OssClient oluşturun.

Klasör oluşturun

Aşağıdaki kod, bir klasörün nasıl oluşturulacağını gösterir.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Specify the folder name. The name must end with a forward slash (/).
$object = "exampledir/";
$content = "";

// You can set headers when you upload the object. For example, you can set the access permissions to private or specify custom metadata.
$options = array(
    OssClient::OSS_HEADERS => array(
        'x-oss-object-acl' => 'private',
        'x-oss-meta-info' => 'your info'
    ),
);
try{
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $ossClient->putObject($bucket, $object, $content, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . "OK" . "\n");           

Klasör silin

Uyarı

Bir dizini silmek, alt dizinlerini ve içindeki tüm nesneleri de siler. İşlemi dikkatli gerçekleştirin.

Aşağıdaki kod, examplebucket paketindeki log klasörünü ve içerdiği tüm dosyaları silmeyi gösterir.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
   require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
   require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name.
$bucket = "examplebucket";

try {
   $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
   $option = array(
      OssClient::OSS_MARKER => null,
      // Specify the full path of the folder to delete. The full path does not include the bucket name.
      OssClient::OSS_PREFIX => "log/",
      OssClient::OSS_DELIMITER=>'',
   );
   $bool = true;
   while ($bool){
      $result = $ossClient->listObjects($bucket,$option);
      $objects = array();
      if(count($result->getObjectList()) > 0){
         foreach ($result->getObjectList() as $key => $info){
            printf("key name:".$info->getKey().PHP_EOL);
            $objects[] = $info->getKey();
         }
         // Delete the folder and all files in it.
         $delObjects = $ossClient->deleteObjects($bucket, $objects);
         foreach ($delObjects as $info){
            $obj = strval($info);
            printf("Delete ".$obj." : Success" . PHP_EOL);
         }
      }

      if($result->getIsTruncated() === 'true'){
         $option[OssClient::OSS_MARKER] = $result->getNextMarker();
      }else{
         $bool = false;
      }
   }
   printf("Delete Objects : OK" . PHP_EOL);
} catch (OssException $e) {
   printf("Delete Objects : Failed" . PHP_EOL);
   printf($e->getMessage() . PHP_EOL);
   return;
}

İlgili kaynaklar

  • Klasör oluşturun

    Klasör oluşturmak için kullanılan API işlemi hakkında daha fazla bilgi için bkz. PutObject.

  • Klasör silin

    • Bir klasörü ve içindeki tüm dosyaları silmek için eksiksiz örnek kodu görmek üzere GitHub örneğine bakın.

    • Klasörleri ve içindeki tüm dosyaları silmek için kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.