-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
75 lines (59 loc) · 1.9 KB
/
index.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3.9
import logging
import boto3
from botocore.exceptions import ClientError
import os
import sys
import threading
import time
from datetime import datetime
import json
errors = []
path = None
try:
path = sys.argv[1]
except IndexError as e:
print('pass the file directory path.')
print('Example: python index.py ~/test/')
sys.exit('')
def upload_file(_s3_client, _file_name, _bucket, _object_name=None):
"""Upload files to an S3 bucket
:param _file_name: File to upload
:param _bucket: Bucket to upload to
:param _object_name: S3 object name. if not used, _file_name will be used
:return: True if file was upload, use _file_name
"""
if _object_name is None:
_object_name = _file_name
# s3_client = boto3.client('s3')
try:
print('[UPLOADING]', _object_name)
time.sleep(1)
response = _s3_client.upload_file(_file_name, _bucket, _object_name)
os.system(f"sudo rm -f {_object_name}")
except ClientError as e:
print('[Error]', e)
errors.append(_file_name)
return False
except FileNotFoundError as e:
print('[Error]', e)
errors.append(_file_name)
return True
def main():
boto3_session = boto3.session.Session()
s3_client = boto3_session.client('s3')
thread_list = []
for root, dirs, files in os.walk(path):
for file in files:
_file_name = os.path.join(root, file)
thread = threading.Thread(target=upload_file, args=(s3_client, _file_name, 'hdpbxrecordings',)) # noqa: E501
thread_list.append(thread)
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
if __name__ == "__main__":
main()
if len(errors):
with open(f"/home/centos/logs/uploadToS3-uploading-{datetime.now()}.json", "w") as f: # noqa: E501
json.dump(errors, f)