2017-03-26 8 views
0

ファイルをIBMオブジェクトストレージにpythonで保存しようとするといくつか問題があります。私はbluemixアカウントから以下の資格情報をコピーしました(詳細は以下省略)。IBM Object Storage with 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

これはOpenStack Swiftベースのオブジェクトストレージで、Bluemix Servicesで利用でき、S3 APIを使用するCloud Object Storageではなく、Bluemix Infrastructureから利用できますか? –

答えて

0

コードは、ストレージからオブジェクトを読み取るためのコードです。

データサイエンスエクスペリエンスデータインポートパネルで「資格情報を挿入する」オプションを使用して、スウィフトクライアントを使用してオブジェクトストレージからファイルを保存および読み込むことをお勧めします。データサイエンスエクスペリエンスでは、ローカルのハードディスクからファイルを参照することはできません。したがって、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') 
+0

十分に公正です。あなたが選んだ特定のイメージは、時々Stack Exchangeをスパムするサイトでホストされます([here](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 =))、したがって私のコメント。 –

関連する問題