Skip to content

Commit 0adfe4a

Browse files
committed
Support S3::PutPublicAccessBlock
1 parent 0cfced4 commit 0adfe4a

File tree

8 files changed

+496
-0
lines changed

8 files changed

+496
-0
lines changed

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@
558558
"PutObject",
559559
"PutObjectAcl",
560560
"PutObjectTagging",
561+
"PutPublicAccessBlock",
561562
"UploadPart",
562563
"UploadPartCopy"
563564
]

src/Service/S3/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- AWS api-change: Expires's property and its getters/setters switched from `\DateTimeImmutable` to `string`.
88

9+
### Added
10+
11+
- Added `S3Client::putPublicAccessBlock()`
12+
913
### Changed
1014

1115
- Apply no CodingStandard from latest php-cs-fixer.
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
namespace AsyncAws\S3\Input;
4+
5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
use AsyncAws\Core\Input;
7+
use AsyncAws\Core\Request;
8+
use AsyncAws\Core\Stream\StreamFactory;
9+
use AsyncAws\S3\Enum\ChecksumAlgorithm;
10+
use AsyncAws\S3\ValueObject\PublicAccessBlockConfiguration;
11+
12+
final class PutPublicAccessBlockRequest extends Input
13+
{
14+
/**
15+
* The name of the Amazon S3 bucket whose `PublicAccessBlock` configuration you want to set.
16+
*
17+
* @required
18+
*
19+
* @var string|null
20+
*/
21+
private $bucket;
22+
23+
/**
24+
* The MD5 hash of the `PutPublicAccessBlock` request body.
25+
*
26+
* For requests made using the Amazon Web Services Command Line Interface (CLI) or Amazon Web Services SDKs, this field
27+
* is calculated automatically.
28+
*
29+
* @var string|null
30+
*/
31+
private $contentMd5;
32+
33+
/**
34+
* Indicates the algorithm used to create the checksum for the object when you use the SDK. This header will not provide
35+
* any additional functionality if you don't use the SDK. When you send this header, there must be a corresponding
36+
* `x-amz-checksum` or `x-amz-trailer` header sent. Otherwise, Amazon S3 fails the request with the HTTP status code
37+
* `400 Bad Request`. For more information, see Checking object integrity [^1] in the *Amazon S3 User Guide*.
38+
*
39+
* If you provide an individual checksum, Amazon S3 ignores any provided `ChecksumAlgorithm` parameter.
40+
*
41+
* [^1]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
42+
*
43+
* @var ChecksumAlgorithm::*|null
44+
*/
45+
private $checksumAlgorithm;
46+
47+
/**
48+
* The `PublicAccessBlock` configuration that you want to apply to this Amazon S3 bucket. You can enable the
49+
* configuration options in any combination. For more information about when Amazon S3 considers a bucket or object
50+
* public, see The Meaning of "Public" [^1] in the *Amazon S3 User Guide*.
51+
*
52+
* [^1]: https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html#access-control-block-public-access-policy-status
53+
*
54+
* @required
55+
*
56+
* @var PublicAccessBlockConfiguration|null
57+
*/
58+
private $publicAccessBlockConfiguration;
59+
60+
/**
61+
* The account ID of the expected bucket owner. If the account ID that you provide does not match the actual owner of
62+
* the bucket, the request fails with the HTTP status code `403 Forbidden` (access denied).
63+
*
64+
* @var string|null
65+
*/
66+
private $expectedBucketOwner;
67+
68+
/**
69+
* @param array{
70+
* Bucket?: string,
71+
* ContentMD5?: string|null,
72+
* ChecksumAlgorithm?: ChecksumAlgorithm::*|null,
73+
* PublicAccessBlockConfiguration?: PublicAccessBlockConfiguration|array,
74+
* ExpectedBucketOwner?: string|null,
75+
* '@region'?: string|null,
76+
* } $input
77+
*/
78+
public function __construct(array $input = [])
79+
{
80+
$this->bucket = $input['Bucket'] ?? null;
81+
$this->contentMd5 = $input['ContentMD5'] ?? null;
82+
$this->checksumAlgorithm = $input['ChecksumAlgorithm'] ?? null;
83+
$this->publicAccessBlockConfiguration = isset($input['PublicAccessBlockConfiguration']) ? PublicAccessBlockConfiguration::create($input['PublicAccessBlockConfiguration']) : null;
84+
$this->expectedBucketOwner = $input['ExpectedBucketOwner'] ?? null;
85+
parent::__construct($input);
86+
}
87+
88+
/**
89+
* @param array{
90+
* Bucket?: string,
91+
* ContentMD5?: string|null,
92+
* ChecksumAlgorithm?: ChecksumAlgorithm::*|null,
93+
* PublicAccessBlockConfiguration?: PublicAccessBlockConfiguration|array,
94+
* ExpectedBucketOwner?: string|null,
95+
* '@region'?: string|null,
96+
* }|PutPublicAccessBlockRequest $input
97+
*/
98+
public static function create($input): self
99+
{
100+
return $input instanceof self ? $input : new self($input);
101+
}
102+
103+
public function getBucket(): ?string
104+
{
105+
return $this->bucket;
106+
}
107+
108+
/**
109+
* @return ChecksumAlgorithm::*|null
110+
*/
111+
public function getChecksumAlgorithm(): ?string
112+
{
113+
return $this->checksumAlgorithm;
114+
}
115+
116+
public function getContentMd5(): ?string
117+
{
118+
return $this->contentMd5;
119+
}
120+
121+
public function getExpectedBucketOwner(): ?string
122+
{
123+
return $this->expectedBucketOwner;
124+
}
125+
126+
public function getPublicAccessBlockConfiguration(): ?PublicAccessBlockConfiguration
127+
{
128+
return $this->publicAccessBlockConfiguration;
129+
}
130+
131+
/**
132+
* @internal
133+
*/
134+
public function request(): Request
135+
{
136+
// Prepare headers
137+
$headers = ['content-type' => 'application/xml'];
138+
if (null !== $this->contentMd5) {
139+
$headers['Content-MD5'] = $this->contentMd5;
140+
}
141+
if (null !== $this->checksumAlgorithm) {
142+
if (!ChecksumAlgorithm::exists($this->checksumAlgorithm)) {
143+
throw new InvalidArgument(\sprintf('Invalid parameter "ChecksumAlgorithm" for "%s". The value "%s" is not a valid "ChecksumAlgorithm".', __CLASS__, $this->checksumAlgorithm));
144+
}
145+
$headers['x-amz-sdk-checksum-algorithm'] = $this->checksumAlgorithm;
146+
}
147+
if (null !== $this->expectedBucketOwner) {
148+
$headers['x-amz-expected-bucket-owner'] = $this->expectedBucketOwner;
149+
}
150+
151+
// Prepare query
152+
$query = [];
153+
154+
// Prepare URI
155+
$uri = [];
156+
if (null === $v = $this->bucket) {
157+
throw new InvalidArgument(\sprintf('Missing parameter "Bucket" for "%s". The value cannot be null.', __CLASS__));
158+
}
159+
$uri['Bucket'] = $v;
160+
$uriString = '/' . rawurlencode($uri['Bucket']) . '?publicAccessBlock';
161+
162+
// Prepare Body
163+
164+
$document = new \DOMDocument('1.0', 'UTF-8');
165+
$document->formatOutput = false;
166+
$this->requestBody($document, $document);
167+
$body = $document->hasChildNodes() ? $document->saveXML() : '';
168+
169+
// Return the Request
170+
return new Request('PUT', $uriString, $query, $headers, StreamFactory::create($body));
171+
}
172+
173+
public function setBucket(?string $value): self
174+
{
175+
$this->bucket = $value;
176+
177+
return $this;
178+
}
179+
180+
/**
181+
* @param ChecksumAlgorithm::*|null $value
182+
*/
183+
public function setChecksumAlgorithm(?string $value): self
184+
{
185+
$this->checksumAlgorithm = $value;
186+
187+
return $this;
188+
}
189+
190+
public function setContentMd5(?string $value): self
191+
{
192+
$this->contentMd5 = $value;
193+
194+
return $this;
195+
}
196+
197+
public function setExpectedBucketOwner(?string $value): self
198+
{
199+
$this->expectedBucketOwner = $value;
200+
201+
return $this;
202+
}
203+
204+
public function setPublicAccessBlockConfiguration(?PublicAccessBlockConfiguration $value): self
205+
{
206+
$this->publicAccessBlockConfiguration = $value;
207+
208+
return $this;
209+
}
210+
211+
private function requestBody(\DOMNode $node, \DOMDocument $document): void
212+
{
213+
if (null === $v = $this->publicAccessBlockConfiguration) {
214+
throw new InvalidArgument(\sprintf('Missing parameter "PublicAccessBlockConfiguration" for "%s". The value cannot be null.', __CLASS__));
215+
}
216+
217+
$node->appendChild($child = $document->createElement('PublicAccessBlockConfiguration'));
218+
$child->setAttribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/');
219+
$v->requestBody($child, $document);
220+
}
221+
}

