Microsoft Graph Filesystem (msgraphfs)

0.4 · active · verified Thu Apr 09

msgraphfs is a Python library that provides a filesystem-like interface to Microsoft Graph drives (SharePoint, OneDrive), built on top of `fsspec`. It enables seamless interaction with Microsoft 365 services, treating remote files and folders as if they were local. The current version, 0.4, introduces URL-based filesystem paths and enhanced multi-site access. The library appears to follow a release cadence driven by feature enhancements and bug fixes, with recent releases addressing usability and pagination.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `MSGraphFileSystem` using Azure AD application credentials (client ID, tenant ID, client secret) provided via environment variables. It then performs basic filesystem operations like listing directories, creating a new directory, writing a file, and reading a file. An Azure AD application with appropriate permissions (e.g., `Sites.ReadWrite.All`) and correct site/drive names are crucial for successful execution.

import os
from msgraphfs import MSGraphFileSystem

# Set environment variables for authentication (replace with your actual credentials)
# Ensure your Azure AD app has 'Sites.Read.All' or 'Files.Read.All' permissions.
# For more robust production, consider using a credential management system like Azure Key Vault.
os.environ['MSGRAPHFS_CLIENT_ID'] = os.environ.get('MSGRAPHFS_CLIENT_ID', 'YOUR_CLIENT_ID')
os.environ['MSGRAPHFS_TENANT_ID'] = os.environ.get('MSGRAPHFS_TENANT_ID', 'YOUR_TENANT_ID')
os.environ['MSGRAPHFS_CLIENT_SECRET'] = os.environ.get('MSGRAPHFS_CLIENT_SECRET', 'YOUR_CLIENT_SECRET')

# Initialize the filesystem for a specific SharePoint site and drive
# Replace 'your_sharepoint_site' and 'your_drive_name' with actual values.
# Alternatively, use URL-based paths directly, e.g., 'msgd://site/drive/path/file'.
try:
    fs = MSGraphFileSystem(
        protocol="sharepoint",
        site_name="your_sharepoint_site",
        drive_name="Documents"
    )

    # List contents of the root directory
    print("Listing root directory:")
    files = fs.ls("/", detail=False)
    for f in files:
        print(f)

    # Example: Check if a directory exists and create it if not
    dir_path = "/path/to/my/new_folder"
    if not fs.isdir(dir_path):
        print(f"Directory '{dir_path}' does not exist, creating it...")
        fs.mkdir(dir_path)
        print(f"Directory '{dir_path}' created.")
    else:
        print(f"Directory '{dir_path}' already exists.")

    # Example: Write a simple file
    file_content = "Hello from msgraphfs!"
    file_path = f"{dir_path}/test_file.txt"
    print(f"Writing to {file_path}...")
    with fs.open(file_path, "w") as f:
        f.write(file_content)
    print(f"Content written to {file_path}.")

    # Example: Read the file
    print(f"Reading from {file_path}...")
    with fs.open(file_path, "r") as f:
        read_content = f.read()
    print(f"Read content: '{read_content}'")

except Exception as e:
    print(f"\nAn error occurred: {e}")
    print("Please ensure:")
    print("- Your MSGRAPHFS_CLIENT_ID, MSGRAPHFS_TENANT_ID, and MSGRAPHFS_CLIENT_SECRET environment variables are set.")
    print("- Your Azure AD application has the necessary API permissions (e.g., Sites.ReadWrite.All or Files.ReadWrite.All).")
    print("- 'your_sharepoint_site' and 'your_drive_name' are correct for your Microsoft 365 environment.")

view raw JSON →