tencentyun / cos-python-sdk

对象存储服务(Cloud Object Service)Python SDK使用说明
https://www.qcloud.com/doc/product/227/3385
MIT License
20 stars 8 forks source link

cos上传api报错:TypeError: character mapping must return integer, None or unicode #12

Open ajaxhe opened 8 years ago

ajaxhe commented 8 years ago

sdk报错: TypeError: character mapping must return integer, None or unicode

Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "./download_and_upload.py", line 140, in run
    self.upload(data, msg)
  File "./download_and_upload.py", line 118, in upload
    result = cos_api.statFile(bucket, cos_path)
  File "/usr/lib/python2.6/site-packages/qcloud_cos/cos.py", line 314, in statFile
    return self.__stat(bucket, path)
  File "/usr/lib/python2.6/site-packages/qcloud_cos/cos.py", line 327, in __stat
    sign = auth.sign_more(bucket, expired)
  File "/usr/lib/python2.6/site-packages/qcloud_cos/auth.py", line 62, in sign_more
    return self.app_sign(bucket, '', expired)
  File "/usr/lib/python2.6/site-packages/qcloud_cos/auth.py", line 51, in app_sign
    bin = hmac.new(self._secret_key, plain_text, hashlib.sha1)
  File "/usr/lib64/python2.6/hmac.py", line 133, in new
    return HMAC(key, msg, digestmod)
  File "/usr/lib64/python2.6/hmac.py", line 72, in __init__
    self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode

` 主要原因: python 2.6的hmac不支持unicode编码导致: http://bugs.python.org/issue5285 解决方案: 传递给api的参数进行str处理:

cos_app_id = str(info['cos_app_id'])
cos_s_id = str(info['cos_s_id'])
cos_s_key = str(info['cos_s_key'])
bucket = str(info['cos_bucket'])
cos_api = qcloud_cos.Cos(cos_app_id, cos_s_id, cos_s_key)
result = cos_api.statFile(bucket, cos_path)

参考链接: http://stackoverflow.com/questions/20849805/python-hmac-typeerror-character-mapping-must-return-integer-none-or-unicode/31572219