除了@Sumit Goyal 回答的内容。您需要在本地 gpfs 中下载文件,以便使用不支持从 swift 对象存储读取或换句话说仅支持从本地存储/文件系统读取的 api 或库。
objStorCred = {
“auth_url”:“ https://identity.open.softlayer.com ”,
“project”:“object_storage_XXXXX”,
“projectId”:“XXXXX5a3”,
“region”:“dallas”,
“userId”:“XXXXXX98a15e0”,
“用户名”:“admin_fXXXXX9”,
“密码”:“XXXXX”,
“domainId”:“aXXXX5a”,
“域名”:“XXXX”,
“角色”:“admin”
}
from io import StringIO
import requests
import json
import pandas as pd
# @hidden_cell
# This function accesses a file in your Object Storage. The definition contains your credentials.
# You might want to remove those credentials before you share your notebook.
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': objStorCred['username'],'domain': {'id': objStorCred['domainId']},
'password': objStorCred['password']}}}}}
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 resp2
请注意,我们获取的是响应对象,而不是获取 stringIO 对象。
现在您可以使用中间本地存储来存储 .mat 文件。
然后调用这个函数。
r = get_object_storage_file("containerr1", "example.mat")
with open('example.mat', 'wb') as file:
file.write(r.content)
现在使用 h5py 读取文件。您可能需要使用 pip install h5py 安装 h5py。
import h5py
f = h5py.File('example.mat')
f.keys()
谢谢,查尔斯。