Skip to content

Commit

Permalink
fix video-flow trigger function to check video object key
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Aug 26, 2024
1 parent 1def30d commit 7bc031a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 2 additions & 3 deletions video-flow/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ Parameters:
service: FNF
authorities:
- AliyunFCInvocationAccess
- AliyunFnFFullAccess

functionRoleArn:
title: 函数角色
Expand Down Expand Up @@ -106,13 +105,13 @@ Parameters:
prefix:
title: 前缀
type: string
default: src
default: src/
description: 建议设置精准的前缀,同一个 Bucket 下的不同触发器条件不能重叠包含

outputDir:
title: 转码后的视频保存目录
type: string
default: dst
default: dst/
description: 转码后的视频保存目录。为防止循环触发产生不必要的费用,强烈建议您设置不同于前缀的目标目录。

triggerRoleArn:
Expand Down
20 changes: 19 additions & 1 deletion video-flow/src/code/oss-trigger/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import re
import logging
import oss2

from aliyunsdkcore.client import AcsClient
from aliyunsdkfnf.request.v20190315 import StartExecutionRequest
Expand All @@ -16,13 +17,30 @@
SEG_INTERVAL = os.environ["SEG_INTERVAL"]
DST_FORMATS = os.environ["DST_FORMATS"]

def check_video_key(object_key, oss_bucket_name, context):
if object_key.endswith('/'):
LOGGER.info("object_key {} is a directory".format(object_key))
return False

creds = context.credentials
auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
oss_client = oss2.Bucket(auth, 'oss-%s-internal.aliyuncs.com' % context.region, oss_bucket_name)
exist = oss_client.object_exists(object_key)
if not exist:
LOGGER.info("object_key {} does not exist".format(object_key))
return False
return True

def handler(event, context):
evt = json.loads(event)
evt = evt["events"]
oss_bucket_name = evt[0]["oss"]["bucket"]["name"]
object_key = evt[0]["oss"]["object"]["key"]

creds = context.credentials

if not check_video_key(object_key, oss_bucket_name, context):
return

sts_token_credential = StsTokenCredential(creds.access_key_id, creds.access_key_secret, creds.security_token)
client = AcsClient(region_id=context.region, credential=sts_token_credential)

Expand Down

0 comments on commit 7bc031a

Please sign in to comment.