|
| 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 query |
| 137 | + $query = []; |
| 138 | + |
| 139 | + // Prepare URI |
| 140 | + $uri = []; |
| 141 | + if (null === $v = $this->bucket) { |
| 142 | + throw new InvalidArgument(\sprintf('Missing parameter "Bucket" for "%s". The value cannot be null.', __CLASS__)); |
| 143 | + } |
| 144 | + $uri['Bucket'] = $v; |
| 145 | + $uriString = '/' . rawurlencode($uri['Bucket']) . '?publicAccessBlock'; |
| 146 | + |
| 147 | + // Prepare Body |
| 148 | + $document = new \DOMDocument('1.0', 'UTF-8'); |
| 149 | + $document->formatOutput = false; |
| 150 | + $this->requestBody($document, $document); |
| 151 | + $body = $document->hasChildNodes() ? $document->saveXML() : ''; |
| 152 | + |
| 153 | + // Headers MUST be prepared AFTER the body, so that Content-MD5 can be calculated. |
| 154 | + $headers = ['content-type' => 'application/xml']; |
| 155 | + if (null !== $this->contentMd5) { |
| 156 | + $headers['Content-MD5'] = $this->contentMd5; |
| 157 | + } |
| 158 | + if (null !== $this->checksumAlgorithm) { |
| 159 | + if (!ChecksumAlgorithm::exists($this->checksumAlgorithm)) { |
| 160 | + throw new InvalidArgument(\sprintf('Invalid parameter "ChecksumAlgorithm" for "%s". The value "%s" is not a valid "ChecksumAlgorithm".', __CLASS__, $this->checksumAlgorithm)); |
| 161 | + } |
| 162 | + $headers['x-amz-sdk-checksum-algorithm'] = $this->checksumAlgorithm; |
| 163 | + } |
| 164 | + if (null !== $this->expectedBucketOwner) { |
| 165 | + $headers['x-amz-expected-bucket-owner'] = $this->expectedBucketOwner; |
| 166 | + } |
| 167 | + |
| 168 | + // Return the Request |
| 169 | + return new Request('PUT', $uriString, $query, $headers, StreamFactory::create($body)); |
| 170 | + } |
| 171 | + |
| 172 | + public function setBucket(?string $value): self |
| 173 | + { |
| 174 | + $this->bucket = $value; |
| 175 | + |
| 176 | + return $this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param ChecksumAlgorithm::*|null $value |
| 181 | + */ |
| 182 | + public function setChecksumAlgorithm(?string $value): self |
| 183 | + { |
| 184 | + $this->checksumAlgorithm = $value; |
| 185 | + |
| 186 | + return $this; |
| 187 | + } |
| 188 | + |
| 189 | + public function setContentMd5(?string $value): self |
| 190 | + { |
| 191 | + $this->contentMd5 = $value; |
| 192 | + |
| 193 | + return $this; |
| 194 | + } |
| 195 | + |
| 196 | + public function setExpectedBucketOwner(?string $value): self |
| 197 | + { |
| 198 | + $this->expectedBucketOwner = $value; |
| 199 | + |
| 200 | + return $this; |
| 201 | + } |
| 202 | + |
| 203 | + public function setPublicAccessBlockConfiguration(?PublicAccessBlockConfiguration $value): self |
| 204 | + { |
| 205 | + $this->publicAccessBlockConfiguration = $value; |
| 206 | + |
| 207 | + return $this; |
| 208 | + } |
| 209 | + |
| 210 | + private function requestBody(\DOMNode $node, \DOMDocument $document): void |
| 211 | + { |
| 212 | + if (null === $v = $this->publicAccessBlockConfiguration) { |
| 213 | + throw new InvalidArgument(\sprintf('Missing parameter "PublicAccessBlockConfiguration" for "%s". The value cannot be null.', __CLASS__)); |
| 214 | + } |
| 215 | + |
| 216 | + $node->appendChild($child = $document->createElement('PublicAccessBlockConfiguration')); |
| 217 | + $child->setAttribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/'); |
| 218 | + $v->requestBody($child, $document); |
| 219 | + |
| 220 | + if (null === $this->contentMd5) { |
| 221 | + $this->contentMd5 = \hash('md5', $document->saveXML($child)); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments