python - 如何在数据科学体验项目中创建到 Bluemix 上的对象存储的连接?

标签 python ibm-cloud object-storage data-science-experience

我正在尝试为一个与项目创建的默认项目不同的项目设置与 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/

相关文章:

ibm-cloud - 使用 Mulesoft (Mule 4) 使用 IBM MQ 时出现身份验证问题

node.js - 如何将文件上传到 Bluemix 中的对象存储(通过 nodejs)

ibm-cloud - IBM Cloud Object Storage - 什么是数据一致性/可见性模型?

python - 基于 Django 类的 View 在 session 中保留 url 参数

python - 在 matplotlib 图例中使用 TeX 表达式 "\tanh"

python - pd.read_csv 添加名为“Unnamed : 0 的列

node.js - 如何使用 NodeJS SDK for IBM Cloud Object Storage 生成预签名链接?

python - 无法在 Sphinx 中获取 TOCTREE 以显示链接

ibm-cloud - 如何删除 IBM Cloud 上的 Watson Assistant 服务中的上下文变量

object-storage - 使用 s3 客户端获取 HTTP 客户端错误