如何使用python更新AWS S3中的现有xml文件,
我编写了一个非常简单的python代码来更新现有的xml文件。现在我必须做同样的事情,但在AWS S3中。下面是我的代码,尝试了一点,没有运气。该错误首先在xml_tree=ET.parse(xml_文件)中找到。有经验的想法吗如何使用python更新AWS S3中的现有xml文件,,python,xml,amazon-web-services,amazon-s3,lambda,Python,Xml,Amazon Web Services,Amazon S3,Lambda,我编写了一个非常简单的python代码来更新现有的xml文件。现在我必须做同样的事情,但在AWS S3中。下面是我的代码,尝试了一点,没有运气。该错误首先在xml_tree=ET.parse(xml_文件)中找到。有经验的想法吗 import os import xml.etree.ElementTree as ET import boto3 s3_resource = boto3.resource('s3') bucket = s3.Bucket('3d-app') def
import os
import xml.etree.ElementTree as ET
import boto3
s3_resource = boto3.resource('s3')
bucket = s3.Bucket('3d-app')
def lambda_handler(event, context):
#xml_file = 's3://3d-app/updator/project_detail.xml/'
xml_file = os.path.realpath("project_detail.xml")
xmltree = ET.parse(xml_file)
#print(xmltree)
root = xmltree.getroot()
#print(root)
#print(root.tag)
#root_tag = root.find('root')
project = ET.SubElement(root, "project")
indexpage_path = ET.SubElement(project, "indexpage_path")
description = ET.SubElement(project, "description")
thumbnail_path = ET.SubElement(project, "thumbnail_path")
indexpage_path.text ="$(new to be added) "
description.text = "$(new to be added) "
thumbnail_path.text = "$(new to be added) "
#print ((indexpage_path), (indexpage_path.text))
#print((description), (description.text))
tree = ET.ElementTree(root)
tree.write(xml_file)
return
请将XML的生成与AWS S3中的存储分开 如果XML存储在本地文件中,请使用从文件名()设置内容
如果XML存储在字符串中,请使用从字符串()设置内容
AWS_Region = 'eu-west-1' # use connect_to_region to avoid 301 error
target_bucket = 'your.bucket.com'
conn = boto.s3.connect_to_region(
AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_ACCESS_KEY_SECRET,
is_secure=True, # uncomment if you are not using ssl
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn_s3_r.get_bucket(target_bucket)
k = Key(bucket)
k.key = target_key
#if k.exists(): print "File existing, overwriting anyway: ", target_key
k.set_contents_from_filename(src_file)
k.make_public() #I use this for static website hosted on AWS S3
请将XML的生成与AWS S3中的存储分开 如果XML存储在本地文件中,请使用从文件名()设置内容
如果XML存储在字符串中,请使用从字符串()设置内容
AWS_Region = 'eu-west-1' # use connect_to_region to avoid 301 error
target_bucket = 'your.bucket.com'
conn = boto.s3.connect_to_region(
AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_ACCESS_KEY_SECRET,
is_secure=True, # uncomment if you are not using ssl
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn_s3_r.get_bucket(target_bucket)
k = Key(bucket)
k.key = target_key
#if k.exists(): print "File existing, overwriting anyway: ", target_key
k.set_contents_from_filename(src_file)
k.make_public() #I use this for static website hosted on AWS S3
XML文件似乎不存在。你犯了什么错误?要直接从S3读取,请查看当我添加print(xml_文件)时,它会打印/var/task/project_detail.xml,但错误消息如下:;[Errno 2]没有这样的文件或目录:'/var/task/project_detail.xml':IOError Traceback(最近一次调用):文件“/var/task/myFunction.py”,第14行,在lambda_处理程序xmltree=ET.parse(xml_文件)文件“/usr/lib64/python2.7/xml/etree/ElementTree.py”中,第1182行,在parse tree.parse(source,parser)文件中“/usr/lib64/python2.7/xml/etree/ElementTree.py”,第647行,在parse source=open(source,“rb”)IOError:[Errno 2]没有这样的文件或目录:'/var/task/project_detail.xml'您的代码正在读取本地文件。但是您的代码中没有从S3下载xml文件的地方。您需要将文件从S3下载到本地文件。只有这样您才能打开它。谢谢Matt,,忘了说它必须是AWS中的Lambda函数,所以可能,,,,可能有不同之处t方法..似乎XML文件不存在。您遇到了什么错误?若要直接从S3读取,请查看当我添加打印(XML文件)时,它打印/var/task/project_detail.XML,但错误消息如下;[Errno 2]没有这样的文件或目录:'/var/task/project_detail.XML':IOError Traceback(最后一次调用):文件“/var/task/myFunction.py”,第14行,lambda_处理程序xmltree=ET.parse(xml_文件)文件“/usr/lib64/python2.7/xml/etree/ElementTree.py”,第1182行,parse tree.parse(source,parser)文件“/usr/lib64/python2.7/xml/etree/ElementTree.py”,第647行,parse source=open(source,“rb”)IOError:[错误2]没有这样的文件或目录:'/var/task/project_detail.xml'您的代码正在读取本地文件。但是您的代码中没有从S3下载xml文件的地方。您需要将文件从S3下载到本地文件。只有这样您才能打开它。谢谢Matt,,忘了说它必须是AWS中的Lambda函数,所以可能,,,,可能有不同之处t接近。。