当前位置:   article > 正文

C++/Python/Java AWS S3认证

C++/Python/Java AWS S3认证
  1. import hashlib
  2. import hmac
  3. import base64
  4. class s3client:
  5. def __init__(self, restful_server, access_key, secret_key):
  6. self.restful_server = restful_server
  7. self.access_key = access_key
  8. self.secret_key = secret_key
  9. # Note1: here http_headers is a directory like: http_headers["header-value"] = [value1, value2]
  10. # Note2: the canonical_path may need urlencoded(here we don't process it)
  11. def sign(self, method, canonical_path, parameters, http_headers):
  12. if self.secret_key == "":
  13. return # no auth secret; skip signing, e.g. for public read-only buckets
  14. md5 = ""
  15. ctype = ""
  16. xamzdate = False
  17. date = ""
  18. xamz_elems = []
  19. s3ParamsToSign = {"uploadId":True, "partNumber":True}
  20. for k in http_headers.keys():
  21. v = http_headers.get(k)
  22. k = k.lower()
  23. if k == "content-md5":
  24. md5 = v[0]
  25. elif k == "content-type":
  26. ctype = v[0]
  27. elif k == "date":
  28. if xamzdate == False:
  29. date = v[0]
  30. else:
  31. if k.startswith("x-amz-",0,len(k)):
  32. str = k + ":"
  33. for i, val in enumerate(v):
  34. str += val
  35. if i != len(v) -1:
  36. str += ","
  37. xamz_elems.append(str)
  38. if k == "x-amz-date":
  39. xamzdate = True
  40. date = ""
  41. xamz = ""
  42. if len(xamz_elems) > 0:
  43. xamz_elems.sort()
  44. # print xamz_elems
  45. for elem in xamz_elems:
  46. xamz += (elem + "\n")
  47. # print xamz
  48. expires = False
  49. if "Expires" in parameters:
  50. # Query string request authentication alternative
  51. expires = True
  52. date = parameters["Expires"]
  53. parameters["AWSAccessKeyId"] = self.access_key
  54. # process the parameters
  55. signParams = []
  56. for k, v in parameters.items():
  57. if k in s3ParamsToSign and s3ParamsToSign[k] == True:
  58. if v == "":
  59. signParams.append(k)
  60. else:
  61. signParams.append(k + "=" + v)
  62. #print signParams
  63. if len(signParams):
  64. signParams.sort()
  65. canonical_path = canonical_path + "?"
  66. for i, v in enumerate(signParams):
  67. canonical_path += v
  68. if i != len(signParams) -1:
  69. canonical_path += "&"
  70. print(canonical_path)
  71. payload = method + "\n"
  72. payload += md5 + "\n"
  73. payload += ctype + "\n"
  74. payload += date + "\n"
  75. payload += xamz
  76. payload += canonical_path
  77. print("payload: %s" %payload)
  78. #byte_sk = bytearray(self.secret_key, "utf-8")
  79. #hmac_sha = hmac.new(byte_sk, digestmod=hashlib.sha1)
  80. hmac_sha = hmac.new(self.secret_key.encode("utf8"), digestmod=hashlib.sha1)
  81. hmac_sha.update(payload.encode("utf8"))
  82. signature = base64.b64encode(hmac_sha.digest())
  83. print("signature: %s" % signature)
  84. if expires:
  85. parameters["Signature"] = signature
  86. #xqIFY7lyjhiWs0rJ+gx6/Amjo6c=
  87. else:
  88. str = "AWS " + self.access_key + ":" + signature.decode()
  89. http_headers["Authorization"] = [str] #["AWS " + self.access_key + ":" + signature]
  90. #AWS B0KTM947XL7ZG0X36RQ2:TjXdqQa8LykH0uq4KmJXXx8ywPg=
  1. # -*- coding:utf-8 -*-
  2. import s3_client
  3. import hashlib
  4. import datetime
  5. import base64
  6. import urllib
  7. def testSign():
  8. UPLOAD_SERVER = "web.com:443"
  9. access_key = "xxxxx"
  10. secret_key = "xxxx"
  11. client = s3_client.s3client(UPLOAD_SERVER, access_key, secret_key)
  12. # 1) method
  13. method = "GET"
  14. # 2) canonical_path
  15. canonical_path = "/bucket/md5/filename"
  16. # 3) parameters
  17. parameters = {}
  18. #uploadId = "2~QymFaxK5DMs8XkzBSoIEhW - 7 - 1 - TBRm"
  19. #parameters["uploadId"] = "2~QymFaxK5DMs8XkzBSoIEhW-7-1-TBRm"
  20. #parameters["partNumber"] = "1"
  21. parameters["AWSAccessKeyId"] = "96X9UA862J8CHV28Y9UU"
  22. parameters["Expires"] = "1551668789"
  23. #parameters["Signature"] = "1551668789"
  24. str = "hello,world"
  25. # 4) http_headers
  26. http_headers = {}
  27. http_headers["Content-Type"] = ["application/octet-stream"]
  28. #http_headers["Content-Length"] = [len(str)]
  29. hash_md5 = hashlib.md5(str.encode("utf8"))
  30. digest = hash_md5.digest()
  31. #digest = "46ff8464449b5f71b0b6496ceb0b8bf0"
  32. #md5b64 = base64.standard_b64encode(digest)
  33. #print(md5b64)
  34. md5b64 = "NDQ5YjVmNzFiMGI2NDk2Yw=="
  35. #http_headers["Content-MD5"] = [md5b64]
  36. GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
  37. #http_headers["x-amz-date"] = [datetime.datetime.utcnow().strftime(GMT_FORMAT)]
  38. #http_headers["x-amz-date"] = ["Mon, 04 Mar 2019 02:36:27 GMT"]
  39. #http_headers["X-Amz-User-Agent"] = ["aws-sdk-js/2.3.19"]
  40. #print("x-amz-date: %s" % http_headers["x-amz-date"])
  41. #http_headers["x-amz-meta-reviewdby"] = ["jjjj@johnsmith.net", "jjjj@johnsmith.net"]
  42. client.sign(method, canonical_path, parameters, http_headers)
  43. print("canonical_path: %s" % canonical_path)
  44. print("parameters: %s" % parameters)
  45. print("http_headers", http_headers)
  46. testSign(
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/662758
推荐阅读
相关标签
  

闽ICP备14008679号