-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcLoUdBACkup.py
51 lines (42 loc) · 1.42 KB
/
cLoUdBACkup.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
import os
import sys
import time
import threading
from google.oauth2 import service_account
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/folder/folder/credentials.json'
# Imports the Google Cloud client library
from google.cloud import storage
#Specify source folder on line 13
#Specify GCP bucket on line 15
#In the case below, folders are created empty until /test, then the subdir. of test
#will be uploaded with its contents as well
path = '/Users/desktop/test2'
bucket_name = 'my-bucket'
print('Preparing to backup to cloud')
time.sleep(2)
def get_files2(path):
for directory, _, filenames in os.walk(path):
for filename in filenames:
yield os.path.join(directory, filename)
print(str(filename) + ' has been sent to the Cloud')
print('Getting coordinates....')
time.sleep(3)
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
time.sleep(3)
print('Sending files to ' + str(bucket))
time.sleep(3)
def upload_serial(bucket, filename):
blob = bucket.blob(filename)
blob.upload_from_filename(filename)
def upload_parallel(bucket, path):
threads = []
for filename in get_files2(path):
# consider using thread pool
thread = threading.Thread(target=upload_serial, args=(bucket, filename))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
upload_parallel(bucket, path)
print('Launch complete')