简体   繁体   中英

Trouble tagging object in S3 with php SDK 3

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

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',
            ],
        ],
    ],
]);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM