-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.py
More file actions
57 lines (45 loc) · 1.59 KB
/
func.py
File metadata and controls
57 lines (45 loc) · 1.59 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
import os
import time
import boto3
from datetime import datetime
from gen import gen_content, gen_report
def get_boto_session():
boto_session = boto3.session.Session(
aws_access_key_id=os.environ['ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['SECRET_ACCESS_KEY']
)
return boto_session
def get_storage_client(region='ru-central1'):
storage_client = get_boto_session().client(
service_name='s3',
endpoint_url='https://storage.yandexcloud.net',
region_name=region
)
return storage_client
def handler(event, context):
args = event['queryStringParameters']
if 'id' not in args.keys() or not args['id'].isnumeric():
raise Exception('Params university ID is required')
args['folder'], args['width'] = '/tmp', 13
object_name = f"reporting/vacancies/{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}_id_{args['id']}.docx"
start = time.time()
gen_content(args)
file_path = gen_report(args)
client = get_storage_client()
client.upload_file(file_path, 'psal.public', object_name)
return {
'statusCode': 302,
'headers': {
'Location': client.generate_presigned_url('get_object', Params={
'Bucket': 'psal.public',
'Key': object_name
}, ExpiresIn=3600)
},
'body': {
"generated_by": f"{round(time.time() - start, 2)} second",
"ulr": client.generate_presigned_url('get_object', Params={
'Bucket': 'psal.public',
'Key': object_name
}, ExpiresIn=3600)
}
}