Todos os produtos
Search
Central de documentação

Object Storage Service:Gerencie a ACL de um bucket usando o OSS SDK for PHP 2.0

Última atualização: Sep 25, 2026

Um bucket é um contêiner usado para armazenar objetos no Object Storage Service (OSS). Todos os objetos são armazenados em buckets. Este tópico descreve como configurar e consultar a lista de controle de acesso (ACL) de um bucket.

Notas de uso

  • O código de exemplo neste tópico usa o ID de região cn-hangzhou da região China (Hangzhou). Por padrão, o endpoint público é usado para acessar recursos em um bucket. Para acessar os recursos do bucket usando outros produtos da Alibaba Cloud na mesma região em que o bucket está localizado, use um endpoint interno. Para obter mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.

  • Para configurar a ACL de um bucket, você deve ter a permissão oss:PutBucketAcl. Para consultar a ACL de um bucket, você deve ter a permissão oss:GetBucketAcl. Para obter mais informações, consulte Grant a custom policy.

ACLs de bucket

A tabela a seguir descreve as ACLs de bucket.

ACL

Descrição

Método

Private

Somente o proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos objetos do bucket. Outros usuários não podem acessar os objetos no bucket.

oss.BucketACLPrivate

Public-read

Somente o proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos objetos do bucket. Outros usuários têm apenas permissões de leitura nos objetos do bucket. Defina a ACL do bucket com este valor com cautela.

oss.BucketACLPublicRead

Public-read-write

Todos os usuários têm permissões de leitura e gravação nos objetos do bucket. Defina a ACL do bucket com este valor com cautela.

oss.BucketACLPublicReadWrite

Exemplos

  1. O código de exemplo a seguir mostra como configurar a ACL de um bucket:

    <?php
    
    // Introduce autoload files to load dependent libraries.
    require_once __DIR__ . '/../vendor/autoload.php';
    
    use AlibabaCloud\Oss\V2 as Oss;
    
    // Specify descriptions for command line parameters.
    $optsdesc = [
        "region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located.
        "endpoint" => ['help' => The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS.
        "bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket.
    ];
    
    // Generate a long options list to parse the command line parameters.
    $longopts = \array_map(function ($key) {
        return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
    }, array_keys($optsdesc));
    
    // Parse the command line parameters.
    $options = getopt("", $longopts); 
    
    // Check whether the required parameters are configured.
    foreach ($optsdesc as $key => $value) {
        if ($value['required'] === True && empty($options[$key])) {
            $help = $value['help'];
            echo "Error: the following arguments are required: --$key, $help"; // Specify that the required parameters are not configured.
            exit(1); 
        }
    }
    
    // Obtain the values of the command line parameters.
    $region = $options["region"]; // The region in which the bucket is located.
    $bucket = $options["bucket"]; // The name of the bucket.
    
    // Use environment variables to load the AccessKey ID and AccessKey secret.
    $credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
    
    // Use the default configurations of the SDK.
    $cfg = Oss\Config::loadDefault();
    
    // Specify the credential provider.
    $cfg->setCredentialsProvider($credentialsProvider);
    
    // Specify the region.
    $cfg->setRegion($region);
    
    // Specify the endpoint if an endpoint is provided.
    if (isset($options["endpoint"])) {
        $cfg->setEndpoint($options["endpoint"]);
    }
    
    // Create an OSSClient instance.
    $client = new Oss\Client($cfg);
    
    // Create a PutBucketAclRequest object and set the bucket ACL to private.
    $request = new Oss\Models\PutBucketAclRequest(
          bucket: $bucket, 
          acl: Oss\Models\BucketACLType::PRIVATE);
    
    // Use the putBucketAcl method to specify the bucket ACL.
    $result = $client->putBucketAcl($request);
    
    // Display the result.
    printf(
        'status code:' . $result->statusCode . PHP_EOL . // The returned HTTP status code.
        'request id:' . $result->requestId // The request ID of the request, which is the unique identifier of the request.
    );
    
  1. O código de exemplo a seguir mostra como consultar a ACL de um bucket:

    <?php
    
    // Introduce autoload files to load dependent libraries.
    require_once __DIR__ . '/../vendor/autoload.php';
    
    use AlibabaCloud\Oss\V2 as Oss;
    
    // Specify descriptions for command line parameters.
    $optsdesc = [
        "region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located.
        "endpoint" => ['help' => The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS.
        "bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket.
    ];
    
    // Generate a long options list to parse the command line parameters.
    $longopts = \array_map(function ($key) {
        return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
    }, array_keys($optsdesc));
    
    // Parse the command line parameters.
    $options = getopt("", $longopts); 
    
    // Check whether the required parameters are configured.
    foreach ($optsdesc as $key => $value) {
        if ($value['required'] === True && empty($options[$key])) {
            $help = $value['help'];
            echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
            exit(1); 
        }
    }
    
    // Obtain the values of the command line parameters.
    $region = $options["region"]; // The region in which the bucket is located.
    $bucket = $options["bucket"]; // The name of the bucket.
    
    // Use environment variables to load the AccessKey ID and AccessKey secret.
    $credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
    
    // Use the default configurations of the SDK.
    $cfg = Oss\Config::loadDefault();
    
    // Specify the credential provider.
    $cfg->setCredentialsProvider($credentialsProvider);
    
    // Specify the region.
    $cfg->setRegion($region);
    
    // Specify the endpoint if an endpoint is provided.
    if (isset($options["endpoint"])) {
        $cfg->setEndpoint($options["endpoint"]);
    }
    
    // Create an OSSClient instance.
    $client = new Oss\Client($cfg);
    
    // Create a GetBucketAclRequest object to query the bucket ACL.
    $request = new Oss\Models\GetBucketAclRequest(bucket: $bucket);
    
    // Use the getBucketAcl method to query the bucket ACL.
    $result = $client->getBucketAcl($request);
    
    // Display the result.
    printf(
        'status code:' . $result->statusCode . PHP_EOL . // The returned HTTP status code.
        'request id:' . $result->requestId . PHP_EOL . // The unique identifier of the request.
        'bucket acl:' . $result->accessControlList->grant // The ACL of the bucket.
    );
    

Referências

  • Para obter o código de exemplo completo usado para configurar a ACL de um bucket, acesse o GitHub.

  • Para obter mais informações sobre a operação de API que você pode chamar para configurar a ACL de um bucket, consulte PutBucketAcl.

  • Para obter o código de exemplo completo usado para consultar a ACL de um bucket, acesse o GitHub.

  • Para obter mais informações sobre a operação de API que você pode chamar para consultar a ACL de um bucket, consulte GetBucketAcl.