[英]Upload a modified XML file to google cloud storage after editting it with ElementTree (python)
I've modified a piece of code for merging two or more xml files into one.我修改了一段代码,用于将两个或多个 xml 文件合并为一个。 I got it working locally without using or storing files on google cloud storage.
我让它在本地工作,而无需在谷歌云存储上使用或存储文件。
I'd like to use it via cloud functions, which seems to work mostly fine, apart from uploading the final xml file to google cloud storage.我想通过云功能使用它,除了将最终的 xml 文件上传到谷歌云存储之外,这似乎大部分工作正常。
import os
import wget
import logging
from io import BytesIO
from google.cloud import storage
from xml.etree import ElementTree as ET
def merge(event, context):
client = storage.Client()
bucket = client.get_bucket('mybucket')
test1 = bucket.blob("xml-file1.xml")
inputxml1 = test1.download_as_string()
root1 = ET.fromstring(inputxml1)
test2 = bucket.blob("xml-file2.xml")
inputxml2 = test2.download_as_string()
root2 = ET.fromstring(inputxml2)
copy_files = [e for e in root1.findall('./SHOPITEM')]
src_files = set([e.find('./SHOPITEM') for e in copy_files])
copy_files.extend([e for e in root2.findall('./SHOPITEM') if e.find('./CODE').text not in src_files])
files = ET.Element('SHOP')
files.extend(copy_files)
blob = bucket.blob("test.xml")
blob.upload_from_string(files)
Ive tried the functions.write and.tostring but unsuccessfully.我尝试了 functions.write 和 .tostring 但没有成功。
Sorry for the incomplete question.抱歉,问题不完整。 I've already found a solution and I cant recall the error message I got.
我已经找到了解决方案,但我不记得收到的错误消息。 Here is my solution:
这是我的解决方案:
blob.upload_from_string(ET.tostring(files, encoding='UTF-8',xml_declaration=True, method='xml').decode('UTF-8'),content_type='application/xml')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.