└── setup.py /setup.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | import os.path 3 | from googleapiclient.discovery import build 4 | from google_auth_oauthlib.flow import InstalledAppFlow 5 | from google.auth.transport.requests import Request 6 | from apiclient.http import MediaFileUpload 7 | 8 | import os 9 | 10 | 11 | class MyDrive(): 12 | def __init__(self): 13 | # If modifying these scopes, delete the file token.pickle. 14 | SCOPES = ['https://www.googleapis.com/auth/drive'] 15 | """Shows basic usage of the Drive v3 API. 16 | Prints the names and ids of the first 10 files the user has access to. 17 | """ 18 | creds = None 19 | # The file token.pickle stores the user's access and refresh tokens, and is 20 | # created automatically when the authorization flow completes for the first 21 | # time. 22 | if os.path.exists('token.pickle'): 23 | with open('token.pickle', 'rb') as token: 24 | creds = pickle.load(token) 25 | # If there are no (valid) credentials available, let the user log in. 26 | if not creds or not creds.valid: 27 | if creds and creds.expired and creds.refresh_token: 28 | creds.refresh(Request()) 29 | else: 30 | flow = InstalledAppFlow.from_client_secrets_file( 31 | 'credentials.json', SCOPES) 32 | creds = flow.run_local_server(port=0) 33 | # Save the credentials for the next run 34 | with open('token.pickle', 'wb') as token: 35 | pickle.dump(creds, token) 36 | 37 | self.service = build('drive', 'v3', credentials=creds) 38 | 39 | def list_files(self, page_size=10): 40 | # Call the Drive v3 API 41 | results = self.service.files().list( 42 | pageSize=page_size, fields="nextPageToken, files(id, name)").execute() 43 | items = results.get('files', []) 44 | 45 | if not items: 46 | print('No files found.') 47 | else: 48 | print('Files:') 49 | for item in items: 50 | print(u'{0} ({1})'.format(item['name'], item['id'])) 51 | 52 | def upload_file(self, filename, path): 53 | folder_id = "1XLeFvsgBbnagvMM20ptRH3PAnNK6QDDE" 54 | media = MediaFileUpload(f"{path}{filename}") 55 | 56 | response = self.service.files().list( 57 | q=f"name='{filename}' and parents='{folder_id}'", 58 | spaces='drive', 59 | fields='nextPageToken, files(id, name)', 60 | pageToken=None).execute() 61 | if len(response['files']) == 0: 62 | file_metadata = { 63 | 'name': filename, 64 | 'parents': [folder_id] 65 | } 66 | file = self.service.files().create(body=file_metadata, media_body=media, fields='id').execute() 67 | print(f"A new file was created {file.get('id')}") 68 | 69 | else: 70 | for file in response.get('files', []): 71 | # Process change 72 | 73 | update_file = self.service.files().update( 74 | fileId=file.get('id'), 75 | media_body=media, 76 | ).execute() 77 | print(f'Updated File') 78 | 79 | 80 | def main(): 81 | path = "C:/Users/programmer/Desktop/Backup/" 82 | files = os.listdir(path) 83 | my_drive = MyDrive() 84 | #my_drive.list_files() 85 | 86 | for item in files: 87 | my_drive.upload_file(item, path) 88 | 89 | 90 | if __name__ == '__main__': 91 | main() --------------------------------------------------------------------------------