Drive Service

The Drive service provides a simple interface for managing files and folders in Google Drive.

Getting Started

python
from gspace import GSpace

gspace = GSpace.from_oauth(
    credentials_file="credentials.json",
    scopes=["drive"]
)

drive = gspace.drive()

Listing Files

python
# List all files
files = drive.list_files(page_size=10)

for file in files:
    print(f"{file.get('name')} - {file.get('mimeType')}")

Uploading Files

python
# Upload a file
file = drive.upload_file(
    file_path="/path/to/document.pdf",
    name="Important Document",
    description="Project proposal document"
)

print(f"File uploaded with ID: {file.get('id')}")

Downloading Files

python
# Download a file
drive.download_file(
    file_id="file_id_here",
    output_path="/path/to/downloaded_file.pdf"
)

Creating Folders

python
# Create a folder
folder = drive.create_folder(
    name="Project Files",
    description="All project-related documents"
)

print(f"Folder created with ID: {folder.get('id')}")

Sharing Files

python
# Share a file with specific user
drive.share_file(
    file_id=file.get('id'),
    email="collaborator@company.com",
    role="writer"  # or "reader", "commenter"
)

# Share with anyone with link
drive.share_file(
    file_id=file.get('id'),
    role="reader",
    allow_file_discovery=False
)

Searching Files

python
# Search for files
files = drive.search_files(
    query="name contains 'report' and modifiedTime > '2024-01-01'"
)

# More search examples
recent_files = drive.search_files(
    query="modifiedTime > '2024-01-01T00:00:00'",
    order_by="modifiedTime desc"
)

Managing File Metadata

python
# Get file metadata
file_info = drive.get_file(file_id="file_id_here")

# Update file metadata
updated_file = drive.update_file(
    file_id="file_id_here",
    name="Updated Document Name",
    description="Updated description"
)

File Operations

python
# Copy a file
copied_file = drive.copy_file(
    file_id="source_file_id",
    name="Copy of Document"
)

# Move a file to a folder
drive.move_file(
    file_id="file_id_here",
    folder_id="destination_folder_id"
)

# Delete a file
drive.delete_file(file_id="file_id_here")

Working with File Permissions

python
# List permissions
permissions = drive.list_permissions(file_id="file_id_here")

# Delete a permission
drive.delete_permission(
    file_id="file_id_here",
    permission_id="permission_id_here"
)

Best Practices

  1. Use search instead of listing - When looking for specific files, use search queries instead of listing all files
  2. Handle pagination - Use page_size and page_token for large result sets
  3. Batch operations - Use batch requests for multiple operations
  4. Error handling - Always wrap operations in try-except blocks

Common Queries

python
# Find PDF files
pdf_files = drive.search_files(query="mimeType='application/pdf'")

# Find files in a specific folder
folder_files = drive.search_files(
    query=f"'{folder_id}' in parents"
)

# Find recently modified files
recent = drive.search_files(
    query="modifiedTime > '2024-01-01T00:00:00'",
    order_by="modifiedTime desc",
    page_size=10
)