0

我在尝试通过 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 StringIO import requests import json 保存到容器中的 python 脚本

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 对象存储的新手,因此任何建议都会有所帮助。

谢谢。

4

1 回答 1

0

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

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

Swift 客户端具有用于保存对象的 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')  
于 2017-03-29T00:04:59.883 回答