python - bluemix 上的授权问题 : Python swift client library accessing object storage

标签 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) 

最佳答案

您有多种身份验证端点选择。 您应该指定 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 可以以类似的方式实现。

关于python - bluemix 上的授权问题 : Python swift client library accessing object storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746762/

相关文章:

python - 使用 python/pandas 将数据标准化并绘制为堆积条形图

python - 模块未找到错误 : No module named 'rest_framework' I already installed djangorestframework

java - 应用程序类 'com.sun.xml.messaging.saaj.soap.SOAPPartImpl.:119' 引发的异常

hadoop - ibm-bluemix-节点红色-应用程序未运行,我无法添加分析服务

sql - IBM SQL Query 如何知道我在 CSV 文件中使用的模式?

python - 提高循环 numpy 数组的速度

python - 如何检测一个二维数组是否在另一个二维数组中?

javascript - 如何将用户元数据添加到 IBM Cloud 虚拟设备订单?

go - 将文件上传到Google存储而不将其保存到内存

scala - Spark+Scala App如何对云对象存储进行认证授权?