我想导入一个托管的.xml URL到谷歌云存储格式为.csv



我正在使用云函数来尝试导入一个xml托管的URL,像这样:

from io import BytesIO
from google.cloud import storage
from xml.etree import ElementTree as ET
import urllib.request
import datetime
import os
import wget
import logging
project_id = 'ID'
bucket_name = 'BUCKETNAME'
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
destination_blob_name = 'NAME'+date_string+'.csv'

source_file_name = 'NAMEexport.xml'
def upload_bloburl(bucket_name, source_file_name, destination_blob_name):   
storage_client = storage.Client() 

file = urllib.request.urlopen(source_file_name)
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(ET.tostring(files, encoding='UTF-8',xml_declaration=True, method='xml').decode('UTF-8'),content_type='application/xml')

print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
return f'Success!'

upload_bloburl(bucket_name, source_file_name,destination_blob_name)

My requirements.txt如下:

# Function dependencies, for example:
# package>=version
requests==2.20.0
google-cloud-storage
urllib3
datetime
os
logging
wget

不幸的是,这不起作用。我对Python也不是很熟悉,所以可能我的网站上有一个明显的错误。如果有人能帮我,那就太好了。

我想让它在云存储中保存一个。csv文件。

在Python中缩进是非常重要的。

在你的函数upload_bloburl中,我认为你搞砸了。你应该试试:

def upload_bloburl(bucket_name, source_file_name, destination_blob_name):   
storage_client = storage.Client() 

file = urllib.request.urlopen(source_file_name)
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(ET.tostring(files, encoding='UTF-8',xml_declaration=True, method='xml').decode('UTF-8'),content_type='application/xml')

print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
return f'Success!'