BUY FULL SOURCE CODE
To download a file from a URL to Google Drive using Google Colab in Python, you can follow the below steps:
- Import the necessary libraries:
!pip install google-auth google-auth-oauthlib google-auth-httplib2
!pip install google-api-python-client
1 2 3 |
from google.colab import auth from googleapiclient.discovery import build import requests |
- Authenticate the user using Google OAuth2 authentication:
1 |
auth.authenticate_user() |
- Set up the Drive API:
1 |
drive_service = build('drive', 'v3') |
- Define the function to download the file from the URL to Google Drive:
1 2 3 4 5 |
def download_file_from_url_to_drive(url, file_name): r = requests.get(url) file_metadata = {'name': file_name, 'parents': ['<google_drive_folder_id>']} file = drive_service.files().create(body=file_metadata, media_body=io.BytesIO(r.content), fields='id').execute() print(f'File ID: {file.get("id")}') |
Note: Replace <google_drive_folder_id>
with the ID of the Google Drive folder where you want to save the downloaded file.
- Call the function and pass the URL and file name as arguments:
1 |
download_file_from_url_to_drive('https://www.example.com/example.pdf', 'example.pdf') |
Note: Replace the URL with the URL of the file you want to download and replace example.pdf
with the desired file name.
This will download the file from the URL and save it to the specified Google Drive folder.
BUY FULL SOURCE CODE