Skip to content

Commit

Permalink
Add drive download utility functions and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Apophenia committed Nov 26, 2024
1 parent 86d9098 commit 20188bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
39 changes: 39 additions & 0 deletions processes/util/google_integration.py
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






15 changes: 15 additions & 0 deletions tests/integration/test_google_integration.py
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

0 comments on commit 20188bc

Please sign in to comment.