cstor-foundcation.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # 1、 SQS——lambda
  2. import json
  3. import boto3
  4. import time
  5. import pymysql
  6. def updata_db(m_key,m_value):
  7. conn2 = pymysql.connect(
  8. host='a.xdc.ink',
  9. port=3306,
  10. user='root',
  11. passwd='xclouds12',
  12. db='cmd',
  13. charset='utf8mb4')
  14. cs2 = conn2.cursor()
  15. select_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{m_key}", "{m_value}");'
  16. cs2.execute(select_sql)
  17. conn2.commit()
  18. cs2.close()
  19. conn2.close()
  20. def lambda_handler(event, context):
  21. # print(event)
  22. time.sleep(3)
  23. body = event['Records'][0]['body']
  24. m_key = event['Records'][0]['messageAttributes']['mykey']['stringValue']
  25. # print("FORMSQS:" + m_key + ":" + body)
  26. ddb= boto3.client('dynamodb')
  27. table_name = 'found'
  28. ddb.update_item(
  29. TableName=table_name,
  30. ExpressionAttributeValues={
  31. ':y': {
  32. 'S': body,
  33. }
  34. },
  35. Key={
  36. 'm_key': {
  37. 'S': m_key,
  38. }
  39. },
  40. UpdateExpression='SET sqs = :y',
  41. ReturnValues="UPDATED_NEW"
  42. )
  43. res = ddb.get_item(
  44. Key={
  45. 'm_key': {
  46. 'S': m_key,
  47. }
  48. },
  49. TableName=table_name,)
  50. lambda_code=res['Item']['lambda']['S']
  51. https_code=res['Item']['https']['S']
  52. sqs_code=res['Item']['sqs']['S']
  53. order=res['Item']['order']['S']
  54. # print(order,https,lambdaa,sqs)
  55. if "012" == order:
  56. valueb = sqs_code+https_code+lambda_code
  57. valuea = valueb.replace("\n","")
  58. # print(valuea,keya)
  59. updata_db(m_key,valuea)
  60. elif "021" == order:
  61. valueb = sqs_code+lambda_code+https_code
  62. valuea = valueb.replace("\n","")
  63. updata_db(m_key,valuea)
  64. elif "102" == order:
  65. valueb = https_code+sqs_code+lambda_code
  66. valuea = valueb.replace("\n","")
  67. updata_db(m_key,valuea)
  68. elif "120" == order:
  69. valueb = https_code+lambda_code+sqs_code
  70. valuea = valueb.replace("\n","")
  71. updata_db(m_key,valuea)
  72. elif "201" == order:
  73. valueb = lambda_code+sqs_code+https_code
  74. valuea = valueb.replace("\n","")
  75. updata_db(m_key,valuea)
  76. elif "210" == order:
  77. valueb = lambda_code+https_code+sqs_code
  78. valuea = valueb.replace("\n","")
  79. updata_db(m_key,valuea)
  80. return {
  81. 'statusCode': 200,
  82. 'body': json.dumps('Hello from Lambda!')
  83. }
  84. # 2、API-gateway lambda
  85. import json
  86. import boto3
  87. import time
  88. def lambda_handler(event, context):
  89. time.sleep(2)
  90. # print(event)
  91. # print(event)
  92. body = json.loads(event['body'])
  93. # print(type(body))
  94. val = body["Message"]
  95. key = body['MessageAttributes']['mykey']['Value']
  96. print("FORMHTTPS:" + key + ":" + val)
  97. ddb= boto3.client('dynamodb')
  98. table_name = 'found'
  99. ddb.update_item(
  100. TableName=table_name,
  101. ExpressionAttributeValues={
  102. ':y': {
  103. 'S': val,
  104. }
  105. },
  106. Key={
  107. 'm_key': {
  108. 'S': key,
  109. }
  110. },
  111. UpdateExpression='SET https = :y',
  112. ReturnValues="UPDATED_NEW"
  113. )
  114. # TODO implement
  115. return {
  116. 'statusCode': 200,
  117. 'body': json.dumps('Hello from Lambda!')
  118. }
  119. # 3、lambda - lambda
  120. import json
  121. import boto3
  122. def lambda_handler(event, context):
  123. # TODO implement
  124. # print(event)
  125. sec = event['Records'][0]['Sns']['MessageAttributes']
  126. mes = event['Records'][0]['Sns']['Message']
  127. m_key = sec['mykey']['Value']
  128. order = sec['order']['Value']
  129. a1 = order.replace(":", "")
  130. a2 = a1.replace("SQS", '')
  131. a3 = a2.replace("HTTPS","")
  132. a4=a3.replace("LAMBDA","")
  133. a5=a4.replace(" ","")
  134. print("ORDER:"+ m_key + ":" + a5)
  135. print("FORMLAMBDA:" + m_key + ":" + mes)
  136. # updata_db(m_key,a5)
  137. ddb= boto3.client('dynamodb')
  138. tableName = 'found'
  139. item = { 'm_key': {'S': m_key},
  140. 'order': {'S': a5},
  141. 'lambda': {'S': mes}
  142. }
  143. ddb.put_item(TableName = tableName, Item = item)