Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SQS send message attributes in php

I need to implement sending messages to SQS with attributes. The body of the message is uploading fine, but I have problem with attributes. Message Attributes require Associative array with Name of the Attribute, Data Type, and Value. I got this kind of error :

AWS HTTP error: Client error: 400 InvalidParameterValue (client): The request must contain non-empty message attribute name.

the function for sending messages:

public function uploadMessage(DataTransferObjectInterface $dataTransferObject)
{

    $command = $this->client->getCommand(
        'SendMessage',
        [
            'QueueUrl' => $this->queueUrl->value(),
            'MessageBody' => $dataTransferObject->getBody()->value(),
            'MessageAttributes' => $dataTransferObject->getAttributes(),

        ]
    );

    $this->client->execute($command);

}

function getAttributes() returns $attributes array

& the test where I run the code

   $attributes = [
   'TestName' =>
            [
            'Name'=>'test',
            'DataType' => 'string',
            'Value' => 'string',

        ]
    ];
    $age = array("Peter" => "35", "Ben" => "9", "Joe" => "43");
    $json = json_encode($age);
    $body = Json::get($json);

    $dto = new DataTransferObject($attributes, $body);

    $uploader = new SQSManager($sqsClient, $queueUrl);
    $uploader->uploadMessage($dto);

How does the $attributes array should look like?

like image 524
goatrenz Avatar asked Apr 28 '26 08:04

goatrenz


1 Answers

A fix for this bug is built into version 3.2.1 and greater.

Additionally, your message attributes array doesn't match the format shown in the documentation. Your attributes should look like this:

$attributes = [
    '<attribute name>' => [
        'DataType' => 'String',
        'StringValue' => '<attribute value>',
    ],
];
like image 160
giaour Avatar answered Apr 29 '26 22:04

giaour