使用 PHP AWS SDK 在 DynamoDB 中存储 JSON 文档

2024-05-10php开发问题
22

本文介绍了使用 PHP AWS SDK 在 DynamoDB 中存储 JSON 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我阅读了 文档其中有 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 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17