如何确保 DynamoDB putItem 成功

ufu*_*cuk 1 php amazon-web-services amazon-dynamodb

我正在使用 amazon sdk v2 并使用 aws factory for dynamoDB,并且我有一个简单的 putItem 操作,但我不知道如何确保 putItem 成功或失败,因为 putItem 返回一个不包含任何有关操作状态信息的模型。任何想法?这是我的代码

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;
}
Run Code Online (Sandbox Code Playgroud)

}

Jer*_*lom 5

使用适用于 PHP 2.x 的 AWS 开发工具包,您应该假设任何返回而不引发异常的操作都是成功的。对于 DynamoDB,Aws\DynamoDb\Exception\DynamoDbException如果出现错误,将抛出一个(或子类)。此外,对于 DynamoDB,只有您的项目已写入至少 2 个位置,该服务才会响应,从而确保数据的完整性。

此外,通过适用于 PHP 2.x 的 AWS 开发工具包,您可以使用长格式命令语法来访问 Guzzle 请求和响应对象(如果您有兴趣内省它们)。这是一个例子:

$command = $client->getCommand('PutItem', array(/*...params...*/));
$model = $command->getResult(); // Actually executes the request

$request = $command->getRequest();
$response = $command->getResponse();
var_dump($response->isSuccessful());
Run Code Online (Sandbox Code Playgroud)

另请参阅适用于 PHP 的 AWS 开发工具包用户指南的命令和响应模型部分。