Reputation: 475
I'm using amazon sdk v2 and using aws factory for dynamoDB and I have a simple putItem operation but I have no idea how I can make sure putItem was successful or not because putItem returns a Model which doesnt contain any information about status of operation. Any idea? Here is my code
class DynamoLogger{
protected $client;
protected $tableName;
public function __construct(ServiceBuilder $builder, $tableName)
{
$this->client = $builder->get('dynamodb');
$this->tableName = $tableName;
}
public function log(Request $request)
{
$model = $this->client->putItem(array(
'TableName' => $this->tableName,
'Item' => array(
'cc_id' => array(
'S' => $request->get('cc_id')
),
'date' => array(
'S' => date('Y-m-d H:i:s') . substr((string)microtime(), 1, 8)
),
'tt_id' => array(
'N' => $request->get('tt_id')
),
'action_name' => array(
'S' => $request->get('name')
),
'action_value' => array(
'S' => $request->get('value')
),
'gg_nn' => array(
'S' => $request->get('gg_nn')
),
'ffr_id' => array(
'N' => $request->get('ffr_id')
)
),
'ReturnValues' => 'ALL_OLD'
));
return $model;
}
}
Upvotes: 1
Views: 3510
Reputation: 6527
With the AWS SDK for PHP 2.x, you should assume that any operation that returns without throwing an exception was successful. In the case of DynamoDB, an Aws\DynamoDb\Exception\DynamoDbException
(or subclass) will be thrown if there was an error. Also, in the case of DynamoDB, the service will not respond until your item has been written to at least 2 locations, ensuring the integrity of your data.
Additionally, with the AWS SDK for PHP 2.x, you can use the long-form command syntax in order to have access to the Guzzle Request and Response objects, if you are interested in introspecting them. Here is an example:
$command = $client->getCommand('PutItem', array(/*...params...*/));
$model = $command->getResult(); // Actually executes the request
$request = $command->getRequest();
$response = $command->getResponse();
var_dump($response->isSuccessful());
Please also see the Commands and Response Models sections of the AWS SDK for PHP User Guide.
Upvotes: 5