我阅读了 文档其中有 PHP 示例使用 AWS 开发工具包在 dynamicDB 表中插入数据.但是,这是针对表格数据的.我正在尝试插入 JSON 数据,即键值对,其中 value 是 JSON 文档.我该怎么做?
I read the documentation where there are PHP examples to insert data in a dynamicDB table using AWS SDK. However this is for tabular data. I am trying to insert JSON data i.e. Key Value pair where value is a JSON document. How do I do that ?
我尝试了文档中的以下代码,但除非 value 是数组,否则它不起作用.
I tried the following code from the doc but it does not work unless value is an array.
<?php
require '/home/ubuntu/vendor/autoload.php';
use AwsDynamoDbDynamoDbClient;
$client = DynamoDbClient::factory(array(
'profile' => 'default',
'region' => 'ap-southeast-1',
'version' => '2012-08-10'
));
$id = "key";
$value = '{"subKey":"value"}'
$result = $client->putItem(array(
'TableName' => 'myTable',
'Item' => array(
'key' => $value
)
));
它给了我以下错误
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Found 2 errors while validating the input provided for the PutItem operation: [Item][key] must be an associative array. Found string(1) "2" [Item][userId] must be an associative array. Found string(18) "{"subKey":"value"}"' in /home/ubuntu/vendor/aws/aws-sdk-php/src/Api/Validator.php:38 Stack trace: #0 /home/ubuntu/vendor/aws/aws-sdk-php/src/Middleware.php(78): AwsApiValidator->validate('PutItem', Object(AwsApiStructureShape), Array) #1 /home/ubuntu/vendor/aws/aws-sdk-php/src/AwsClient.php(208): AwsMiddleware::Aws{closure}(Object(AwsCommand)) #2 /home/ubuntu/vendor/aws/aws-sdk-php/src/AwsClient.php(202): AwsAwsClient->executeAsync(Object(AwsCommand)) #3 /home/ubuntu/vendor/aws/aws-sdk-php/src/AwsClient.php(167): AwsAwsClient->execute(Object(AwsCommand)) #4 /var/www/html/dynamoDB.php(25): AwsAwsClient->__call('putItem', Array) #5 /var/www/html/dynamoDB.php(25): AwsDynamoDbDynamoDbClient->putItem(Array) #6 {main} thrown in /home/ubuntu/vendor/aws/aws-sdk-php/src/Api/Validator.php on line 38
DynamoDB 要求您在请求中指定 AttributeValue 类型.这是来自 https://docs 的示例.aws.amazon.com/aws-sdk-php/v2/guide/service-dynamodb.html:
DynamoDB requires you specify the AttributeValue types with the request. Here is the example from https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-dynamodb.html:
$result = $client->putItem(array(
'TableName' => 'errors',
'Item' => array(
'id' => array('N' => '1201'),
'time' => array('N' => $time),
'error' => array('S' => 'Executive overflow'),
'message' => array('S' => 'no vacant areas')
)
));
对于您的示例,请尝试添加 DynamoDB 类型:
For your example try adding DynamoDB types:
$result = $client->putItem(array(
'TableName' => 'myTable',
'Item' => array(
'key' => array('S' => $value)
)
));
这里的S"可以替换为N"、B"、SS"、NS"、BS"、M"、L"或BOOL":https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html#DataModel.DataTypes
Where 'S' could be replaced with 'N', 'B', 'SS', 'NS', 'BS', 'M', 'L', or 'BOOL' as defined here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html#DataModel.DataTypes
这篇关于使用 PHP AWS SDK 在 DynamoDB 中存储 JSON 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!