I'm trying to set a tag on an object in an AWS S3 bucket using the AWS PHP SDK V3. I can read them OK once I manually set, by doing the following:
$objecttagged = $s3client->getObjectTagging([
'Bucket' => 'mybucketname',
'Key' => $myobject['Key'],
]);
I've looked through the docs on AWS and on the web but nothing seems to work. Closest I can figure is:
$result = $s3client->putObject([
'Bucket' => 'mybucketname',
'Key' => $myobject['Key'],
'Tagging' => 'status=ready'
]);
But doing this just creates a version of the object name with zero byte size.
I assume you can add tagging to a buckets object programatically?
Has anyone got a link to docs I've missed or can confirm it can be done?
Thanks
My Answer:
It looks like you are almost there with your code. The issue might be with the format of the tagging parameter. According to the AWS PHP SDK V3 documentation, the Tagging parameter should be an array of key-value pairs.
Here is how you can set tags on an object in an S3 bucket using the AWS PHP SDK V3:
php
$result = $s3client->putObject([
'Bucket' => 'mybucketname',
'Key' => $myobject['Key'],
'Tagging' => [
[
'Key' => 'status',
'Value' => 'ready'
]
]
]);
Make sure to replace 'mybucketname' and $myobject['Key'] with your actual bucket name and object key.
This code should set a tag with key 'status' and value 'ready' on the specified object in the S3 bucket.
Rate this post
5 of 5 based on 4345 votesComments