我正在尝试为一个与项目创建的默认项目不同的项目设置与 Bluemix 对象存储的连接。这是一个问题,因为:
1) 当我去添加新连接时,我要使用的对象存储实例不在数据服务中。
2) 当我去添加一个 Softlayer 对象存储时,我被要求提供的凭证是(登录 URL、访问 key 和 secret key ),但我的实例的凭证是(“auth_url”:“project":"projectId":"region":"userId":"username":"password":"domainId":"domainName":"role")
3) 我有一个很好的占位符对象存储接口(interface),但我想用另一个实例替换它。
请帮助我访问不同的 Bluemix Object Storage 实例中的数据,而不是默认附加到项目的实例。
最佳答案
除了@Sumit Goyal 回答的内容。 您需要在本地 gpfs 中下载文件,以便使用不支持从 swift 对象存储读取或换句话说仅支持从本地存储/文件系统读取的 api 或库。
objStorCred = {
"auth_url": "https://identity.open.softlayer.com ",
"项目": "object_storage_XXXXX",
"projectId": "XXXXX5a3",
"地区": "达拉斯",
"userId": "XXXXXX98a15e0",
"用户名": "admin_fXXXXX9",
"密码": "XXXXX",
"domainId": "aXXXX5a",
"域名": "XXXX",
“角色”:“管理员”
从 io 导入 StringIO
导入请求
导入 json
将 pandas 导入为 pd
#@hidden_cell
# 此函数访问对象存储中的文件。该定义包含您的凭据。
# 在共享笔记本之前,您可能希望删除这些凭据。
def get_object_storage_file(容器,文件名):
"""这个函数返回一个 StringIO 对象包含
来自 Bluemix 对象存储的文件内容。"""
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()
谢谢, 查尔斯。
关于python - 如何在数据科学体验项目中创建到 Bluemix 上的对象存储的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44192849/