src/Service/S3/src/S3Client.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
use AsyncAws\S3\Input\PutObjectAclRequest;
6363
use AsyncAws\S3\Input\PutObjectRequest;
6464
use AsyncAws\S3\Input\PutObjectTaggingRequest;
65+
use AsyncAws\S3\Input\PutPublicAccessBlockRequest;
6566
use AsyncAws\S3\Input\UploadPartCopyRequest;
6667
use AsyncAws\S3\Input\UploadPartRequest;
6768
use AsyncAws\S3\Result\AbortMultipartUploadOutput;
@@ -101,6 +102,7 @@
101102
use AsyncAws\S3\ValueObject\MultipartUpload;
102103
use AsyncAws\S3\ValueObject\NotificationConfiguration;
103104
use AsyncAws\S3\ValueObject\Part;
105+
use AsyncAws\S3\ValueObject\PublicAccessBlockConfiguration;
104106
use AsyncAws\S3\ValueObject\Tagging;
105107

106108
class S3Client extends AbstractApi
@@ -2891,6 +2893,54 @@ public function putObjectTagging($input): PutObjectTaggingOutput
28912893
return new PutObjectTaggingOutput($response);
28922894
}
28932895

2896+
/**
2897+
* > This operation is not supported for directory buckets.
2898+
*
2899+
* Creates or modifies the `PublicAccessBlock` configuration for an Amazon S3 bucket. To use this operation, you must
2900+
* have the `s3:PutBucketPublicAccessBlock` permission. For more information about Amazon S3 permissions, see Specifying
2901+
* Permissions in a Policy [^1].
2902+
*
2903+
* ! When Amazon S3 evaluates the `PublicAccessBlock` configuration for a bucket or an object, it checks the
2904+
* ! `PublicAccessBlock` configuration for both the bucket (or the bucket that contains the object) and the bucket
2905+
* ! owner's account. If the `PublicAccessBlock` configurations are different between the bucket and the account, Amazon
2906+
* ! S3 uses the most restrictive combination of the bucket-level and account-level settings.
2907+
*
2908+
* For more information about when Amazon S3 considers a bucket or an object public, see The Meaning of "Public" [^2].
2909+
*
2910+
* The following operations are related to `PutPublicAccessBlock`:
2911+
*
2912+
* - GetPublicAccessBlock [^3]
2913+
* - DeletePublicAccessBlock [^4]
2914+
* - GetBucketPolicyStatus [^5]
2915+
* - Using Amazon S3 Block Public Access [^6]
2916+
*
2917+
* [^1]: https://docs.aws.amazon.com/AmazonS3/latest/dev/using-with-s3-actions.html
2918+
* [^2]: https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html#access-control-block-public-access-policy-status
2919+
* [^3]: https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetPublicAccessBlock.html
2920+
* [^4]: https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeletePublicAccessBlock.html
2921+
* [^5]: https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketPolicyStatus.html
2922+
* [^6]: https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html
2923+
*
2924+
* @see https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutPublicAccessBlock.html
2925+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putpublicaccessblock
2926+
*
2927+
* @param array{
2928+
* Bucket: string,
2929+
* ContentMD5?: string|null,
2930+
* ChecksumAlgorithm?: ChecksumAlgorithm::*|null,
2931+
* PublicAccessBlockConfiguration: PublicAccessBlockConfiguration|array,
2932+
* ExpectedBucketOwner?: string|null,
2933+
* '@region'?: string|null,
2934+
* }|PutPublicAccessBlockRequest $input
2935+
*/
2936+
public function putPublicAccessBlock($input): Result
2937+
{
2938+
$input = PutPublicAccessBlockRequest::create($input);
2939+
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'PutPublicAccessBlock', 'region' => $input->getRegion()]));
2940+
2941+
return new Result($response);
2942+
}
2943+
28942944
/**
28952945
* Uploads a part in a multipart upload.
28962946
*

0 commit comments

Comments
 (0)