【问题标题】:IBM Object Storage with Python使用 Python 的 IBM 对象存储
【发布时间】:2017-03-31 08:15:53
【问题描述】:

我在尝试通过 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>
  }

下面是我用来尝试将文件保存到容器的 python 脚本 从 io 导入 StringIO 导入请求 导入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 对象存储的新手,因此任何建议都会有所帮助。

谢谢。

【问题讨论】:

  • 你能粘贴错误吗?
  • dal.objectstorage.open.softlayer.com/v1/…

    禁止

    对此资源的访问被拒绝。

    <_io.stringio at>
  • 确认一下,这是 Bluemix Services 中可用的基于 OpenStack Swift 的对象存储,而不是使用 S3 API 且可通过 Bluemix Infrastructure 获得的云对象存储?
  • 尼克。在我看来,它是基于 OpenStack Swift 的对象存储。为了添加更多信息,我正在测试他们的数据科学经验(jupyter notebook)平台,我使用的是 python 3.5。

标签: python python-3.x ibm-cloud object-storage


【解决方案1】:

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

我建议您使用 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')  

【讨论】:

  • 很公平。您选择的特定图像托管在偶尔向 Stack Exchange 发送垃圾邮件的网站上(请参阅 here),因此我发表了评论。
猜你喜欢
  • 2018-07-30
  • 2018-12-03
  • 2021-01-06
  • 2021-09-21
  • 2021-12-14
  • 1970-01-01
  • 2018-08-19
  • 2020-01-25
  • 2021-02-15
相关资源
最近更新 更多