-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drive download utility functions and test
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from io import BytesIO | ||
import json | ||
import os | ||
from googleapiclient.discovery import build | ||
from googleapiclient.http import MediaIoBaseDownload | ||
from google.oauth2.service_account import Credentials | ||
|
||
SERVICE_ACCOUNT_FILE = os.environ['GOOGLE_SERVICE_ACCOUNT_FILE'] | ||
|
||
def create_drive_service(service_account_info): | ||
scopes = ['https://www.googleapis.com/auth/drive'] | ||
credentials = Credentials.from_service_account_info(service_account_info, scopes=scopes) | ||
|
||
return build('drive', 'v3', credentials=credentials) | ||
|
||
def get_drive_file(file_id: str) -> BytesIO: | ||
drive_service = create_drive_service(service_account_info=json.loads(SERVICE_ACCOUNT_FILE)) | ||
request = drive_service.files().get_media(fileId=file_id) | ||
file = BytesIO() | ||
|
||
try: | ||
downloader = MediaIoBaseDownload(file, request) | ||
|
||
done = False | ||
while done is False: | ||
status, done = downloader.next_chunk() | ||
print(f"Download {int(status.progress() * 100)}.") | ||
|
||
except HttpError as error: | ||
print(f"An error occurred when downloading Drive file {file_id}: {error}") | ||
file = None | ||
|
||
return file | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
from load_env import load_env_file | ||
|
||
load_env_file('local-compose', file_string='config/local-compose.yaml') | ||
|
||
from processes.util.google_integration import get_drive_file | ||
|
||
def test_get_drive_file(): | ||
test_id = os.environ['EXAMPLE_FILE_ID'] | ||
file = get_drive_file(test_id) | ||
|
||
assert file != None | ||
print(file.seek(0, 2)) | ||
assert file.seek(0, 2) > 1 | ||
|