-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.py
69 lines (64 loc) · 2.77 KB
/
upload.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
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient.http import MediaFileUpload
import os
import argparse
# Access this link
# https://developers.google.com/drive/api/v3/quickstart/python
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive']
def main(folder_image, folder_id, output_id_file):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
nb_file = 0
f_write = open(output_id_file, "w")
for sub_dir, _, fs in os.walk(folder_image):
for f in fs:
nb_file += 1
file_name = os.path.join(sub_dir, f)
print("Uploaded: {} file ".format(nb_file))
print("Current upload: {}".format(file_name))
file_metadata = {
'name': f,
'parents': [ folder_id ]
}
media = MediaFileUpload(file_name,
mimetype='image/png')
file = service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
f_write.write("{}\t{}\n".format(file_name , file.get('id')))
f_write.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--folder_image", type=str, default="/home/tanlm/Downloads/covid_data/rotation_data")
parser.add_argument("--folder_id", type=str, default="1kWliiJLrM4mEWStP7FWU8S8CNYa92S5F")
parser.add_argument("--output_id_file", type=str, default="/home/tanlm/Downloads/covid_data/id_file_rotation.txt")
args = parser.parse_args()
main(args.folder_image, args.folder_id, args.output_id_file)