2
0

kkms.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- encoding:utf-8 -*-
  2. from kscore.session import get_session
  3. import json
  4. # 打印一个 对象 (可忽略该函数)
  5. def printObj(Obj = None,level = 0):
  6. ObjType = type(Obj).__name__;
  7. for key in Obj:
  8. Value = None;
  9. PrintStr = " " * level;
  10. # 判断是否字典
  11. if( ObjType == 'dict' ):
  12. Value = Obj[key];
  13. PrintStr += "[%s]-" % (key);
  14. else:
  15. Value = key;
  16. # 判断当前要打印的变量类型
  17. TmpType = type(Value).__name__;
  18. if( TmpType == 'dict' or TmpType == 'list' or TmpType == 'set' ):
  19. PrintStr += "[%s]:" % ( TmpType , );
  20. print PrintStr;
  21. printObj(Value,level + 1);
  22. else:
  23. PrintStr += "[%s]\n" % ( Value, );
  24. print PrintStr;
  25. # 样例文件入口
  26. if __name__ == "__main__":
  27. s = get_session()
  28. # 设置链接基本参数
  29. client = s.create_client(service_name="kkms",
  30. region_name = "cn-beijing-6",
  31. use_ssl=False,
  32. ks_access_key_id = "Your AK",
  33. ks_secret_access_key="Your SK"
  34. );r
  35. # 参数配置方法
  36. # client.[接口]([参数])
  37. # 如创建kms 的主Key 需要参数
  38. # {
  39. # "KeyUsage" : "EncryptDecrypt",
  40. # "Origin" : "kms",
  41. # "KeyName" : "NoName"
  42. # }
  43. # 则调用
  44. # client.create_key(KeyUsage = "EncryptDecrypt",Origin = "kms",KeyName = "NoName");
  45. # 参数错误,将抛出异常,请查看对应的异常信息,获取正确的参数使用方法
  46. # 也可以参照KMS文档,来设定具体的参数
  47. try:
  48. # 查询 kms 的主Key列表
  49. printObj( client.describe_keys() );
  50. # 创建kms 的主Key
  51. printObj( client.create_key() );
  52. # 修改kms 的主Key
  53. printObj( client.modify_key() );
  54. # 修改kms 的主Key状态
  55. printObj( client.modify_key_state() );
  56. # 删除kms 的主Key
  57. printObj( client.delete_key() );
  58. # 加密
  59. printObj( client.encrypt() );
  60. # 解密
  61. printObj( client.decrypt() );
  62. # 生成数据密钥
  63. printObj( client.generate_data_key());
  64. except BaseException as e:
  65. print e.message;
  66. # 实际调用时,请按照以下方式单独使用 try except 来捕获异常
  67. '''
  68. try:
  69. # 查询 kms 的主Key列表
  70. printObj( client.describe_keys() );
  71. except BaseException as e:
  72. print e.message;
  73. try:
  74. # 创建kms 的主Key
  75. printObj( client.create_key() );
  76. except BaseException as e:
  77. print e.message;
  78. try:
  79. # 修改kms 的主Key
  80. printObj( client.modify_key(KeyId = "Your KeyID") );
  81. except BaseException as e:
  82. print e.message;
  83. try:
  84. # 修改kms 的主Key状态
  85. printObj( client.modify_key_state(KeyId = "Your KeyID") );
  86. except BaseException as e:
  87. print e.message;
  88. try:
  89. # 删除kms 的主Key
  90. printObj( client.delete_key(KeyId = "Your KeyID") );
  91. except BaseException as e:
  92. print e.message;
  93. try:
  94. # 加密
  95. printObj( client.encrypt(KeyId = "Your KeyID") );
  96. except BaseException as e:
  97. print e.message;
  98. try:
  99. # 解密
  100. printObj( client.decrypt(KeyId = "Your KeyID") );
  101. except BaseException as e:
  102. print e.message;
  103. try:
  104. # 生成数据密钥
  105. printObj( client.generate_data_key(KeyId = "Your KeyID"));
  106. except BaseException as e:
  107. print e.message;
  108. '''