简体   繁体   English

使用 ElementTree (python) 编辑后将修改后的 XML 文件上传到谷歌云存储

[英]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.

相关问题 如何将文件上传到 Python 3 上的 Google Cloud Storage? - How to upload a file to Google Cloud Storage on Python 3? Python - 返回谷歌云存储 - 返回文件 - Python - Return Google Cloud Storage - Return a File 谷歌云存储从 Python 生成器上传 - Google Cloud Storage streaming upload from Python generator 在 Python/Django 中从 Google Cloud Storage/ Buckets 上传和检索文件 - Upload and retrieve files from Google Cloud Storage/ Buckets in Python/Django Google Cloud Storage XML 文件转换为 CSV 或 JSON 格式 - Google Cloud Storage XML file Conversion to CSV or JSON format 如何 stream 将 CSV 数据上传到谷歌云存储(Python) - How to stream upload CSV data to Google Cloud Storage (Python) 如何使用 Python API 在 Google Cloud Storage 上上传文件夹 - How to upload folder on Google Cloud Storage using Python API 如何将 dataframe 上传到 Python 3 上的 Google Cloud Storage(bucket)? - How to upload a dataframe to Google Cloud Storage(bucket) on Python 3? 如何将 JSON 文件上传到 Google Cloud Storage? - How can I upload JSON file to Google Cloud Storage? 使用 Python 将 csv 大文件上传到云存储 - Upload large csv file to cloud storage using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM