Skip to content

Commit 70b0600

Browse files
yangzong18huiguangjun
authored andcommitted
add bucket encryption api
1 parent 392e638 commit 70b0600

19 files changed

+931
-35
lines changed

sample/DeleteBucketEncryption.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use AlibabaCloud\Oss\V2 as Oss;
6+
7+
// parse args
8+
$optsdesc = [
9+
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
10+
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
11+
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
12+
];
13+
$longopts = \array_map(function ($key) {
14+
return "$key:";
15+
}, array_keys($optsdesc));
16+
$options = getopt("", $longopts);
17+
foreach ($optsdesc as $key => $value) {
18+
if ($value['required'] === True && empty($options[$key])) {
19+
$help = $value['help'];
20+
echo "Error: the following arguments are required: --$key, $help";
21+
exit(1);
22+
}
23+
}
24+
25+
$region = $options["region"];
26+
$bucket = $options["bucket"];
27+
28+
// Loading credentials values from the environment variables
29+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
30+
31+
// Using the SDK's default configuration
32+
$cfg = Oss\Config::loadDefault();
33+
$cfg->setCredentialsProvider($credentialsProvider);
34+
$cfg->setRegion($region);
35+
if (isset($options["endpoint"])) {
36+
$cfg->setEndpoint($options["endpoint"]);
37+
}
38+
39+
$client = new Oss\Client($cfg);
40+
$request = new Oss\Models\DeleteBucketEncryptionRequest($bucket);
41+
$result = $client->deleteBucketEncryption($request);
42+
43+
printf(
44+
'status code:' . $result->statusCode . PHP_EOL .
45+
'request id:' . $result->requestId
46+
);
47+

sample/GetBucketEncryption.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use AlibabaCloud\Oss\V2 as Oss;
6+
7+
// parse args
8+
$optsdesc = [
9+
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
10+
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
11+
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
12+
];
13+
$longopts = \array_map(function ($key) {
14+
return "$key:";
15+
}, array_keys($optsdesc));
16+
$options = getopt("", $longopts);
17+
foreach ($optsdesc as $key => $value) {
18+
if ($value['required'] === True && empty($options[$key])) {
19+
$help = $value['help'];
20+
echo "Error: the following arguments are required: --$key, $help";
21+
exit(1);
22+
}
23+
}
24+
25+
$region = $options["region"];
26+
$bucket = $options["bucket"];
27+
28+
// Loading credentials values from the environment variables
29+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
30+
31+
// Using the SDK's default configuration
32+
$cfg = Oss\Config::loadDefault();
33+
$cfg->setCredentialsProvider($credentialsProvider);
34+
$cfg->setRegion($region);
35+
if (isset($options["endpoint"])) {
36+
$cfg->setEndpoint($options["endpoint"]);
37+
}
38+
39+
$client = new Oss\Client($cfg);
40+
$request = new Oss\Models\GetBucketEncryptionRequest($bucket);
41+
$result = $client->getBucketEncryption($request);
42+
43+
printf(
44+
'status code:' . $result->statusCode . PHP_EOL .
45+
'request id:' . $result->requestId . PHP_EOL .
46+
'encryption:' . var_export($result->serverSideEncryptionRule, true)
47+
);
48+

