Set and get bucket ACLs to control read and write permissions on the objects in 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.
-
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.
-
Setting a bucket ACL requires the
oss:PutBucketAclpermission. Retrieving a bucket ACL requires theoss:GetBucketAclpermission. For more information, see Grant custom access policies to a RAM user.
Set the ACL for a bucket
OSS supports the following bucket ACLs.
|
ACL |
Description |
Method |
|
private |
Only the bucket owner and authorized users can read and write objects in the bucket. Other users cannot access the objects. |
OssClient::OSS_ACL_TYPE_PRIVATE |
|
public-read |
The bucket owner and authorized users can read and write objects in the bucket. Other users have read-only access. Exercise caution when you use this ACL. |
OssClient::OSS_ACL_TYPE_PUBLIC_READ |
|
public-read-write |
All users have read and write access to objects in the bucket. Exercise caution when you use this ACL. |
OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE |
The following sample 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 code gets the bucket 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\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 complete sample code for managing bucket ACLs, see GitHub.
-
For more information about the API operation for setting a bucket ACL, see PutBucketAcl.
-
For more information about the API operation for retrieving a bucket ACL, see GetBucketAcl.