skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the help and you are spot on with this answer - works a treat.

    I hadn't added the 'TagSet' => [ part of the script and this seemed to just re-create the file, but with no content to it. Hence the zero byte size.

    Hope this helps others in the future.


  2. Your putObject call doesn’t have a Body, hence you’re uploading a zero-sized object. So, correct that and your object will be uploaded with tags.

    If you want to apply tags after your object has been uploaded then call PutObjectTagging. For example:

    $result = $client->putObjectTagging([
        'Bucket' => 'mybucket',
        'Key' => 'mykey',
        'Tagging' => [
            'TagSet' => [
                [
                    'Key' => 'status',
                    'Value' => 'ready',
                ],
                [
                    'Key' => 'owner',
                    'Value' => 'elvis',
                ],
            ],
        ],
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search