Closed merforga closed 6 years ago
Okay, went ahead and logged in to Google with our account, ambled over to https://console.developers.google.com/apis/ then created a project and credentials for accessing Drive. Once we integrate this, we'll have a new dependency: pip3 install google-api-python-client
. I verified that I can upload a file and make sure it's there:
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient import discovery
from googleapiclient.http import MediaFileUpload
scopes = ['https://www.googleapis.com/auth/drive.file']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'credentials/GW2Raidar-2d968ae45d30.json', scopes=scopes)
http_auth = credentials.authorize(Http())
service = discovery.build('drive', 'v3', http=http_auth)
# upload
file_path = 'TestLogs/20170222-202803.evtc'
file_name = '20170222-202803.evtc'
file_metadata = { 'name' : file_name }
media = MediaFileUpload(file_path, mimetype='application/prs.evtc')
file = service.files().create(body=file_metadata,
media_body=media,
fields='id, webContentLink').execute()
# allow download to everyone
permission = {
'role': 'reader',
'type': 'anyone',
'allowFileDiscovery': False
}
result = service.permissions().create(
fileId=file.get('id'),
body=permission,
fields='id',
).execute()
# list files
results = service.files().list(
pageSize=10,fields="nextPageToken, files(id, name, webContentLink)").execute()
items = results.get('files', [])
print(items)
# delete them batchfully
def callback(request_id, response, exception):
if exception:
# Handle error
print(exception)
else:
print("Done with %s" % request_id)
print(response)
batch = service.new_batch_http_request(callback=callback)
for item in items:
print(item)
request = service.files().delete(fileId=item.get('id'))
batch.add(request)
batch.execute()
Since these are considered "application files", they do not show up in Google Drive dashboard. It is important to note that files on Google Drive are identified by ID, not by filename, so rerunning the code above will create another copy of the same file. Thus, we will either search the file by name, or (better) add a google drive file ID to the Encounter DB schema.
Anyway... there's reference, guides, and the Python API.
Error on two files while uploading:
Working as intended \o/
Store log files on Google Drive instead of directly on the server