DynamoDb - Integer无法转换为String

pri*_*ime 1 amazon-dynamodb

我是dynamoDB的新手,我正在努力填充我的DynamoDB表,我已经按如下方式定义了表:

    $response = $DynamoDb->createTable([
        'TableName' => 'products',
        'AttributeDefinitions' => [
            [
                'AttributeName' => 'product_code',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'created_at',
                'AttributeType' => 'N'
            ],
        ],
        'KeySchema' => [
            [
                'AttributeName' => 'product_code',
                'KeyType' => 'HASH' 
            ],
            [
                'AttributeName' => 'created_at',
                'KeyType' => 'RANGE' 
            ]
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'    => 5,
            'WriteCapacityUnits' => 6
        ]
    ]);
Run Code Online (Sandbox Code Playgroud)

并填充使用

    $result = $DynamoDb->putItem([
      'TableName' => 'products',
        'Item' => [
          'product_code' => ['N'  =>  (int)$row[0]],
          'created_at' => ['N' => (int)strtotime("now")]
        ]
    ]);
Run Code Online (Sandbox Code Playgroud)

我一直在收到错误

Integer can not be converted to an String
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉新手.非常感谢.

Eya*_* Ch 7

你应该插入发电机str值.如果'N'是行的类型,dynamo会将其转换为整数

"N": "string", 

 $result = $DynamoDb->putItem([
          'TableName' => 'products',
            'Item' => [
              'product_code' => ['N'  =>  (str)$row[0]],
              'created_at' => ['N' => (str)strtotime("now")]
            ]
        ]);
Run Code Online (Sandbox Code Playgroud)

来自documantaion:

Item

Each element in the Item map is an AttributeValue object.

**Type: String to AttributeValue object map**
Run Code Online (Sandbox Code Playgroud)