Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Created March 16, 2025 15:04
Show Gist options
  • Select an option

  • Save rajathithan/0157d6bb55964d587c94fd6cdbe0bab4 to your computer and use it in GitHub Desktop.

Select an option

Save rajathithan/0157d6bb55964d587c94fd6cdbe0bab4 to your computer and use it in GitHub Desktop.
Download files from google drive folder
import os
import pickle
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# ID of the folder to be downloaded.
# ID can be obtained from the URL of the folder
FOLDER_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # an example folder
# If modifying these scopes, delete the file token.pickle.
# SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
SCOPES = ["https://www.googleapis.com/auth/drive"]
# Download all files in the specified folder in Google Drive.
def main():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("key.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
# Initialize the Drive v3 API service with the provided credentials
service = build("drive", "v3", credentials=creds)
# Initialize a variable to keep track of pagination tokens
page_token = None
# Loop through all files in the specified folder, handling pagination
while True:
# Call the Drive v3 API to list files in the specified folder
results = (
service.files()
.list(
q=f"'{FOLDER_ID}' in parents",
pageSize=10,
fields="nextPageToken, files(id, name)",
pageToken=page_token,
)
.execute()
)
items = results.get("files", [])
# Print message if no files are found in the folder
if not items:
print("No files found.")
else:
# Iterate over each file in the folder
for item in items:
print("{0} ({1})".format(item["name"], item["id"]))
# Set the current file ID for downloading
file_id = item["id"]
# Prepare a request to download the file's media content
request = service.files().get_media(fileId=file_id)
# Open a file in binary write mode with the same name as
# the downloaded file
with open(item["name"], "wb") as fh:
# Initialize a media download object
downloader = MediaIoBaseDownload(fh, request)
# Flag to indicate when the download is complete
done = False
# Loop until the download is complete
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress()*100))
# Get the next pagination token from the response
page_token = results.get("nextPageToken", None)
if page_token is None:
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment