AWS CLI:解析参数“--item”时出错:无效 JSON:

AWS CLI: Error parsing parameter '--item': Invalid JSON:

我正在尝试运行以下命令

aws dynamodb put-item \
--table-name TABLENAME \
--item file://accounts.json \
--profile ACCOUNTID \
--region us-east-1

TABLENAME 和 ACCOUNTID 不是变量。只是擦掉他们的真实姓名。

json 文件看起来像这样

[
{
    "accountid": {"N": "ACCOUNTID"},
    "accountname": {"S": "ACCOUNTNAME"}
},
{
    "accountid": {"N": "ACCOUNTID"},
    "accountname": {"S": "ACCOUNTNAME"}
}
]

此示例的帐户信息也被清理

而且我不断得到

Error parsing parameter '--item': Invalid JSON:

我已经检查了各种验证器上的 JSON,它显示它是正确的。

我也用过下面的json文件

{
    "accountid": {"N": "090697449863"},
    "accountname": {"S": "awc-n-sf-devops"}
}

将一项放入 table 中,效果很好。

我在 json 文件中缺少什么导致错误?

提前致谢。

put-item 功能允许您放置一个项目,而不是多个项目。

对多个项目使用 batch-write-item,或对每个项目调用一次 put-item

好像现在put-item does not support inserting multiple items. You have to use batch-write-item

以下示例可以帮助您了解如何在您的案例中使用 batch-write-item

accounts.json 文件的内容

{
  "test-table": [
    {
      "PutRequest": {
        "Item": {
          "accountid": {
            "S": "100"
          },
          "accountname": {
            "S": "ACCOUNTNAME1"
          }
        }
      }
    },
    {
      "PutRequest": {
        "Item": {
          "accountid": {
            "S": "101"
          },
          "accountname": {
            "S": "ACCOUNTNAME2"
          }
        }
      }
    }
  ]
}

CLI 命令

aws dynamodb batch-write-item --region us-east-1 --profile ACCOUNTID --request-items file://accounts.json

它将插入 accounts.json 文件中提到的所有项目。

下面是在 运行 上述命令之后插入的项目的附加图像:

也有一种使用put-item插入多个项目的方法,但是你需要一个一个地遍历所有项目并将其插入到table(你可以使用任何SDK) .我相信这不是最佳解决方案,因为您会多次调用 put-item API.