-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdrive_api.py
More file actions
89 lines (69 loc) · 2.66 KB
/
drive_api.py
File metadata and controls
89 lines (69 loc) · 2.66 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import time
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient import http
import io
# api가 전부 접근할 수는 없음 google drive로 작성됨이 있어야함
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('drive_storage.json')
creds = store.get()
if not creds or creds.invalid:
print("make new storage data file ")
flow = client.flow_from_clientsecrets('client_secret_api.json', SCOPES)
creds = tools.run_flow(flow, store, flags) \
if flags else tools.run(flow, store)
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))
# find file_id by file_name
def get_file_id(file_title) :
query = "name contains '{}'".format(file_title)
response = DRIVE.files().list(q=query,
spaces='drive',
fields='files(id, name)').execute()
for exist_folder in response.get('files', []):
print(exist_folder)
# Process change
if exist_folder.get('name') == file_title :
print('Found : %s (%s)' % (exist_folder.get('name'), exist_folder.get('id')))
return exist_folder.get('id')
print("fail to find")
return None
# single file upload
def upload_file(file_title, folder_id=None):
metadata = {'name': file_title,
'mimeType': None,
}
if folder_id is not None :
metadata['parents'] = [folder_id]
res = DRIVE.files().create(body=metadata, media_body=file_title, fields='id, name, webViewLink').execute()
if res:
print('Uploaded "%s" (%s)' % (res['name'], res['webViewLink']))
def download_file(file_title, local_fd=None):
file_id = get_file_id(file_title)
request = DRIVE.files().get_media(fileId=file_id)
if local_fd is None:
local_fd = file_title
file_to_save = open(local_fd, "wb")
media_request = http.MediaIoBaseDownload(file_to_save, request)
while True:
try:
download_progress, done = media_request.next_chunk()
except Exception as e:
print(e)
file_to_save.close()
return
if download_progress:
print('Download Progress: %d%%' % int(download_progress.progress() * 100))
if done:
print('Download Complete')
file_to_save.close()
return
if __name__ == "__main__" :
#upload_file("test.jpg", "0B_CtpwiAk5hIZDJhMGlneURHTUE")
#upload_file("test.jpg")
download_file("test.jpg")