bluemix上的授权问题:Python swift客户端库访问对象存储

时间:2016-06-10 11:11:09

标签: python ibm-cloud object-storage

我正在编写一个使用python swift客户端库访问对象存储的代码。该程序的目的是使用swift客户端API读取,写入和删除对象存储中的CSV文件。我可能知道出了什么问题。我从网站上获得了这段代码。有没有其他方法可以从python访问对象存储?

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) 

2 个答案:

答案 0 :(得分:2)

您可以为身份验证端点选择多个选项。 您应指定auth_version并相应地调整authurl。 只需将'/ v3'附加到authurl即可。

这是一个使用Insert to code - credentials函数的小例子。

# 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')

通过使用python-swiftclient包并自己构建请求,还可以替代requests。看看这个示例笔记本:https://console.ng.bluemix.net/data/notebooks/samples/Precipitation%20Analysis

在“访问对象存储”部分中,函数get_file_content(credentials)可用于读取文件。相应的POST或DELETE可以以类似的方式实现。

答案 1 :(得分:0)

您必须在authurl中指定您正在使用的auth_version。

将v3添加到authurl并取消注释auth_version行。

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