When working on big data projects, extracting data from Google drive into orchestration workflows to initially store the data in data lakes followed by a series of operations like data validation, cleansing, and transformation is widely used to gather business insights from the data. In this Python Sample Code, we are going to upload files to google drive using python and use them in data flow orchestration processes. Show
Steps for Uploading files on Google Drive using PythonTable of Contents
Pre-Requisites
Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects Step 1: Import the libraries
Step 2: OAuth made easyFollow the steps to Get Authentication for Google Service API in the below link: Get Authentication for Google Service API Download client_secrets.json from Google API Console and OAuth2.0 is done in two lines. You can customize the behavior of OAuth2 in one settings file settings.yaml
Above steps together as follows :
Step 3 : Upload files to your Google Drive
Output of the above code:
Step 4: List out files from Google DriveWe can also list all files from the specific folder in google drive as follows :
Output of the above code: Step 5: Download the files from Google DriveWe can also download the files from Google Drive as follows. Note - after listing the files only we can download the file.
The output of the above code: In the above snapshot files are downloaded from the specific folder, Note here files will download where the code will be executed. Step 6: Create the Text files in Google DriveWe can also write files directly to Google Drive using the following code:
The output of the above code: test.txt file is created in google drive. Step 7: Read the content of the text file directly from Google DriveAlso, we can read the file directly from Google Drive using the below code :
The output of the above code: In the above snapshot reading the content of the file as "Hello world" How do I use Google Drive API with Python?If you're unfamiliar with authentication and authorization for Google Workspace APIs, read the Authentication and authorization overview. Create a Python command-line application that makes requests to the Drive API.. Set up your environment.. Install the client library.. Set up the sample.. Run the sample.. Can you access Google Drive from Python?Google Drive Access via Python (gdrive-access) gdrive-access is a simplified set of Python functions for navigating Google Drive folders and uploading/downloading files, built on PyDrive. To get set up, you will need to install gdrive-access and run the "setup credentials" script ( python -m gdrive_access.
Is Google Drive API free?All use of the Drive API is available at no additional cost.
How does Python read data from Google Drive?1) Install PyDrive. The first step is to install PyDrive. ... . 2) Authenticate. The second step is to authenticate and create a PyDrive client.. 3) Authorizing. ... . 4) Generating a shareable link. ... . 5) Getting the file_id. ... . 6) Load the CSV. ... . 7) Showing the Results.. |