我试图为与该项目创建的默认项目不同的项目建立到Bluemix Object Storage的连接。这是一个问题,因为:

1)当我添加新连接时,我要使用的对象存储实例不在数据服务下。

2)当我添加一个Softlayer Object Storage时,要求输入的凭据是(登录URL,访问密钥和秘密密钥),但是我的实例具有的凭据是(“ auth_url”:“ project”: “ projectId”:“ region”:“ userId”:“用户名”:“密码”:“ domainId”:“ domainName”:“角色”)

3)我有一个很好的占位符对象存储接口,但是我想用另一个实例替换它。

请帮助我访问另一个Bluemix Object Storage实例中的数据,而不是默认情况下附加到项目的实例。

最佳答案

除了@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“,” domainName“:” XXXX“ ,“角色”:“管理员” }

from io import StringIOimport requestsimport jsonimport 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()

谢谢,
查尔斯

关于python - 如何在Data Science Experience Project中创建与Bluemix上的对象存储的连接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44192849/

10-11 21:06