Skip to content

Download files from google drive using google api #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
FROM kbase/sdkbase2:python
FROM kbase/sdkpython:3.8.10
MAINTAINER KBase Developer
# -----------------------------------------

# Insert apt-get instructions here to install
# any required dependencies for your module.

# RUN apt-get update
RUN apt-get update
RUN apt-get install pigz wget nano

RUN pip install semver \
&& ( [ $(pip show filemagic|grep -c filemagic) -eq 0 ] || pip uninstall -y filemagic ) \
&& pip install python-magic \
&& pip install ftputil \
&& pip install ipython==5.3.0 \
&& pip install ipython \
&& pip install pyftpdlib==1.5.6 \
&& sudo apt-get install nano
# -----------------------------------------
&& pip install google-api-python-client \
&& pip install bz2file

RUN sudo apt-get update
RUN sudo apt-get install pigz wget
RUN pip install bz2file

COPY ./ /kb/module
RUN mkdir -p /kb/module/work
Expand Down
73 changes: 73 additions & 0 deletions lib/DataFileUtil/utils/download_file_from_gdrive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Requires enabling "Google Drive API" in console.cloud.google.com
"""
# [START drive_download_file]

from __future__ import print_function

import io
import os
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
import pathlib

def download_file_from_gdrive(real_file_id):
"""Downloads a file
Args:
real_file_id: ID of the file to download
Returns : IO object with location.

Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""



os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str(pathlib.Path(__file__).parent.resolve()) + "/google_authentication.json"
creds, _ = google.auth.default()


# create drive api client
service = build('drive', 'v3', credentials=creds)
file_id = real_file_id
filename = service.files().get(fileId=file_id).execute().get('name')
request = service.files().get_media(fileId=file_id)

# This will cause an issue if the user downloads a "google_authentication.json" file,
# otherwise this downloads to the "utils" folder
# This app is meant to be used once and then thrown away, so the container filessystem will be cleared each run
output_filepath = str(pathlib.Path(__file__).parent.resolve()) + "/" + filename


file = io.FileIO(output_filepath,'w')

downloader = MediaIoBaseDownload(file, request)
done = False
# Might want to either pass in a logger, or delete the print statements
while done is False:
status, done = downloader.next_chunk()
print(F'Download percentage: {int(status.progress() * 100)} %')
print ("Downloaded to ", output_filepath)
return output_filepath


if __name__ == '__main__':
download_file_from_gdrive(real_file_id='1m3YLV7hlFluFJOA6ztTCe4DxN0Vn9-kZ')

13 changes: 13 additions & 0 deletions lib/DataFileUtil/utils/google_authentication.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "REDACTED",
"private_key_id": "REDACTED",
"private_key": "-----BEGIN PRIVATE KEY-----REDACTED-----END PRIVATE KEY-----\n",
"client_email": "REDACTED",
"client_id": "REDACTED",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/REDACTED"
}