繁体   English   中英

使用Python的IBM对象存储

[英]IBM Object Storage with Python

尝试通过python将文件保存到IBM对象存储时遇到一些问题。 我从bluemix帐户复制了以下凭证(以下省略了详细信息)。

credentials = {
  "auth_url": "https://identity.open.softlayer.com",
  "project": <my project>,
  "projectId": <my project id>,
  "region": "dallas",
  "userId": <user id>,
  "username": <user name>,
  "password": <password>,
  "domainId": <domain Id>,
  "domainName": <domain Name>,
  "role": <role>
  }

下面是我用来尝试将文件从io import String保存到容器的python脚本IO导入请求import json

url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
        'password': {'user': {'name': credentials['username'],'domain': {'id': credentials['domainId']},
        'password': credentials['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'}
print(url2)
resp2 = requests.post(url=url2, data=filename, headers=headers2)
print(resp2.text)
return StringIO(resp2.text)

filename = "sample.png"<br>
post_object_storage_file("democontainer", filename)

我似乎通过resp1获得了令牌并获得了url2。 但是,当我打印resp2.text时,收到“禁止”响应。 我是该存储容器的管理员,所以我看不到为什么无法使用此存储容器。

我是IBM对象存储的新手,所以任何建议都将对您有所帮助。

谢谢。

您拥有的代码用于从存储中读取对象。

我建议您使用Data Science Experience数据导入面板中的“插入凭据”选项,然后使用swift客户端保存和读取对象存储中的文件。 借助Data Science Experience,您无法从本地硬盘中引用文件,因此,我举了一个从Web检索图像的示例。

Swift Client具有put_object函数,用于保存对象。

import swiftclient
import keystoneclient.v3 as keystoneclient
from PIL import Image   ## To get image
import requests         ## To get image
from io import BytesIO


credentials = {
   ## Credentials HERE
}

conn = swiftclient.Connection(
key=credentials['password'],
authurl=credentials['auth_url']+"/v3",
auth_version='3',
os_options={
    "project_id": credentials['project_id'],
    "user_id": credentials['user_id'],
    "region_name": credentials['region']})



response = requests.get("URL to image file") ## Change to link to image file
img = Image.open(BytesIO(response.content))

conn.put_object(credentials['container'],"test.jpg",response,content_type='image/jpeg')  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM