-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.py
More file actions
58 lines (46 loc) · 1.6 KB
/
Copy pathindex.py
File metadata and controls
58 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
import subprocess
import oss2
import logging
import json
import os
import time
logging.getLogger("oss2.api").setLevel(logging.ERROR)
logging.getLogger("oss2.auth").setLevel(logging.ERROR)
LOGGER = logging.getLogger()
'''
1. function and bucket locate in same region
2. service's role has OSSReadAccess
3. event format
{
"bucket_name" : "test-bucket",
"object_key" : "a.mp4"
}
'''
# a decorator for print the excute time of a function
def print_excute_time(func):
def wrapper(*args, **kwargs):
local_time = time.time()
ret = func(*args, **kwargs)
LOGGER.info('current Function [%s] excute time is %.2f seconds' %
(func.__name__, time.time() - local_time))
return ret
return wrapper
@print_excute_time
def handler(event, context):
evt = json.loads(event)
oss_bucket_name = evt["bucket_name"]
object_key = evt["object_key"]
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:
raise Exception("object {} is not exist".format(object_key))
object_url = oss_client.sign_url('GET', object_key, 15 * 60)
raw_result = subprocess.check_output(["ffprobe", "-v", "quiet", "-show_format", "-show_streams",
"-print_format", "json", "-i", object_url])
result = json.loads(raw_result)
return result