2

我正在编写一个代码来使用 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) 
4

2 回答 2

2

您可以为身份验证端点提供多种选择。您应该指定auth_version并相应地调整authurl。只需将'/v3'附加到authurl它应该可以工作。

这是一个使用插入到代码 - 凭据功能的小示例。

# 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 可以以类似的方式实现。

于 2016-08-23T11:37:36.257 回答
0

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

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

authurl='https://identity.open.softlayer.com/v3',
auth_version='3',
于 2017-03-22T04:37:36.967 回答