I want to return a binary file which resides in (IBM Cloud) ObjectStorage via HTTP using OpenWhisk in a IBM Cloud Function.
Is this possible? To me it seems OpenWhisk only supports JSON as result to an action.
This is the code I'm using (get_object_storage_file returns binary data):
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'}
Is this about a web-action or a "normal" action?
In general, you can always return a Base64 encoded representation of your binary data, encoded in a JSON object. It is true, that a CloudFunctions action always needs to return a JSON object.
In your specific example, the following could work:
import base64
def main(dict):
binary = get_object_storage_file('data', 'raw.bin')
return {'data':base64.base64encode(binary)}
(untested pseudocode)