我想在 IBM Cloud Function 中使用 OpenWhisk 通过 HTTP 返回驻留在(IBM Cloud)ObjectStorage 中的二进制文件。
这可能吗?在我看来,OpenWhisk 似乎只支持 JSON 作为操作的结果。
这是我正在使用的代码(get_object_storage_file 返回二进制数据):
import sys
from io import StringIO
import requests
import json
def get_object_storage_file(container, filename):
"""This functions returns a StringIO object containing
the file content from Bluemix Object Storage."""
url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
'password': {'user': {'name': 'member_1feaf9dc308e9d57b5fce8a2424e51cd3f04af17','domain': {'id': '4619da2fa8524beda11c89d2d1969c5b'},
'password': 'nviJ.XXXXXXX.aexT'}}}}}
headers1 = {'Content-Type': 'application/json'}
resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
resp1_body = resp1.json()
for e1 in resp1_body['token']['catalog']:
if(e1['type']=='object-store'):
for e2 in e1['endpoints']:
if(e2['interface']=='public'and e2['region']=='dallas'):
url2 = ''.join([e2['url'],'/', container, '/', filename])
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
resp2 = requests.get(url=url2, headers=headers2)
return StringIO(resp2.text)
def main(dict):
get_object_storage_file('data', 'raw.bin')
return {'greeting':'test'}