2016-06-10 5 views
2

python swift 클라이언트 라이브러리를 사용하여 객체 저장소에 액세스하는 코드를 작성하고 있습니다. 이 프로그램의 목적은 신속 클라이언트 API를 사용하여 객체 저장소에서 CSV 파일을 읽고 쓰고 삭제하는 것입니다. 나는 무엇이 잘못 될지 알 수 있습니다. 웹 사이트에서이 코드를 받았습니다. 파이썬에서 객체 저장소에 접근하는 다른 방법이 있습니까?bluemix의 권한 문제 : 객체 저장소에 액세스하는 Python swift 클라이언트 라이브러리

import swiftclient.client 
from keystoneclient import client 
print 'i am fine' 
conn = swiftclient.Connection(key="password", 
authurl='https://identity.open.softlayer.com', 
#auth_version='3', 
os_options={"project_id": 'project-id', 
"user_id": 'user_id', 
"region_name": "dallas"}) 

container_name = 'test' 

# File name for testing 
file_name = 'result.csv' 

# Create a new container 
conn.put_container(container_name) 
print "nContainer %s created successfully." % container_name 

ERROR:swiftclient:Auth GET failed: https://identity.open.softlayer.com 300 Multiple Choices 
Traceback (most recent call last): 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 1553, in _retry 
    self.url, self.token = self.get_auth() 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 1507, in get_auth 
    timeout=self.timeout) 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 593, in get_auth 
    timeout=timeout) 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 468, in get_auth_1_0 
    http_status=resp.status, http_reason=resp.reason) 
ClientException: Auth GET failed: https://identity.open.softlayer.com 300 Multiple Choices 

ClientException       Traceback (most recent call last) 
<ipython-input-5-3708d247f0cc> in <module>() 
    15 
    16 # Create a new container 
---> 17 conn.put_container(container_name) 
    18 print "nContainer %s created successfully." % container_name 
    19 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in put_container(self, container, headers, response_dict) 
    1641   """Wrapper for :func:`put_container`""" 
    1642   return self._retry(None, put_container, container, headers=headers, 
-> 1643       response_dict=response_dict) 
    1644 
    1645  def post_container(self, container, headers, response_dict=None): 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in _retry(self, reset_func, func, *args, **kwargs) 
    1551    try: 
    1552     if not self.url or not self.token: 
-> 1553      self.url, self.token = self.get_auth() 
    1554      self.http_conn = None 
    1555     if self.service_auth and not self.service_token: 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth(self) 
    1505           cacert=self.cacert, 
    1506           insecure=self.insecure, 
-> 1507           timeout=self.timeout) 
    1508   return self.url, self.token 
    1509 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth(auth_url, user, key, **kwargs) 
    591           cacert=cacert, 
    592           insecure=insecure, 
--> 593           timeout=timeout) 
    594  elif auth_version in AUTH_VERSIONS_V2 + AUTH_VERSIONS_V3: 
    595   # We are handling a special use case here where the user argument 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth_1_0(url, user, key, snet, **kwargs) 
    466   raise ClientException('Auth GET failed', http_scheme=parsed.scheme, 
    467        http_host=conn.host, http_path=parsed.path, 
--> 468        http_status=resp.status, http_reason=resp.reason) 
    469  if snet: 
    470   parsed = list(urlparse(url)) 

***ClientException: Auth GET failed: https://identity.open.softlayer.com 300 Multiple Choices*** 

*****With auth_version enabled:***** 
ERROR:swiftclient:Authorization Failure. Authorization failed: The resource could not be found. (HTTP 404) 
Traceback (most recent call last): 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 1553, in _retry 
    self.url, self.token = self.get_auth() 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 1507, in get_auth 
    timeout=self.timeout) 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 617, in get_auth 
    auth_version=auth_version) 
    File "/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.py", line 543, in get_auth_keystone 
    raise ClientException('Authorization Failure. %s' % err) 
ClientException: Authorization Failure. Authorization failed: The resource could not be found. (HTTP 404) 
i am fine 
--------------------------------------------------------------------------- 
ClientException       Traceback (most recent call last) 
<ipython-input-21-d0874a2c5851> in <module>() 
    15 
    16 # Create a new container 
