2017-03-26 72 views
0

我有一些問題試圖通過python將文件保存到IBM對象存儲。我從bluemix帳戶中複製了以下憑據(詳情在下面省略)。IBM對象存儲與Python

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導入python腳本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對象存儲的新手,所以任何建議都會有所幫助。

謝謝。

+0

你能粘貼錯誤嗎? – RiyaMRoy

+0

https://dal.objectstorage.open.softlayer.com/v1/AUTH_1231413123d74ab89848eb8504e271bd/democontainer/sample.png

禁止

拒絕訪問此資源。

<_io.StringIO at 0x7f6a3a0539d8> – Harris

+0

爲了確認,這是Bluemix Services中提供的基於OpenStack Swift的對象存儲,而不是使用S3 API的雲對象存儲,並且可以通過Bluemix Infrastructure獲得? –

回答

0

您擁有的代碼用於從存儲中讀取對象。

我建議您在Data Science Experience數據導入面板中使用「插入憑據」選項,然後使用swift客戶端來保存和讀取對象存儲中的文件。有了Data Science Experience,您無法從本地硬盤中引用文件,因此我舉出了從網絡中檢索到的圖像示例。

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') 
+0

夠公平的。您選擇的特定圖片託管在偶爾發送堆棧交換的站點上(請參閱[這裏](https://metasmoke.erwaysoftware.com/search?body=menshealth&commit=Search&feedback=&feedback_filter=tp&reason=44&site=&title=&user_rep_direction=% 3E%3D&user_reputation = 0&username =&utf8 =%E2%9C%93&why =)),因此我的評論。 –