A bucket access control list (ACL) controls read and write permissions for the bucket and its objects. This topic describes how to set and query the ACL of a bucket by using OSS PHP SDK V1.
Prerequisites
Before you begin, make sure that:
The
oss:PutBucketAclpermission is granted to set the bucket ACL. Theoss:GetBucketAclpermission is granted to query the bucket ACL. For more information, see Grant custom access policies to a RAM user.An OSSClient instance is created. For more information, see Create an OSSClient instance.
Usage notes
This topic uses the public endpoint of the China (Hangzhou) region. To access Object Storage Service (OSS) from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.
To create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
ACL types
The following table describes the bucket ACL types.
| ACL | Description | Constant |
|---|---|---|
| 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 have read-only access. Use with caution. | OssClient::OSS_ACL_TYPE_PUBLIC_READ |
| public-read-write | All users have read and write permissions on objects in the bucket. Use with caution. | OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE |
Set the ACL of a bucket
The following code sets the ACL of a bucket to private:
<?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.
// Make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the China (Hangzhou) region is used. Replace the value with your 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");Query the ACL of a bucket
The following code queries 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.
// Make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the China (Hangzhou) region is used. Replace the value with your 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);
// Query 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 complete sample code for managing bucket ACLs, see GitHub.
For the PutBucketAcl API, see PutBucketAcl.
For the GetBucketAcl API, see GetBucketAcl.