Manage object ACLs (PHP SDK V1)

Updated at:

Object Storage Service (OSS) provides four object-level access control lists (ACLs): public-read-write, public-read, private, and default. This topic describes how to manage object ACLs in a versioned bucket.

Usage 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.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.

  • To set the ACL of an object, you must have the oss:PutObjectAcl permission. To retrieve the ACL of an object, you must have the oss:GetObjectAcl permission. For more information, see Attach a custom policy to a RAM user.

Object ACLs

An Object ACL has four access permissions:

ACL value

Name

Access control

public-read-write

Public read/write

All users, including anonymous users, can read and write the object.

public-read

Public read, private write

The object owner has read and write permissions. Other users, including anonymous users, have only read permissions.

private

Private

Only the object owner has read and write permissions. Other users cannot access the object without authorization.

default

Default

The ACL of the object is the same as the ACL of the bucket.

The ACL of an object takes precedence over the ACL of the bucket in which the object is stored. For example, if the ACL of a bucket is public-read and the ACL of an object in the bucket is private, only the object owner has read and write permissions on the object. Other users cannot access the object without authorization.

Set an object ACL

By default, the putObjectAcl method sets the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns a 404 Not Found error. You can specify a version ID in the request to set the ACL of a specific version of the object.

The following code shows how to set an object ACL:

<?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 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 value with the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "<yourBucketName>";
// Specify the full path of the object. Do not include the bucket name in the path. Example: example/test.txt.
$object = "<yourObjectName>";
// Specify the version ID of the object.
$versionId = "<yourObjectVersionId>";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try {
    // Set the ACL of the specified version of the object to public-read.
    $ossClient->putObjectAcl($bucket, $object, 'public-read', array(OssClient::OSS_VERSION_ID => $versionId));
  
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");

For more information about how to set object ACLs, see PutObjectACL.

Get an object ACL

By default, the getObjectAcl method retrieves the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns a 404 Not Found error. You can specify a version ID in the request to retrieve the ACL of a specific version of the object.

The following code shows how to retrieve an object ACL:

<?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 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 value with the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "<yourBucketName>";
// Specify the full path of the object. Do not include the bucket name in the path. Example: example/test.txt.
$object = "<yourObjectName>";
// Specify the version ID of the object.
$versionId = "<yourObjectVersionId>";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try {
    // Get the ACL of the object.
    $acl = $ossClient->getObjectAcl($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId));
    printf($acl . "\n");

} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");

For more information about how to retrieve object ACLs, see GetObjectACL.