-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathfunction.py
More file actions
executable file
·61 lines (53 loc) · 1.76 KB
/
Copy pathfunction.py
File metadata and controls
executable file
·61 lines (53 loc) · 1.76 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
59
60
61
import datetime
import os
import uuid
import urllib.request
from jsonschema import validate
from . import storage
client = storage.storage.get_instance()
def handler(event):
schema = {
"type": "object",
"required": ["bucket", "object"],
"properties": {
"bucket": {
"type": "object",
"required": ["output"]
},
"object": {
"type": "object",
"required": ["url"]
}
}
}
try:
validate(event, schema=schema)
except:
return { 'status': 'failure', 'result': 'Some value(s) is/are not found in JSON data or of incorrect type' }
output_bucket = event['bucket']['output']
url = event['object']['url']
name = os.path.basename(url)
download_path = f'/tmp/{name}'
process_begin = datetime.datetime.now()
urllib.request.urlretrieve(url, filename=download_path)
size = os.path.getsize(download_path)
process_end = datetime.datetime.now()
upload_begin = datetime.datetime.now()
key_name = client.upload(output_bucket, name, download_path)
upload_end = datetime.datetime.now()
process_time = (process_end - process_begin) / datetime.timedelta(microseconds=1)
upload_time = (upload_end - upload_begin) / datetime.timedelta(microseconds=1)
return {
'status': 'success',
'result': 'Returned with no error',
'measurement': {
'bucket': output_bucket,
'url': url,
'key': key_name,
'download_time': 0,
'download_size': 0,
'upload_time': upload_time,
'upload_size': size,
'compute_time': process_time
}
}