Python Boto3 - 数据未正确写入DynamoDB
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Boto3 - 数据未正确写入DynamoDB相关的知识,希望对你有一定的参考价值。
我有一个XML文件,我正在使用Python解析字符串并写入AWS中的DynamoDB表。标签是<IMAGE_ID>
和<CVSS_FINAL>
。当我遍历和print()
这些值时,它返回所有这些值。但是,当我写入Dynamo时,只写入一行数据。所以,我不明白为什么print()
返回所有内容,但只有一行写入数据存储区。
码:
import boto3
import lxml
from lxml import etree
def WriteItemToTable():
s3 = boto3.resource('s3')
bucket = ‘xxxxxxxxxxxx’
key = 'vuln_data.xml'
dynamo = boto3.client('dynamodb')
obj = s3.Object(‘xxxxxxxxxx’, 'vuln_data.xml')
body = obj.get()['Body'].read()
image_id = etree.fromstring(body).findall('HOST_LIST/HOST/EC2_INFO/IMAGE_ID')
risk_score = etree.fromstring(body).findall('HOST_LIST/HOST/VULN_INFO_LIST/VULN_INFO/CVSS_FINAL')
for el in image_id:
i = el.text
print(i)
for el in risk_score:
j = el.text
print(j)
response = dynamo.put_item(
TableName='ExistingAMI',
Item={
'AMI_ID': {
'S': i
},
'CVSS_SCORE': {
'S': j
},
}
)
WriteItemToTable()
XML:
<HOST_LIST>
<HOST>
<EC2_INFO>
<PUBLIC_DNS_NAME><![CDATA[ec2-xxxxxxxxxxx.compute-1.amazonaws.com]]></PUBLIC_DNS_NAME>
<IMAGE_ID><![CDATA[ami-xxxxxx]]></IMAGE_ID>
</EC2_INFO>
<OPERATING_SYSTEM><![CDATA[Linux x.y]]></OPERATING_SYSTEM>
<VULN_INFO_LIST>
<VULN_INFO>
<QID id="qid_x”>x</QID>
<TYPE>Vuln</TYPE>
<CVSS_FINAL>3.5</CVSS_FINAL>
<RESULT><![CDATA[TLSv1.0 is supported]]></RESULT>
</VULN_INFO>
<VULN_INFO>
<QID id="qid_xxxx">xxxxx</QID>
<CVSS_FINAL>2.1</CVSS_FINAL>
</VULN_INFO>
<VULN_INFO>
<QID id="qid_xxxx">xxxx</QID>
<CVSS_FINAL>4.3</CVSS_FINAL>
<RESULT><![CDATA[TLSv1.0 is supported]]></RESULT>
</VULN_INFO>
</VULN_INFO_LIST>
</HOST>
<HOST>
<EC2_INFO>
<PUBLIC_DNS_NAME><![CDATA[ec2-xxxxxxxxx.compute-1.amazonaws.com]]></PUBLIC_DNS_NAME>
<IMAGE_ID><![CDATA[ami-yyyyyy]]></IMAGE_ID>
</EC2_INFO>
<OPERATING_SYSTEM><![CDATA[Amazon Linux]]></OPERATING_SYSTEM>
<VULN_INFO_LIST>
<VULN_INFO>
<QID id=“x”>x</QID>
<CVSS_FINAL>3.6</CVSS_FINAL>
</VULN_INFO>
</VULN_INFO_LIST>
</HOST>
</HOST_LIST>
print()输出:
ami-xxxxxx
ami-yyyyyy
3.5
3.6
发电机表:
答案
虽然我对DynamoDB一无所知,但是你的Python代码只应传递一个i和j值,因为你的dynamo.put_item
代码块没有嵌套在任何一个for
循环中,因此它们会获取它们最后指定的值。
只需在一个循环中运行image_id和risk_score搜索,这个循环可以嵌套在<HOST>
级别。并考虑xpath()
,lxml
。而且当您导入其方法import lxml
时,不需要etree
调用。
doc = etree.fromstring(body) # PARSE ONLY ONCE
hosts = doc.xpath('//HOST')
for h in hosts:
i = h.xpath('EC2_INFO/IMAGE_ID')[0].text
print(i)
j = h.xpath('VULN_INFO_LIST/VULN_INFO/CVSS_FINAL')[0].text
print(j)
response = dynamo.put_item(
TableName='ExistingAMI',
Item={
'AMI_ID': {
'S': i
},
'CVSS_SCORE': {
'S': j
},
}
)
WriteItemToTable()
# ami-xxxxxx
# 3.5
# ami-yyyyyy
# 3.6
对于多个CVSS_FINAL,使用XPath解析为<CSVSS_FINAL>
然后使用ancestor::*
检索相应的IMAGE_ID
cvss = obj.xpath('//CVSS_FINAL') # ALL CVSS_FINAL NODES
for c in cvss:
i = c.xpath('ancestor::HOST/EC2_INFO/IMAGE_ID')[0].text
print(i)
j = c.text
print(j)
k = c.xpath('concat(following-sibling::RESULT, "")')
print(k)
response = dynamo.put_item( ... )
# ami-xxxxxx
# 3.5
# TLSv1.0 is supported
# ami-xxxxxx
# 2.1
#
# ami-xxxxxx
# 4.3
# TLSv1.0 is supported
# ami-yyyyyy
# 3.6
#
以上是关于Python Boto3 - 数据未正确写入DynamoDB的主要内容,如果未能解决你的问题,请参考以下文章
AWS BOTO3 S3 python - 调用 HeadObject 操作时发生错误(404):未找到