---> 17 conn.put_container(container_name) 
    18 print "nContainer %s created successfully." % container_name 
    19 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in put_container(self, container, headers, response_dict) 
    1641   """Wrapper for :func:`put_container`""" 
    1642   return self._retry(None, put_container, container, headers=headers, 
-> 1643       response_dict=response_dict) 
    1644 
    1645  def post_container(self, container, headers, response_dict=None): 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in _retry(self, reset_func, func, *args, **kwargs) 
    1551    try: 
    1552     if not self.url or not self.token: 
-> 1553      self.url, self.token = self.get_auth() 
    1554      self.http_conn = None 
    1555     if self.service_auth and not self.service_token: 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth(self) 
    1505           cacert=self.cacert, 
    1506           insecure=self.insecure, 
-> 1507           timeout=self.timeout) 
    1508   return self.url, self.token 
    1509 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth(auth_url, user, key, **kwargs) 
    615            insecure=insecure, 
    616            timeout=timeout, 
--> 617            auth_version=auth_version) 
    618  else: 
    619   raise ClientException('Unknown auth_version %s specified.' 

/gpfs/fs01/user/s1ab-0eb9620648b3ce-07c8d52a704f/.local/lib/python2.7/site-packages/swiftclient/client.pyc in get_auth_keystone(auth_url, user, key, os_options, **kwargs) 
    541   raise ClientException(msg) 
    542  except exceptions.AuthorizationFailure as err: 
--> 543   raise ClientException('Authorization Failure. %s' % err) 
    544  service_type = os_options.get('service_type') or 'object-store' 
    545  endpoint_type = os_options.get('endpoint_type') or 'publicURL' 

ClientException: Authorization Failure. Authorization failed: The resource could not be found. (HTTP 404) 
+0

'auth_version'이 주석 처리 된 이유는 무엇입니까? 선택할 수있는 여러 가지 선택 사항이 있기 때문에 GET이 실패했다는 오류로부터 꽤 분명해 보입니다. – joe

답변

2

인증 끝점에는 여러 가지 선택 항목이 있습니다. auth_version을 지정하고 그에 따라 authurl을 조정해야합니다. authurl에 '/ v3'을 추가하면 제대로 작동합니다.

다음은 코드에 삽입 - 자격 증명 기능을 사용하는 작은 예제입니다.

# Insert to code - credentials 
credentials = { 
    'auth_url':'https://identity.open.softlayer.com', 
    'project':'xxx', 
    'project_id':'xxx', 
    'region':'dallas', 
    'user_id':'xxx', 
    'domain_id':'xxx', 
    'domain_name':'xxx', 
    'username':'xxx', 
    'password':"""xxx""", 
    'filename':'precipitation.csv', 
    'container':'notebooks', 
    'tenantId':'xxx' 
} 

# read CSV file from Object Storage 
from StringIO import StringIO 
import swiftclient.client as swiftclient 
import pandas as pd 

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']}) 

obj = conn.get_object(credentials['container'], credentials['filename']) 
data_df = pd.read_csv(StringIO(obj[1])) 
data_df.head() 

# Create a new container 
container_name = "new_container" 
conn.put_container(container_name) 

# Write CSV file to Object Storage 
import csv 
conn.put_object(credentials['container'], "precipitation_2.csv", data_df.to_csv(quoting=csv.QUOTE_ALL, index=False), content_type='text') 

이 또한 requests 패키지를 사용하여 python-swiftclient의 대안이며, 요청 자신을 구축 할 수 있습니다. 이 샘플 노트북을 살펴보십시오. https://console.ng.bluemix.net/data/notebooks/samples/Precipitation%20Analysis

"Access Object Storage"섹션에서 파일을 읽는 데 get_file_content(credentials) 함수를 사용할 수 있습니다. 해당 POST 또는 DELETE는 유사한 f 식으로 구현 될 수 있습니다.

+0

안녕하세요, Sven, 감사합니다. 이 코드는 한 가지 예외로 작동했습니다. obj = conn.get_object ('Containername', 'filename') –

+0

정확히 작동하지 않습니다. 개체가 반환되지 않거나 오류 메시지가 표시됩니까? –

0

authurl에서 사용중인 auth_version을 지정해야합니다.

auth_l에 v3을 추가하고 auth_version 행의 주석 처리를 제거하십시오.

authurl='https://identity.open.softlayer.com/v3', 
auth_version='3',