Table of Contents
Today is a cool weekend in Saigon. There is a lot of knowledge I (Hieu) want to write, but life is too busy. Suddenly, I remembered I had a very good tool for those who have little knowledge of Python language to use. That is Simple-Drive, a library using the Google Drive API I wrote. It helps us not only in manipulating files, such as creating, editing, deleting, uploading, downloading, renaming, and copying, but also in many features related to permissions, comments, and version history.
Simple-Drive Functions
Files:
create
: Create a new file in Google Drive.create_shortcut
: Create a shortcut to a file or folder in Google Drive.upload
: Upload files from a local device to Google Drive.get
: Get details about a specific file on Google Drive.move
: Move a file to another folder in Google Drive.copy
: Copy the current file into a new file in Google Drive.rename
: Rename files on Google Drive.restrict
: Restrict access to a file (allow only specified people to access).list
: List files in Google Drive (can include filters).download
: Download files from Google Drive to a local device.export:
Export Google Drive files (like Google Docs and Sheets) to other formats (PDF, Word, etc.).empty_trash
: Permanently delete all files from Google Drive’s trash.trash
: Move the file to the trash.delete
: Permanently delete a file from Google Drive.
Permissions:
add
: Add access (view, edit) to a user or group for a file or folder.transfer_ownership
: Transfer ownership of a file or folder to another user.get
: Get detailed information about the access permissions of a file or folder.update
: Update current access permissions (for example, change from view-only to edit).list
: List all access permissions of a file or directory.remove
: Remove user or group access from a file or folder.
Comments:
create
: Create a new comment on a file (e.g., Google Docs, Sheets).get
: Get detailed information about a specific comment.update
: Update the content of an existing comment.list
: List all comments on a file.delete
: Delete a comment from the file.
Replies:
create
: Create a response to a comment on the file.get
: Get detailed information about a specific response.update
: Updates the contents of an existing response.list
: List all responses to a comment.delete
: Delete a response from the comment.
Revisions:
get
: Get details about a previous version of a file.list
: List all revisions of a file.delete
: Delete a specific version from the file history.
Install Simple-Drive library
As long as you have Conda or Python installed on your machine, you can add the Simple-Drive library using the Pip command as below.
pip install --upgrade simple-drive
Authorize use of Google Drive API
First, we will import the necessary Simple-Drive modules.
from simple_drive import Auth, Drive, MimeTypes, Roles
There are two ways to authorize use of the Google Drive API:
Method 1: Use a Service Account. This is the most common way to authorize the use of Google APIs. This method makes your script run easily because the Service Account never expires. “Create once, use forever.” – I often joke.
# Nếu service account là file
auth = Auth.from_service_account_file(file='service_account.json')
# Nếu service acount là variable
auth = Auth.from_service_account_info(info)
Method 2: Use your primary account. This method requires manual authentication, and the authorization expires in 7 days. You need to create a Client Secret in Google Cloud to do this.
auth = Auth.local_web_server(client_secrets_file='client_secrets.json')
After authorization is done, we will initialize the Drive object. The parameter verbose
is optional when printing information when using Google Drive API. You should leave it True
because printing is very beautiful, hehe.
drive = Drive(auth, verbose=True)
Using Simple-Drive Basics
I will take a scenario to make it easier for you to understand. Suppose you want to create a folder, then create a Google Sheets file in that folder, then share it with someone and add a comment to the newly created file.
# Create a folder
folder = drive.Files.create(name='My folder', mime_type=MimeTypes.FOLDER, dest_folder_id=None)
# Create a sheet
sheet = drive.Files.create(name='My sheet', mime_type=MimeTypes.SHEETS, dest_folder_id=folder['id'])
# Add a permission
drive.Permissions.add(file_id=sheet['id'], email='her@gmail.com', role=Roles.EDITOR)
# Create a comment
drive.Comments.create(file_id=sheet['id'], content='Hello, this is today data.')
As in the example above, you will see that I create a class and function structure according to each functional group. At the same time, I support standard parameters that Google Drive API requires, such as MimeType and Role, so that users do not have to fill in manually.
Another example, let’s say you want to perform an operation on a batch of files, and you need to get a list of files based on the following criteria:
- Files created from 2024-01-01 onwards.
- The name contains “Backlog data”.
- Located in a specified folder.
from simple_drive import SearchTerms
import pandas as pd
term_1 = SearchTerms.createdTime_greater_equal('2024-01-01')
term_2 = SearchTerms.name_contains('Backlog data')
term_3 = SearchTerms.folder_id('YourFolderID')
files = drive.Files.list(term_1, term_2, term_3, fields='*', operator='and')
df = pd.DataFrame(files)
Once you have the file list, you just need to use for
loop to perform batch operations on all files.
Conclusion
With more than 30 functions, I believe that Simple-Drive can meet many needs related to Google Drive API. Especially when combined with other libraries, such as gspread
– A library that works with Google Sheets API will help you expand your capabilities in API with Google products.