Managing Bucket Policies
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.
Besides bucket ACLs, bucket owners can use bucket policies to centrally control access to buckets and objects in buckets.
For more information, see Bucket Policy.
Setting a Bucket Policy
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
//Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
'endpoint' => 'https://your-endpoint',
]);
try {
// If Principal is set to *, all users are authorized. Exercise caution when setting this parameter based on service requirements. In the production environment, you are advised to authorize only specific IAM users or accounts.
$policy = json_encode(['Statement' => [['Principal' => '*', 'Effect' => 'Allow', 'Action' => 'ListBucket', 'Resource' => $bucketName]]]);
// Configure a bucket policy.
$resp = $obsClient->setBucketPolicy([
'Bucket' => "bucketname",
'Policy' => $policy
]);
printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("SetBucketPolicyFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
} Obtaining a Bucket Policy
You can call ObsClient->getBucketPolicy to obtain a bucket policy. Sample code is as follows:
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
//Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
'endpoint' => 'https://your-endpoint',
]);
try {
// Obtain a bucket policy.
$resp = $obsClient->getBucketPolicy([
'Bucket' => "bucketname",
]);
printf("RequestId:%s\n",$resp['RequestId']);
printf("Policy:%s\n",$resp['Policy']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("GetBucketPolicyFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
} Deleting a Bucket Policy
You can call ObsClient->deleteBucketPolicy to delete a bucket policy. Sample code is as follows:
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
//Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
'endpoint' => 'https://your-endpoint',
]);
try {
// Delete a bucket policy.
$resp = $obsClient->deleteBucketPolicy([
'Bucket' => "bucketname",
]);
printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("deleteBucketPolicyFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
} What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
