Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A minimal and functional Google Drive API wrapper for Python. Easily perform Google Drive file operations like upload, download, list, delete, and manage metadata with an intuitive class-based interface.
with
statementInstall the package using pip:
pip install gdrive-core
Before using gdrive-core
, ensure you have:
credentials.json
file for either an OAuth 2.0 Client ID or a Service Account.credentials.json
file in your working directory.token.json
file will be created to store access tokens for future use.token.json
file is necessary as credentials are managed by the credentials.json
file.Initialize the GDriveCore
client for interacting with Google Drive. You can choose between oauth2
and service_account
authentication types.
from gdrive_core import GDriveCore
# Initialize the client with OAuth2
client = GDriveCore(auth_type='oauth2', credentials_file='credentials.json', token_file='token.json')
# Initialize the client with Service Account
# client = GDriveCore(auth_type='service_account', credentials_file='credentials.json')
Upload a file to Google Drive, optionally adding custom properties and using a callback function to track progress.
def upload_progress(progress):
print(f"Upload progress: {progress:.2f}%")
# Upload a file with progress tracking and custom properties
file_id = client.upload('example.txt', properties={'is-penguin': 'true', 'category': 'animal'}, progress_callback=upload_progress)
print(f"Uploaded file ID: {file_id}")
List files in the root directory or filter files using queries.
# List all files
files = client.list_files()
print("Files in Drive:")
for f in files:
print(f"- {f['name']} (ID: {f['id']})")
Download a file from Google Drive using its file ID. You can track the progress using a callback function.
def download_progress(progress):
print(f"Download progress: {progress:.2f}%")
# Download a file
download_path = 'downloaded_example.txt'
client.download(file_id, download_path, progress_callback=download_progress)
print(f"File downloaded to: {download_path}")
Create a new folder in Google Drive to organize your files.
# Create a folder
folder_id = client.create_folder('NewFolder')
print(f"Folder created with ID: {folder_id}")
Move an existing file into a specific folder.
# Move the file to the new folder
client.move(file_id, new_parent_id=folder_id)
print("File moved successfully!")
Update a file's name or description.
# Update file metadata
client.update_metadata(file_id, metadata={'name': 'renamed_example.txt', 'description': 'Updated metadata'})
print("File metadata updated!")
Search for files matching specific criteria like name, MIME type, and trashed status.
# Search for files
results = client.search(query_params={'name_contains': 'example', 'mime_type': 'text/plain', 'trashed': 'false'})
print("Search results:")
for file in results:
print(f"- {file['name']} (ID: {file['id']})")
Delete multiple files at once by providing their file IDs.
# Batch delete files
deletion_results = client.batch_delete([file_id])
print(f"Files deleted successfully: {deletion_results}")
Share a file with another user with a specific role (e.g., reader, writer).
# Share file with reader access
share_result = client.share(file_id, email='user@example.com', role='reader')
print(f"File shared successfully: {share_result}")
Get a list of previous versions (revisions) of a file.
# Get file revisions
revisions = client.get_file_revisions(file_id)
print(f"File revisions: {revisions}")
Create a copy of an existing file.
# Create a copy of the file
copied_file_id = client.copy_file(file_id, new_name='example_copy.txt')
print(f"Copied file ID: {copied_file_id}")
Retrieve the storage usage of your Google Drive.
# Get storage quota
quota = client.get_storage_quota()
print(f"Storage quota: {quota}")
Set up a webhook to receive notifications when the file is modified.
# Watch file for changes
watch_response = client.watch_file(file_id, webhook_url='https://your-webhook-url.com')
print(f"Watch setup result: {watch_response}")
Export a Google Workspace file to a specific format.
# Export file to PDF
exported_file = client.export_file(file_id, mime_type='application/pdf')
with open('exported.pdf', 'wb') as f:
f.write(exported_file.read())
print(f"File exported to 'exported.pdf'")
The simplest way to use gdrive-core is with the context manager:
from gdrive_core import GDriveCore
with GDriveCore() as drive:
# Upload a file to a nested folder structure (creates folders if they don't exist)
folder_id = drive.get_or_create_folder('Projects/2024/Reports')
file_id = drive.upload_file('monthly_report.pdf', folder_id)
Work with Google Drive using familiar path strings:
with GDriveCore() as drive:
# Get file ID from path
file_id = drive.path_to_id('Projects/2024/Reports/monthly_report.pdf')
# Create nested folders automatically
folder_id = drive.get_or_create_folder('Projects/2024/Reports')
Upload or delete multiple files efficiently:
with GDriveCore() as drive:
# Upload multiple files in parallel
files_to_upload = ['file1.txt', 'file2.pdf', 'file3.docx']
results = drive.batch_upload(files_to_upload, folder_id)
# Delete multiple files
files_to_delete = ['id1', 'id2', 'id3']
deletion_results = drive.batch_delete(files_to_delete)
Search for files using intuitive parameters:
with GDriveCore() as drive:
# Search for documents in a specific folder
docs = drive.search({
'type': 'document',
'parent': folder_id,
'name': 'Monthly Report'
})
# Search for non-trashed folders
folders = drive.search({
'type': 'folder',
'trashed': False
})
Here's a complete example showcasing the intuitive features:
from gdrive_core import GDriveCore
with GDriveCore() as drive:
# Create a nested folder structure
folder_id = drive.get_or_create_folder('Projects/2024/Reports')
# Upload multiple files to the folder
files_to_upload = ['report1.pdf', 'report2.pdf', 'data.xlsx']
upload_results = drive.batch_upload(files_to_upload, folder_id)
# Search for all PDF files in the folder
pdfs = drive.search({
'type': 'pdf',
'parent': folder_id
})
# Download all PDF files
for pdf in pdfs:
drive.download(pdf['id'], f"downloaded_{pdf['name']}")
# Share the folder with someone
drive.share(folder_id, 'colleague@company.com', role='writer')
# Get the folder's metadata
metadata = drive.get_file_metadata(folder_id)
print(f"Folder size: {metadata.get('size', 'N/A')} bytes")
Here are some common operations and their simplified syntax:
with GDriveCore() as drive:
# Create nested folders
folder_id = drive.get_or_create_folder('Path/To/Folder')
# Upload a file
file_id = drive.upload_file('document.pdf', folder_id)
# Get file ID from path
file_id = drive.path_to_id('Path/To/Folder/document.pdf')
# Search for files
results = drive.search({
'name': 'document.pdf',
'type': 'pdf',
'trashed': False
})
# Batch operations
drive.batch_upload(['file1.txt', 'file2.txt'], folder_id)
drive.batch_delete(['file_id1', 'file_id2'])
credentials.json
: Ensure the credentials.json
file is placed in the working directory.token.json
file and re-authenticate.gdrive-core
is released under the MIT License.
FAQs
A minimal, functional Google Drive API wrapper.
We found that gdrive-core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.