Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer les listes de contrôle d'accès (ACL) des objets à l'aide du SDK OSS pour PHP 2.0

Dernière mise à jour :Aug 18, 2026

Définissez et interrogez les listes de contrôle d'accès (ACL) des objets avec le SDK OSS pour PHP.

Remarques sur l'utilisation

  • Par défaut, les exemples de code de cette rubrique utilisent la région cn-hangzhou et l'endpoint public. Si vous accédez au bucket depuis un autre service Alibaba Cloud situé dans la même région, utilisez un endpoint interne pour optimiser les performances et la sécurité. Pour plus d'informations sur les régions et les endpoints, consultez la section Régions et endpoints.

  • Pour définir l'ACL d'un objet, vous devez disposer de l'autorisation oss:PutObjectAcl. Pour interroger l'ACL d'un objet, vous devez disposer de l'autorisation oss:GetObjectAcl. Pour plus d'informations, consultez la section Accorder une politique personnalisée.

Types d'ACL

Le tableau suivant décrit les types d'ACL disponibles pour un objet.

ACL

Description

Valeur

Héritée du bucket

L'objet hérite de l'ACL du bucket dans lequel il est stocké.

oss.ObjectACLDefault

Privé

Seul le propriétaire de l'objet et les utilisateurs autorisés peuvent lire et écrire l'objet. Les autres utilisateurs ne peuvent pas y accéder.

oss.ObjectACLPrivate

Lecture publique

Seul le propriétaire de l'objet et les utilisateurs autorisés peuvent lire et écrire l'objet. Les autres utilisateurs peuvent uniquement lire l'objet. Soyez prudent lorsque vous définissez l'ACL de l'objet sur cette valeur.

ObjectACLPublicRead

Lecture-écriture publique

Tous les utilisateurs peuvent lire et écrire l'objet. Soyez prudent lorsque vous définissez l'ACL de l'objet sur cette valeur.

oss.ObjectACLPublicReadWrite

L'ACL d'un objet est prioritaire sur l'ACL du bucket. Par exemple, si un objet situé dans un bucket privé possède l'ACL public-read, tous les utilisateurs, y compris les utilisateurs anonymes, peuvent lire l'objet. Si aucune ACL n'est définie pour un objet, celui-ci hérite de l'ACL de son bucket.

Exemple de code

1. Définissez l'ACL d'un objet.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket" => ['help' => 'The name of the bucket', 'required' => True],
    "key" => ['help' => 'The name of the object', 'required' => True],
];

// Create an array of long options required by getopt. Example: --region:.
$longopts = array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command line parameters.
$options = getopt("", $longopts); 

// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        // Print the help information and exit if required parameters are missing.
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help\n";
        exit(1);
    }
}

// Extract the values of the parameters.
$region = $options["region"];
$bucket = $options["bucket"];
$key = $options["key"];

// Obtain access credentials (AccessKey ID and AccessKey secret) from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Load the default configurations and specify the credential provider and region.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);

// If the endpoint parameter is specified, it will be configured as the custom access URL.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Initialize the OSSClient instance.
$client = new Oss\Client($cfg);

// Create a PutObjectAclRequest object and specify the bucket name, object key and ACL.
$request = new Oss\Models\PutObjectAclRequest($bucket, $key, Oss\Models\ObjectACLType::PUBLIC_READ);

// Set the ACL of the object to public-read.
$result = $client->putObjectAcl($request);

// Output the HTTP status code and request ID from the response.
printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId
);

2. Interrogez l'ACL d'un objet.

<?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 OSS endpoint.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
    "key" => ['help' => 'The name of the object', 'required' => True], // (Required) Specify the name of the object.
];

// 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 missing.
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.
$key = $options["key"]; // The name of the object.

// Load the AccessKey ID and AccessKey secret from environment variables.
$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);

// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSSClient instance.
$client = new Oss\Client($cfg);

// Create a request object for obtaining the ACL of the object.
$request = new Oss\Models\GetObjectAclRequest(bucket: $bucket, key: $key);

// Use the getObjectAcl method to query the ACL of the object.
$result = $client->getObjectAcl($request);

// Display the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
    'request id:' . $result->requestId . PHP_EOL . // The request ID.
    'acl:' . $result->accessControlList->grant // The ACL of the object.
);

Références

  • Pour obtenir l'exemple de code complet permettant de définir l'ACL d'un objet, consultez GitHub.

  • Pour en savoir plus sur l'opération API permettant de définir l'ACL d'un objet, consultez la section PutObjectACL.

  • Pour obtenir l'exemple de code complet permettant d'interroger l'ACL d'un objet, consultez GitHub.

  • Pour en savoir plus sur l'opération API permettant d'interroger l'ACL d'un objet, consultez la section GetObjectACL.