-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive_connector.py
More file actions
136 lines (102 loc) · 4.7 KB
/
Copy pathdrive_connector.py
File metadata and controls
136 lines (102 loc) · 4.7 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# https://www.geeksforgeeks.org/upload-and-download-files-from-google-drive-storage-using-python/
# import the required libraries
from __future__ import print_function
import pickle
import os.path
import io
import shutil
# import requests
from mimetypes import MimeTypes
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
class DriveAPI:
global SCOPES
# Define the scopes
SCOPES = ['https://www.googleapis.com/auth/drive']
def __init__(self):
service_name = 'drive'
api_version = 'v3'
self.creds = None
# The file token.pickle stores the
# user's access and refresh tokens. It is
# created automatically when the authorization
# flow completes for the first time.
# Check if file token.pickle exists
if os.path.exists(f'token_{service_name}_{api_version}.pickle'):
# Read the token from the file and
# store it in the variable self.creds
with open(f'token_{service_name}_{api_version}.pickle', 'rb') as token:
self.creds = pickle.load(token)
# If no valid credentials are available,
# request the user to log in.
if not self.creds or not self.creds.valid:
# If token is expired, it will be refreshed,
# else, we will request a new one.
if self.creds and self.creds.expired and self.creds.refresh_token:
self.creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
self.creds = flow.run_local_server(port=0)
# Save the access token in token.pickle
# file for future usage
with open(f'token_{service_name}_{api_version}.pickle', 'wb') as token:
pickle.dump(self.creds, token)
# Connect to the API service
self.service = build('drive', 'v3', credentials=self.creds)
# request a list of first N files or
# folders with name and id from the API.
# results = self.service.files().list(
# pageSize=100, fields="files(id, name)",q="'1tl2tVcc3FBokIKSqQrqCAeuTY8Oa6LNJ' in parents and trashed=false").execute()
# items = results.get('files', [])
# print a list of files
# print("Here's a list of files: \n")
# print(*items, sep="\n", end="\n\n")
def FileDownload(self, file_id, file_name):
request = self.service.files().get_media(fileId=file_id)
fh = io.BytesIO()
# Initialise a downloader object to download the file
downloader = MediaIoBaseDownload(fh, request, chunksize=204800)
done = False
try:
# Download the data in chunks
while not done:
status, done = downloader.next_chunk()
fh.seek(0)
# Write the received data to the file
with open(file_name, 'wb') as f:
shutil.copyfileobj(fh, f)
# print("File Downloaded")
# Return True if file Downloaded successfully
return True
except:
# Return False if something went wrong
print("Something went wrong.")
return False
def FolderDownload (self, folder_id, path = ""):
results = self.service.files().list(
pageSize=100, fields="files(id, name)",q="'{}' in parents and trashed=false".format(folder_id)).execute()
items = results.get('files', [])
for item in items:
self.FileDownload(item['id'], '{}/{}'.format(path,item['name']))
def FileUpload(self, filepath):
# Extract the file name out of the file path
name = filepath.split('/')[-1]
# Find the MimeType of the file
mimetype = MimeTypes().guess_type(name)[0]
# create file metadata
file_metadata = {'name': name}
try:
media = MediaFileUpload(filepath, mimetype=mimetype)
# Create a new file in the Drive storage
file = self.service.files().create(
body=file_metadata, media_body=media, fields='id').execute()
print("File Uploaded.")
except:
# Raise UploadError if file is not uploaded.
raise UploadError("Can't Upload File.")
if __name__ == "__main__":
obj = DriveAPI()
obj.FolderDownload('1SauYlBU0gw21ggh7k-kIx099kBuNAtQM','queries')