Python 指定类型时,Boto 3 DynamoDB batchWriteItem的属性值类型无效
在尝试对DynamoDB表执行批处理写入项时,Python Boto3遇到了一个奇怪的问题。我正在遵循这一点,并试图写一个单一的项目。表设置正确,我可以通过AWS cli运行批处理写入项,没有问题 假设客户端和DynamoDB设置正确,我运行:Python 指定类型时,Boto 3 DynamoDB batchWriteItem的属性值类型无效,python,amazon-dynamodb,boto3,Python,Amazon Dynamodb,Boto3,在尝试对DynamoDB表执行批处理写入项时,Python Boto3遇到了一个奇怪的问题。我正在遵循这一点,并试图写一个单一的项目。表设置正确,我可以通过AWS cli运行批处理写入项,没有问题 假设客户端和DynamoDB设置正确,我运行: client.batch_write_item(RequestItems={ "myTable": [ { "PutRequest": { "Item": {
client.batch_write_item(RequestItems={
"myTable": [
{
"PutRequest": {
"Item": {
"name": {
"S": "hello"
},
"value": {
"S": "world"
}
}
}
}
]
})
我得到以下错误:
botocore.exceptions.ClientError:调用BatchWriteItem操作时发生错误(ValidationException):属性值类型无效
如果更改,请删除类型并运行:
client.batch_write_item(RequestItems={
"myTable": [
{
"PutRequest": {
"Item": {
"name": "hello",
"value": "world"
}
}
}
]
})
它按预期工作
我需要使用之前的格式,该格式遵循文档,并且与AWS cli兼容
文档是否有误,或者我错过了配置设置、版本问题或其他问题?这也让我明白了,看起来您使用的是DynamoDB资源,而不是客户端。它们都提供相同的功能,但其作用略有不同。以下是您要查找的内容: 除此之外,这些文件仍然相当不清楚。以下是我的发现:
- 使用资源时(您当前正在执行的操作),可以指定非关键属性的类型,不能指定关键属性的类型
- 使用客户机时(另一个选项),必须指定所有属性的类型
resource = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')
mytable = resource.create_table(
TableName='mytable',
KeySchema=[{ 'AttributeName': 'name', 'KeyType': 'HASH' }],
AttributeDefinitions=[{ 'AttributeName': 'name', 'AttributeType': 'S' }],
ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }
)
try:
resource.batch_write_item(RequestItems={
'mytable': [{ 'PutRequest': { 'Item': {
'name': { 'S': 'myname' },
'value': { 'S': 'myvalue' }
}}}]
})
print(f'resource, specify all types : write succeeded.')
except Exception as e:
print(f'resource, specify all types : write failed: {e}')
try:
resource.batch_write_item(RequestItems={
'mytable': [{ 'PutRequest': { 'Item': {
'name': 'myname',
'value': { 'S': 'myvalue' }
}}}]
})
print(f'resource, specify value only: write succeeded.')
except Exception as e:
print(f'resource, specify value only: write failed: {e}')
try:
resource.batch_write_item(RequestItems={
'mytable': [{ 'PutRequest': { 'Item': {
'name': 'myname',
'value': 'myvalue'
}}}]
})
print(f'resource, specify none : write succeeded.')
except Exception as e:
print(f'resource, specify none : write failed: {e}')
输出
resource, specify all types : write failed:
An error occurred (ValidationException) when calling the BatchWriteItem operation: Invalid attribute value type
resource, specify value only: write succeeded.
resource, specify none : write succeeded.
client, specify all types : write succeeded.
client, specify value only: write failed: Parameter validation failed:
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
client, specify none : write failed: Parameter validation failed:
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.value, value: myvalue, type: <class 'str'>, valid types: <class 'dict'>
然后使用DynamoDB客户机(用客户机替换上面的所有“资源”)
输出
resource, specify all types : write failed:
An error occurred (ValidationException) when calling the BatchWriteItem operation: Invalid attribute value type
resource, specify value only: write succeeded.
resource, specify none : write succeeded.
client, specify all types : write succeeded.
client, specify value only: write failed: Parameter validation failed:
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
client, specify none : write failed: Parameter validation failed:
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.value, value: myvalue, type: <class 'str'>, valid types: <class 'dict'>
客户端,指定所有类型:写入成功。
客户端,仅指定值:写入失败:参数验证失败:
参数RequestItems.mytable[0]的类型无效。PutRequest.Item.name,值:myname,类型:,有效类型:
客户端,指定无:写入失败:参数验证失败:
参数RequestItems.mytable[0]的类型无效。PutRequest.Item.name,值:myname,类型:,有效类型:
参数RequestItems.mytable[0]的类型无效。PutRequest.Item.value,值:myvalue,类型:,有效类型:
你说得对,我们使用的是资源而不是客户端。