Python For loop从云函数在云存储中写入多个文件
我在python中有一个云函数来调用RESTAPI get。函数响应为传递给函数的每个json文件名返回一个json文件。该函数只需一次调用即可完美运行:Python For loop从云函数在云存储中写入多个文件,python,google-cloud-platform,google-cloud-functions,google-cloud-storage,Python,Google Cloud Platform,Google Cloud Functions,Google Cloud Storage,我在python中有一个云函数来调用RESTAPI get。函数响应为传递给函数的每个json文件名返回一个json文件。该函数只需一次调用即可完美运行: def request_func(x): url = 'https://use4.dm-us.informaticacloud.com/saas/api/v2/'+ x payload_headers = {'content-type': 'application/json','Accept': 'application/js
def request_func(x):
url = 'https://use4.dm-us.informaticacloud.com/saas/api/v2/'+ x
payload_headers = {'content-type': 'application/json','Accept': 'application/json','icSessionId':ricSessionId}
r = requests.get(url,headers=payload_headers)
data = r.json()
return data
def upload_blob(bucket_name, blob_text, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(blob_text)
wf_name = 'JOB_NAME'
data = request_func('workflow/name/'+ wf_name)
rid = data['id']
data = request_func('activity/activityLog?taskId='+rid)
def monitoramento(request):
BUCKET_NAME = 'bucketname'
BLOB_NAME = wf_name
BLOB_STR = json.dumps(data)
upload_blob(BUCKET_NAME, BLOB_STR, BLOB_NAME)
return f'{wf_name} forecast json file success uploaded!'
我需要将几个json文件传递给函数,然后我对函数进行了一些调整,以使用FOR循环:
def request_func(x):
url = 'https://use4.dm-us.informaticacloud.com/saas/api/v2/'+ x
payload_headers = {'content-type': 'application/json','Accept': 'application/json','icSessionId':ricSessionId}
r = requests.get(url,headers=payload_headers)
data = r.json()
return data
def upload_blob(bucket_name, blob_text, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(blob_text)
list_jobs = ['JOB_NAME1','JOB_NAME2','JOB_NAME3']
for wf_name in list_jobs:
data = request_func('workflow/name/'+ wf_name)
rid = data['id']
data = request_func('activity/activityLog?taskId='+rid)
def monitoramento(request):
BUCKET_NAME = 'bucketname'
BLOB_NAME = wf_name
BLOB_STR = json.dumps(data)
upload_blob(BUCKET_NAME, BLOB_STR, BLOB_NAME)
问题是,只有列表的最后一个对象JOB_NAME3 json file被写入bucket!
我做错了什么?
我省略了一些代码。使用@AlexanderCécile技巧,我可以解决我的问题:
def request_func(x):
url = 'https://use4.dm-us.informaticacloud.com/saas/api/v2/'+ x
payload_headers = {'content-type': 'application/json','Accept': 'application/json','icSessionId':ricSessionId}
r = requests.get(url,headers=payload_headers)
data = r.json()
return data
def upload_blob(bucket_name, blob_text, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(blob_text)
list_jobs = ['JOB_NAME1','JOB_NAME2','JOB_NAME3']
for wf_name in list_jobs:
data = request_func('workflow/name/'+ wf_name)
rid = data['id']
data = request_func('activity/activityLog?taskId='+rid)
BUCKET_NAME = 'monitoramento'
BLOB_NAME = wf_name
BLOB_STR = json.dumps(data)
upload_blob(BUCKET_NAME, BLOB_STR, BLOB_NAME)
从哪里调用
monitoramento()
?提示:不要忽略代码。在请求帮助时创建一个最小且可复制的示例。请不要使用所有大写字母来发布您的邮件。你能把它编辑一下,让它看起来不那么像喊叫吗?我建议你参加Stackoverflow之旅:。创建好的编程问题:为什么要在循环中定义函数?!据我所知,该函数从未运行过。@Robertalmeda它不应该运行,你确定吗?其他函数是否可以创建类似的输出?