Skip to content

Commit 72cca34

Browse files
authored
Managing object tags (GetObjectTagging/PutObjectTagging/DeleteObjectTagging) (#1453)
* Support Put/Delete/Get ObjectTagging * added documentation * fixed tests * updated from master
1 parent 69c7bff commit 72cca34

17 files changed

+1411
-0
lines changed

docs/clients/s3.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,44 @@ $client->putBucketTagging(
130130
);
131131
```
132132

133+
### Managing object tags
134+
135+
You can associate multiple key-value pairs (tags) with each of your S3 objects, with the ability to change them at any time.
136+
The tags can be used to manage and control access, set up lifecycle rules, customize S3 Storage Class Analysis, and filter CloudWatch metrics.
137+
138+
Learn more at [Simplify your data lifecycle by using object tags with Amazon S3 Lifecycle](https://aws.amazon.com/blogs/storage/simplify-your-data-lifecycle-by-using-object-tags-with-amazon-s3-lifecycle/)
139+
140+
```php
141+
$client->putObjectTagging(
142+
new PutObjectTaggingRequest([
143+
'Bucket' => 'examplebucket',
144+
'Key' => 'baz/HappyFace.jpg',
145+
'Tagging' => new Tagging([
146+
'TagSet' => [
147+
new Tag([
148+
'Key' => 'expire-after-30-days',
149+
'Value' => '1',
150+
])
151+
],
152+
]),
153+
])
154+
);
155+
156+
$client->getObjectTagging(
157+
new GetObjectTaggingRequest([
158+
'Bucket' => 'examplebucket',
159+
'Key' => 'baz/HappyFace.jpg',
160+
])
161+
);
162+
163+
$client->deleteObjectTagging(
164+
new DeleteObjectTaggingRequest([
165+
'Bucket' => 'examplebucket',
166+
'Key' => 'baz/HappyFace.jpg',
167+
])
168+
);
169+
```
170+
133171
## Virtual Hosted-Style Requests
134172

135173
When calling AWS endpoints, AsyncAws uses [Virtual Hosted-Style Requests](https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html):

manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,13 @@
540540
"DeleteBucket",
541541
"DeleteBucketCors",
542542
"DeleteObject",
543+
"DeleteObjectTagging",
543544
"DeleteObjects",
544545
"GetBucketCors",
545546
"GetBucketEncryption",
546547
"GetObject",
547548
"GetObjectAcl",
549+
"GetObjectTagging",
548550
"HeadObject",
549551
"ListBuckets",
550552
"ListMultipartUploads",
@@ -557,6 +559,7 @@
557559
"PutBucketTagging",
558560
"PutObject",
559561
"PutObjectAcl",
562+
"PutObjectTagging",
560563
"UploadPart"
561564
]
562565
},
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
10+
final class DeleteObjectTaggingRequest extends Input
11+
{
12+
/**
13+
* The bucket name containing the objects from which to remove the tags.
14+
*
15+
* When using this action with an access point, you must direct requests to the access point hostname. The access point
16+
* hostname takes the form *AccessPointName*-*AccountId*.s3-accesspoint.*Region*.amazonaws.com. When using this action
17+
* with an access point through the Amazon Web Services SDKs, you provide the access point ARN in place of the bucket
18+
* name. For more information about access point ARNs, see Using access points [^1] in the *Amazon S3 User Guide*.
19+
*
20+
* When you use this action with Amazon S3 on Outposts, you must direct requests to the S3 on Outposts hostname. The S3
21+
* on Outposts hostname takes the form `*AccessPointName*-*AccountId*.*outpostID*.s3-outposts.*Region*.amazonaws.com`.
22+
* When you use this action with S3 on Outposts through the Amazon Web Services SDKs, you provide the Outposts access
23+
* point ARN in place of the bucket name. For more information about S3 on Outposts ARNs, see What is S3 on Outposts
24+
* [^2] in the *Amazon S3 User Guide*.
25+
*
26+
* [^1]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-access-points.html
27+
* [^2]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/S3onOutposts.html
28+
*
29+
* @required
30+
*
31+
* @var string|null
32+
*/
33+
private $bucket;
34+
35+
/**
36+
* The key that identifies the object in the bucket from which to remove all tags.
37+
*
38+
* @required
39+
*
40+
* @var string|null
41+
*/
42+
private $key;
43+
44+
/**
45+
* The versionId of the object that the tag-set will be removed from.
46+
*
47+
* @var string|null
48+
*/
49+
private $versionId;
50+
51+
/**
52+
* The account ID of the expected bucket owner. If the bucket is owned by a different account, the request fails with
53+
* the HTTP status code `403 Forbidden` (access denied).
54+
*
55+
* @var string|null
56+
*/
57+
private $expectedBucketOwner;
58+
59+
/**
60+
* @param array{
61+
* Bucket?: string,
62+
* Key?: string,
63+
* VersionId?: string,
64+
* ExpectedBucketOwner?: string,
65+
* '@region'?: string|null,
66+
* } $input
67+
*/
68+
public function __construct(array $input = [])
69+
{
70+
$this->bucket = $input['Bucket'] ?? null;
71+
$this->key = $input['Key'] ?? null;
72+
$this->versionId = $input['VersionId'] ?? null;
73+
$this->expectedBucketOwner = $input['ExpectedBucketOwner'] ?? null;
74+
parent::__construct($input);
75+
}
76+
77+
public static function create($input): self
78+
{
79+
return $input instanceof self ? $input : new self($input);
80+
}
81+
82+
public function getBucket(): ?string
83+
{
84+
return $this->bucket;
85+
}
86+
87+
public function getExpectedBucketOwner(): ?string
88+
{
89+
return $this->expectedBucketOwner;
90+
}
91+
92+
public function getKey(): ?string
93+
{
94+
return $this->key;
95+
}
96+
97+
public function getVersionId(): ?string
98+
{
99+
return $this->versionId;
100+
}
101+
102+
/**
103+
* @internal
104+
*/
105+
public function request(): Request
106+
{
107+
// Prepare headers
108+
$headers = ['content-type' => 'application/xml'];
109+
if (null !== $this->expectedBucketOwner) {
110+
$headers['x-amz-expected-bucket-owner'] = $this->expectedBucketOwner;
111+
}
112+
113+
// Prepare query
114+
$query = [];
115+
if (null !== $this->versionId) {
116+
$query['versionId'] = $this->versionId;
117+
}
118+
119+
// Prepare URI
120+
$uri = [];
121+
if (null === $v = $this->bucket) {
122+
throw new InvalidArgument(sprintf('Missing parameter "Bucket" for "%s". The value cannot be null.', __CLASS__));
123+
}
124+
$uri['Bucket'] = $v;
125+
if (null === $v = $this->key) {
126+
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
127+
}
128+
$uri['Key'] = $v;
129+
$uriString = '/' . rawurlencode($uri['Bucket']) . '/' . str_replace('%2F', '/', rawurlencode($uri['Key'])) . '?tagging';
130+
131+
// Prepare Body
132+
$body = '';
133+
134+
// Return the Request
135+
return new Request('DELETE', $uriString, $query, $headers, StreamFactory::create($body));
136+
}
137+
138+
public function setBucket(?string $value): self
139+
{
140+
$this->bucket = $value;
141+
142+
return $this;
143+
}
144+
145+
public function setExpectedBucketOwner(?string $value): self
146+
{
147+
$this->expectedBucketOwner = $value;
148+
149+
return $this;
150+
}
151+
152+
public function setKey(?string $value): self
153+
{
154+
$this->key = $value;
155+
156+
return $this;
157+
}
158+
159+
public function setVersionId(?string $value): self
160+
{
161+
$this->versionId = $value;
162+
163+
return $this;
164+
}
165+
}

0 commit comments

Comments
 (0)