sample/PutBucketEncryption.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use AlibabaCloud\Oss\V2 as Oss;
6+
7+
// parse args
8+
$optsdesc = [
9+
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
10+
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
11+
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
12+
];
13+
$longopts = \array_map(function ($key) {
14+
return "$key:";
15+
}, array_keys($optsdesc));
16+
$options = getopt("", $longopts);
17+
foreach ($optsdesc as $key => $value) {
18+
if ($value['required'] === True && empty($options[$key])) {
19+
$help = $value['help'];
20+
echo "Error: the following arguments are required: --$key, $help";
21+
exit(1);
22+
}
23+
}
24+
25+
$region = $options["region"];
26+
$bucket = $options["bucket"];
27+
28+
// Loading credentials values from the environment variables
29+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
30+
31+
// Using the SDK's default configuration
32+
$cfg = Oss\Config::loadDefault();
33+
$cfg->setCredentialsProvider($credentialsProvider);
34+
$cfg->setRegion($region);
35+
if (isset($options["endpoint"])) {
36+
$cfg->setEndpoint($options["endpoint"]);
37+
}
38+
39+
$client = new Oss\Client($cfg);
40+
$request = new Oss\Models\PutBucketEncryptionRequest($bucket, new Oss\Models\ServerSideEncryptionRule(
41+
new Oss\Models\ApplyServerSideEncryptionByDefault(
42+
sseAlgorithm: 'KMS',
43+
kmsDataEncryption: 'SM4'
44+
)));
45+
$result = $client->putBucketEncryption($request);
46+
47+
printf(
48+
'status code:' . $result->statusCode . PHP_EOL .
49+
'request id:' . $result->requestId
50+
);

src/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@
190190
* @method \GuzzleHttp\Promise\Promise deleteBucketPolicyAsync(Models\DeleteBucketPolicyRequest $request, array $args = []) Deletes a policy for a bucket.
191191
* @method Models\GetBucketPolicyStatusResult getBucketPolicyStatus(Models\GetBucketPolicyStatusRequest $request, array $args = []) Checks whether the current bucket policy allows public access.
192192
* @method \GuzzleHttp\Promise\Promise getBucketPolicyStatusAsync(Models\GetBucketPolicyStatusRequest $request, array $args = []) Checks whether the current bucket policy allows public access.
193+
* @method Models\PutBucketEncryptionResult putBucketEncryption(Models\PutBucketEncryptionRequest $request, array $args = []) Configures encryption rules for a bucket.
194+
* @method \GuzzleHttp\Promise\Promise putBucketEncryptionAsync(Models\PutBucketEncryptionRequest $request, array $args = []) Configures encryption rules for a bucket.
195+
* @method Models\GetBucketEncryptionResult getBucketEncryption(Models\GetBucketEncryptionRequest $request, array $args = []) Queries the encryption rules configured for a bucket.
196+
* @method \GuzzleHttp\Promise\Promise getBucketEncryptionAsync(Models\GetBucketEncryptionRequest $request, array $args = []) Queries the encryption rules configured for a bucket.
197+
* @method Models\DeleteBucketEncryptionResult deleteBucketEncryption(Models\DeleteBucketEncryptionRequest $request, array $args = []) Deletes encryption rules for a bucket.
198+
* @method \GuzzleHttp\Promise\Promise deleteBucketEncryptionAsync(Models\DeleteBucketEncryptionRequest $request, array $args = []) Deletes encryption rules for a bucket.
193199
*/
194200
final class Client
195201
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlibabaCloud\Oss\V2\Models;
6+
7+
use AlibabaCloud\Oss\V2\Types\Model;
8+
use AlibabaCloud\Oss\V2\Annotation\XmlElement;
9+
use AlibabaCloud\Oss\V2\Annotation\XmlRoot;
10+
11+
/**
12+
* Class ApplyServerSideEncryptionByDefault
13+
* @package AlibabaCloud\Oss\V2\Models
14+
*/
15+
#[XmlRoot(name: 'ApplyServerSideEncryptionByDefault')]
16+
final class ApplyServerSideEncryptionByDefault extends Model
17+
{
18+
/**
19+
* The default server-side encryption method. Valid values: KMS, AES256, and SM4. You are charged when you call API operations to encrypt or decrypt data by using CMKs managed by KMS. For more information, see [Billing of KMS](~~52608~~). If the default server-side encryption method is configured for the destination bucket and ReplicaCMKID is configured in the CRR rule:* If objects in the source bucket are not encrypted, they are encrypted by using the default encryption method of the destination bucket after they are replicated.* If objects in the source bucket are encrypted by using SSE-KMS or SSE-OSS, they are encrypted by using the same method after they are replicated.For more information, see [Use data replication with server-side encryption](~~177216~~).
20+
* @var string|null
21+
*/
22+
#[XmlElement(rename: 'SSEAlgorithm', type: 'string')]
23+
public ?string $sseAlgorithm;
24+
25+
/**
26+
* The CMK ID that is specified when SSEAlgorithm is set to KMS and a specified CMK is used for encryption. In other cases, leave this parameter empty.
27+
* @var string|null
28+
*/
29+
#[XmlElement(rename: 'KMSMasterKeyID', type: 'string')]
30+
public ?string $kmsMasterKeyID;
31+
32+
/**
33+
* The algorithm that is used to encrypt objects. If this parameter is not specified, objects are encrypted by using AES256. This parameter is valid only when SSEAlgorithm is set to KMS. Valid value: SM4.
34+
* @var string|null
35+
*/
36+
#[XmlElement(rename: 'KMSDataEncryption', type: 'string')]
37+
public ?string $kmsDataEncryption;
38+
39+
40+
/**
41+
* ApplyServerSideEncryptionByDefault constructor.
42+
* @param string|null $sseAlgorithm The default server-side encryption method.
43+
* @param string|null $kmsMasterKeyID The CMK ID that is specified when SSEAlgorithm is set to KMS and a specified CMK is used for encryption.
44+
* @param string|null $kmsDataEncryption The algorithm that is used to encrypt objects.
45+
*/
46+
public function __construct(
47+
?string $sseAlgorithm = null,
48+
?string $kmsMasterKeyID = null,
49+
?string $kmsDataEncryption = null
50+
)
51+
{
52+
$this->sseAlgorithm = $sseAlgorithm;
53+
$this->kmsMasterKeyID = $kmsMasterKeyID;
54+
$this->kmsDataEncryption = $kmsDataEncryption;
55+
}
56+
}

src/Models/BucketInfo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ final class BucketInfo extends Model
8282

8383
/**
8484
* The server-side encryption configurations of the bucket.
85-
* @var ServerSideEncryptionRule|null
85+
* @var ServerSideEncryptionRuleInfo |null
8686
*/
87-
public ?ServerSideEncryptionRule $sseRule;
87+
public ?ServerSideEncryptionRuleInfo $sseRule;
8888

8989
/**
9090
* The versioning status of the bucket.
@@ -138,7 +138,7 @@ final class BucketInfo extends Model
138138
* @param Owner|null $owner The owner of the bucket.
139139
* @param string|null $storageClass The storage class of the bucket.
140140
* @param string|null $resourceGroupId The ID of the resource group to which the bucket belongs.
141-
* @param ServerSideEncryptionRule|null $sseRule The server-side encryption configurations of the bucket.
141+
* @param ServerSideEncryptionRuleInfo |null $sseRule The server-side encryption configurations of the bucket.
142142
* @param string|null $versioning The versioning status of the bucket.
143143
* @param string|null $transferAcceleration Indicates whether transfer acceleration is enabled for the bucket.
144144
* @param string|null $crossRegionReplication Indicates whether cross-region replication (CRR) is enabled for the bucket.
@@ -158,7 +158,7 @@ public function __construct(
158158
?Owner $owner = null,
159159
?string $storageClass = null,
160160
?string $resourceGroupId = null,
161-
?ServerSideEncryptionRule $sseRule = null,
161+
?ServerSideEncryptionRuleInfo $sseRule = null,
162162
?string $versioning = null,
163163
?string $transferAcceleration = null,
164164
?string $crossRegionReplication = null,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace AlibabaCloud\Oss\V2\Models;
5+
6+
use AlibabaCloud\Oss\V2\Types\RequestModel;
7+
use AlibabaCloud\Oss\V2\Annotation\TagProperty;
8+
use AlibabaCloud\Oss\V2\Annotation\RequiredProperty;
9+
10+
/**
11+
* The request for the DeleteBucketEncryption operation.
12+
* Class DeleteBucketEncryptionRequest
13+
* @package AlibabaCloud\Oss\V2\Models
14+
*/
15+
final class DeleteBucketEncryptionRequest extends RequestModel
16+
{
17+
/**
18+
* The name of the bucket.
19+
* @var string|null
20+
*/
21+
#[RequiredProperty()]
22+
#[TagProperty(tag: '', position: 'host', rename: 'bucket', type: 'string')]
23+
public ?string $bucket;
24+
25+
/**
26+
* DeleteBucketEncryptionRequest constructor.
27+
* @param string|null $bucket The name of the bucket.
28+
* @param array|null $options
29+
*/
30+
public function __construct(
31+
?string $bucket = null,
32+
?array $options = null
33+
)
34+
{
35+
$this->bucket = $bucket;
36+
parent::__construct($options);
37+
}
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace AlibabaCloud\Oss\V2\Models;
5+
6+
use AlibabaCloud\Oss\V2\Types\ResultModel;
7+
8+
/**
9+
* The result for the DeleteBucketEncryption operation.
10+
* Class DeleteBucketEncryptionResult
11+
* @package AlibabaCloud\Oss\V2\Models
12+
*/
13+
final class DeleteBucketEncryptionResult extends ResultModel
14+
{
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace AlibabaCloud\Oss\V2\Models;
5+
6+
use AlibabaCloud\Oss\V2\Types\RequestModel;
7+
use AlibabaCloud\Oss\V2\Annotation\TagProperty;
8+
use AlibabaCloud\Oss\V2\Annotation\RequiredProperty;
9+
10+
/**
11+
* The request for the GetBucketEncryption operation.
12+
* Class GetBucketEncryptionRequest
13+
* @package AlibabaCloud\Oss\V2\Models
14+
*/
15+
final class GetBucketEncryptionRequest extends RequestModel
16+
{
17+
/**
18+
* The name of the bucket.
19+
* @var string|null
20+
*/
21+
#[RequiredProperty()]
22+
#[TagProperty(tag: '', position: 'host', rename: 'bucket', type: 'string')]
23+
public ?string $bucket;
24+
25+
/**
26+
* GetBucketEncryptionRequest constructor.
27+
* @param string|null $bucket The name of the bucket.
28+
* @param array|null $options
29+
*/
30+
public function __construct(
31+
?string $bucket = null,
32+
?array $options = null
33+
)
34+
{
35+
$this->bucket = $bucket;
36+
parent::__construct($options);
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace AlibabaCloud\Oss\V2\Models;
5+
6+
use AlibabaCloud\Oss\V2\Types\ResultModel;
7+
use AlibabaCloud\Oss\V2\Annotation\TagBody;
8+
9+
/**
10+
* The result for the GetBucketEncryption operation.
11+
* Class GetBucketEncryptionResult
12+
* @package AlibabaCloud\Oss\V2\Models
13+
*/
14+
final class GetBucketEncryptionResult extends ResultModel
15+
{
16+
/**
17+
* The container that stores server-side encryption rules.
18+
* @var ServerSideEncryptionRule|null
19+
*/
20+
#[TagBody(rename: 'ServerSideEncryptionRule', type: ServerSideEncryptionRule::class, format: 'xml')]
21+
public ?ServerSideEncryptionRule $serverSideEncryptionRule;
22+
23+
/**
24+
* GetBucketEncryptionRequest constructor.
25+
* @param ServerSideEncryptionRule|null $serverSideEncryptionRule The container that stores server-side encryption rules.
26+
*/
27+
public function __construct(
28+
?ServerSideEncryptionRule $serverSideEncryptionRule = null
29+
)
30+
{
31+
$this->serverSideEncryptionRule = $serverSideEncryptionRule;
32+
}
33+
}

0 commit comments

Comments
 (0)