-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
109 lines (84 loc) · 3.77 KB
/
start.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import json
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
SETTINGS_JSON = "settings.json"
# Scopes
# https://www.googleapis.com/auth/drive See, edit, create, and delete all of your Google Drive files
# https://www.googleapis.com/auth/drive.appdata See, create, and delete its own configuration data in your Google Drive
# https://www.googleapis.com/auth/drive.file See, edit, create, and delete only the specific Google Drive files you use with this app
# https://www.googleapis.com/auth/drive.metadata View and manage metadata of files in your Google Drive
# https://www.googleapis.com/auth/drive.metadata.readonly See information about your Google Drive files
# https://www.googleapis.com/auth/drive.photos.readonly View the photos, videos and albums in your Google Photos
# https://www.googleapis.com/auth/drive.readonly See and download all your Google Drive files
# https://www.googleapis.com/auth/drive.scripts Modify your Google Apps Script scripts' behavior
SCOPES = ['https://www.googleapis.com/auth/drive.file']
TOKEN_AUTH_FILE = './key/google_auth.json'
CLIENT_SECRET_FILE = './key/client_secret.json'
UPLOAD_FOLDER_ID = '1NoUtT4OzLTVgrA3inKXgi_Ag7mXyY05X' #1jUJBwKx5fMMuXxxXWiOK6L4a1FuxLrsr
def authorization():
credentials = None
if os.path.exists(TOKEN_AUTH_FILE):
credentials = Credentials.from_authorized_user_file(TOKEN_AUTH_FILE, scopes=SCOPES)
if credentials.expired:
print('憑證已過期')
if not credentials.valid:
print("憑證無效")
if not credentials or not credentials.valid:
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRET_FILE, scopes=SCOPES
)
credentials = flow.run_local_server(port=0)
with open(TOKEN_AUTH_FILE, "w") as token:
token.write(credentials.to_json())
return credentials
def create_drive_service():
credentials = authorization();
# Use service account file
# create drive api client
service = build("drive", "v3", credentials=credentials)
return service
def upload_file(file_path, file_type):
drive_service = create_drive_service()
file_metadata = {
'name': os.path.basename(file_path),
'parents': [UPLOAD_FOLDER_ID],
}
file = None
if file_type in ['media', 'text', 'zip']:
if file_type == 'media':
file = MediaFileUpload(file_path, mimetype="image/png")
if file_type == 'text':
file = MediaFileUpload(file_path, mimetype='text/plain')
if file_type == 'zip':
file = MediaFileUpload(file_path, mimetype='application/zip')
else:
print("沒有此檔案格式")
try:
response = drive_service.files().create(
body=file_metadata,
media_body=file,
).execute()
except HttpError as error:
print(f"An error occurred: {error}")
response = None
# print(f'File uploaded: {file}')
print(f'File uploaded: {response["name"]} ({response["id"]})')
def get_file(service, file_id):
file_info = service.files().get(fileId=file_id).execute()
print(file_info)
def check_authorization(service, file_id):
permissions = service.permissions().list(fileId=file_id).execute()
print('檔案的權限:', permissions.get('permissions', []))
if __name__ == "__main__":
global file_path
global file_type
with open(SETTINGS_JSON, "r") as settings_file:
settings = json.load(settings_file)
file_path = settings['file_path']
file_type = settings['file_type']
UPLOAD_FOLDER_ID = settings['destination_dir']
upload_file(file_path, file_type)