空港防疫算法交互
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

127 lines
6.5KB

  1. import oss2
  2. import time
  3. from voduploadsdk.AliyunVodUtils import *
  4. from exception.CustomerException import ServiceException
  5. from enums.ExceptionEnum import ExceptionType
  6. import json
  7. from aliyunsdkcore.client import AcsClient
  8. from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest
  9. from voduploadsdk.AliyunVodUtils import *
  10. from voduploadsdk.AliyunVodUploader import AliyunVodUploader
  11. from voduploadsdk.UploadVideoRequest import UploadVideoRequest
  12. class AliyunOssSdk():
  13. def __init__(self, content, logger, requestId):
  14. self.content = content
  15. self.bucket = None
  16. self.logger = logger
  17. self.requestId = requestId
  18. def get_oss_bucket(self):
  19. if self.bucket is None:
  20. self.logger.info("初始化oss桶, requestId:{}", self.requestId)
  21. auth = oss2.Auth(self.content["aliyun"]["access_key"], self.content["aliyun"]["access_secret"])
  22. self.bucket = oss2.Bucket(auth, self.content["aliyun"]["oss"]["endpoint"],
  23. self.content["aliyun"]["oss"]["bucket"],
  24. connect_timeout=self.content["aliyun"]["oss"]["connect_timeout"])
  25. def upload_file(self, updatePath, fileByte):
  26. self.logger.info("开始上传文件到oss, requestId:{}", self.requestId)
  27. self.get_oss_bucket()
  28. MAX_RETRIES = 3
  29. retry_count = 0
  30. while True:
  31. try:
  32. self.bucket.put_object(updatePath, fileByte)
  33. self.logger.info("上传文件到oss成功! requestId:{}", self.requestId)
  34. break
  35. except Exception as e:
  36. retry_count += 1
  37. time.sleep(1)
  38. self.logger.info("上传文件到oss失败, 重试次数:{}, requestId:{}", retry_count, self.requestId)
  39. if retry_count > MAX_RETRIES:
  40. self.logger.exception("上传文件到oss重试失败:{}, requestId:{}", e, self.requestId)
  41. raise e
  42. class ThAliyunVodSdk():
  43. def __init__(self, content, logger, requestId):
  44. self.content = content
  45. self.logger = logger
  46. self.requestId = requestId
  47. def init_vod_client(self, accessKeyId, accessKeySecret):
  48. regionId = self.content["aliyun"]["vod"]["ecsRegionId"]
  49. return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=5)
  50. def get_play_info(self, videoId):
  51. self.logger.info("开始获取视频地址,videoId:{}, requestId:{}", videoId, self.requestId)
  52. start = time.time()
  53. while True:
  54. try:
  55. clt = self.init_vod_client(self.content["aliyun"]["access_key"],
  56. self.content["aliyun"]["access_secret"])
  57. request = GetPlayInfoRequest.GetPlayInfoRequest()
  58. request.set_accept_format('JSON')
  59. request.set_VideoId(videoId)
  60. request.set_AuthTimeout(3600 * 5)
  61. response = json.loads(clt.do_action_with_exception(request))
  62. play_url = response["PlayInfoList"]["PlayInfo"][0]["PlayURL"]
  63. self.logger.info("获取视频地址成功,视频地址: {}, requestId: {}", play_url, self.requestId)
  64. return play_url
  65. except Exception as e:
  66. self.logger.error("获取视频地址失败,5秒后重试, requestId: {}", self.requestId)
  67. time.sleep(5)
  68. current_time = time.time()
  69. if "HTTP Status: 403" not in str(e):
  70. self.logger.exception("获取视频地址失败: {}, requestId: {}", e, self.requestId)
  71. raise ServiceException(ExceptionType.GET_VIDEO_URL_EXCEPTION.value[0],
  72. ExceptionType.GET_VIDEO_URL_EXCEPTION.value[1])
  73. if "HTTP Status: 403" in str(e) and ("UploadFail" in str(e) or "TranscodeFail" in str(e)):
  74. self.logger.exception("获取视频地址失败: {}, requestId: {}", e, self.requestId)
  75. raise ServiceException(ExceptionType.GET_VIDEO_URL_EXCEPTION.value[0],
  76. ExceptionType.GET_VIDEO_URL_EXCEPTION.value[1])
  77. diff_time = current_time - start
  78. if diff_time > 60 * 60 * 2:
  79. self.logger.exception("获取视频地址失败超时异常: {},超时时间:{}, requestId: {}", e, diff_time, self.requestId)
  80. raise ServiceException(ExceptionType.GET_VIDEO_URL_TIMEOUT_EXCEPTION.value[0],
  81. ExceptionType.GET_VIDEO_URL_TIMEOUT_EXCEPTION.value[1])
  82. def upload_local_video(self, filePath, file_title, storageLocation=None):
  83. self.logger.info("开始执行vod视频上传, filePath: {}, requestId: {}", filePath, self.requestId)
  84. uploader = AliyunVodUploader(self.content["aliyun"]["access_key"], self.content["aliyun"]["access_secret"])
  85. uploadVideoRequest = UploadVideoRequest(filePath, file_title)
  86. # 可以设置视频封面,如果是本地或网络图片可使用UploadImageRequest上传图片到视频点播,获取到ImageURL
  87. # ImageURL示例:https://example.com/sample-****.jpg
  88. # uploadVideoRequest.setCoverURL('<your Image URL>')
  89. # 标签
  90. # uploadVideoRequest.setTags('tag1,tag2')
  91. if storageLocation:
  92. uploadVideoRequest.setStorageLocation(storageLocation)
  93. MAX_RETRIES = 3
  94. retry_count = 0
  95. while True:
  96. try:
  97. result = uploader.uploadLocalVideo(uploadVideoRequest)
  98. self.logger.info("vod视频上传成功, videoId:{}, requestId:{}", result.get("VideoId"), self.requestId)
  99. return result.get("VideoId")
  100. except AliyunVodException as e:
  101. retry_count += 1
  102. time.sleep(3)
  103. self.logger.error("vod视频上传失败,重试次数:{}, requestId:{}", retry_count, self.requestId)
  104. if retry_count >= MAX_RETRIES:
  105. self.logger.exception("vod视频上传重试失败: {}, requestId:{}", e, self.requestId)
  106. raise ServiceException(ExceptionType.VIDEO_UPDATE_EXCEPTION.value[0],
  107. ExceptionType.VIDEO_UPDATE_EXCEPTION.value[1])
  108. def get_play_url(args):
  109. thAliyunVodSdk = ThAliyunVodSdk(args[2], args[3], args[4])
  110. videoId = thAliyunVodSdk.upload_local_video(args[0], args[1])
  111. if videoId is None or len(videoId) == 0:
  112. return None
  113. return thAliyunVodSdk.get_play_info(videoId)