All Products
Search
Document Center

Object Storage Service:Manage bucket ACLs (PHP SDK V1)

Last Updated:Nov 29, 2025

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are stored in buckets. This topic describes how to configure and query the access control list (ACL) of a bucket.

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 set the ACL for a bucket, you must have the oss:PutBucketAcl permission. To retrieve the ACL for a bucket, you must have the oss:GetBucketAcl permission. For more information, see Grant custom access policies to a RAM user.

Set the ACL for a bucket

The following table describes the bucket ACLs.

ACL

Description

Method

private

Only the bucket owner and authorized users have read and write permissions on objects in the bucket. Other users cannot access the objects.

OssClient::OSS_ACL_TYPE_PRIVATE

public-read

The bucket owner and authorized users have read and write permissions on objects in the bucket. Other users only have read permission. Use this permission with caution.

OssClient::OSS_ACL_TYPE_PUBLIC_READ

public-read-write

All users have read and write permissions on objects in the bucket. Use this permission with caution.

OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE

The following code shows how to set the ACL for a bucket:

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

// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The China (Hangzhou) region is used as an example. Replace the value with the actual Endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "yourBucketName";
// Set the bucket ACL to private.
$acl = OssClient::OSS_ACL_TYPE_PRIVATE;
try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

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

Get the ACL for a bucket

The following sample code provides an example on how to query the ACL of a bucket:

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

// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The China (Hangzhou) region is used as an example. Replace the value with the actual Endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "yourBucketName";

try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
    // Get the bucket ACL.
    $res = $ossClient->getBucketAcl($bucket);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");
print('acl: ' . $res);

References

  • For the complete sample code that shows how to manage bucket ACLs, see GitHub.

  • For more information about the API operation to set a bucket ACL, see PutBucketAcl.

  • For more information about the API operation to retrieve a bucket ACL, see GetBucketAcl.