Error in PutObjectTagging AWS PHP SDK 3.0

0

Seems to be an issue with the s3 PutObjectTagging() function in the PHP SDK 3.0.

The documentation example is:

$result = $client->putObjectTagging([
    'Bucket' => '<string>', // REQUIRED
    'ChecksumAlgorithm' => 'CRC32|CRC32C|SHA1|SHA256',
    'ContentMD5' => '<string>',
    'ExpectedBucketOwner' => '<string>',
    'Key' => '<string>', // REQUIRED
    'RequestPayer' => 'requester',
    'Tagging' => [ // REQUIRED
        'TagSet' => [ // REQUIRED
            [
                'Key' => '<string>', // REQUIRED
                'Value' => '<string>', // REQUIRED
            ],
            // ...
        ],
    ],
    'VersionId' => '<string>',
]);

but when I call it in my application like this:

 $result = $s3Client->PutObjectTagging([ 'Bucket' => $launch_bucket,
                                                    'Key' => $zipfilename,
                                                    'Tagging' => [
                                                        'TagSet' => [
                                                            'Key' => 'wizard',
                                                            'Value' => '1'
                                                        ]
                                                    ]
                                                ]);

I get the rather bizarre error:

Found 2 errors while validating the input provided for the PutObjectTagging operation: 
[Tagging][TagSet][Key] must be an associative array. Found string(6) "wizard" 
[Tagging][TagSet][Value] must be an associative array. Found string(1) "1"

According to the documentation (https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobjecttagging) the Key and Value fields must be strings, not associative arrays ...

Unless I'm missing something really obvious ...

David

dmb0058
asked 7 months ago255 views
2 Answers
0
Accepted Answer

Ahh!!! That's it ... TagSet is an array of arrays inside an array inside an array 🙄 I must have gone code blind after staring at it for so long 😂

dmb0058
answered 7 months ago
0

Hello.

I tried running the code below in my environment, but no error occurred.

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$s3Client = new S3Client([
    'region' => 'ap-northeast-1',
    'version' => '2006-03-01'
]);

$result = $s3Client->putObjectTagging([
    'Bucket' => 'S3-Bucket-Name', // REQUIRED
    'Key' => 'test.yml', // REQUIRED
    'Tagging' => [ // REQUIRED
        'TagSet' => [ // REQUIRED
            [
                'Key' => 'wizard', // REQUIRED
                'Value' => '1' // REQUIRED
            ]
        ]
    ]
]);

I think you will be successful if your code is as below.
Try modifying it to the following code.

$result = $s3Client->PutObjectTagging([ 'Bucket' => $launch_bucket,
                                                    'Key' => $zipfilename,
                                                    'Tagging' => [
                                                        'TagSet' => [
                                                            [
                                                                'Key' => 'wizard',
                                                                'Value' => '1'
                                                            ]
                                                        ]
                                                    ]
                                                ]);
profile picture
EXPERT
answered 7 months ago
  • Hi,

    That's great to hear, thanks for your reply!

    I'm struggling to see what's different though - apart from the S3 initialisation it all looks the same as I have?

    David

  • Comparing your code and mine, there are differences as follows.

    diff my.php your.php
    1c1
    < $result = $s3Client->PutObjectTagging([ 'Bucket' => $launch_bucket,
    ---
    >  $result = $s3Client->PutObjectTagging([ 'Bucket' => $launch_bucket,
    5,8c5,6
    <                                                             [
    <                                                                 'Key' => 'wizard',
    <                                                                 'Value' => '1'
    <                                                             ]
    ---
    >                                                             'Key' => 'wizard',
    >                                                             'Value' => '1'
    
  • My Code:

                                                            'TagSet' => [
                                                                [
                                                                    'Key' => 'wizard',
                                                                    'Value' => '1'
                                                                ]
                                                            ]
    

    Your Code:

                                                            'TagSet' => [
                                                                'Key' => 'wizard',
                                                                'Value' => '1'
                                                            ]
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions