簡體   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