Skip to main content

A package to interact with the Microsoft Graph API for personal OneDrives.

Project description

OneDrive Personal SDK

OneDrive Personal SDK is a Python library for interacting with a personal OneDrive through the Graph API.

Usage

Getting an authentication token

The library is built to support different token providers. To add authentication to the library you need to define a get_access_token method and pass that to the library.

To use msal as a token provider you can create that function like the following:

from msal import PublicClientApplication

app = PublicClientApplication(
    "Client ID",
    authority="https://login.microsoftonline.com/consumers",
)


async def get_access_token(self) -> str:
    result = app.acquire_token_interactive(
        scopes=[
            "Files.ReadWrite.All",
        ]
    )
    return result["access_token"]

Creating a client

To create a client you need to provide a function to retrieve an access token.

from onedrive_personal_sdk import OneDriveClient

client = OneDriveClient(get_access_token)

# can also be created with a custom aiohttp session
client = OneDriveClient(get_access_token, session=session)

Calling the API

The client provides methods to interact with the OneDrive API. The methods are async and return the response from the API. Most methods accept item ids or paths as arguments.

root = await client.get_drive_item("root") # Get the root folder
item = await client.get_drive_item("root:/path/to/item:") # Get an item by path
item = await client.get_drive_item(item.id) # Get an item by id

folder = await client.get_drive_item("root:/path/to/folder:") # Get a folder

# List children of a folder
children = await client.list_children("root:/path/to/folder:")
children = await client.list_children(folder.id)

Uploading files

Smaller files (<250MB) can be uploaded with the upload method. All files need to be provided as AsyncIterators. Such a generator can be created from aiofiles.

import aiofiles
from collections.abc import AsyncIterator

async def file_reader(file_path: str) -> AsyncIterator[bytes]:
    async with aiofiles.open(file_path, "rb") as f:
        while True:
            chunk = await f.read(1024)  # Read in chunks of 1024 bytes
            if not chunk:
                break
            yield chunk
await client.upload_file("root:/TestFolder", "test.txt", file_reader("test.txt"))

Larger files (and small files as well) can be uploaded with a LargeFileUploadClient. The client will handle the upload in chunks.

import os
from onedrive_personal_sdk import LargeFileUploadClient
from onedrive_personal_sdk.models.upload import FileInfo

filename = "testfile.txt"
size = os.path.getsize(filename)

file = FileInfo(
    name=filename,
    size=size,
    folder_path_id="root",
    content_stream=file_reader(filename),
)
file = await LargeFileUploadClient.upload(get_access_token, file)

Adaptive Chunk Size

The LargeFileUploadClient supports adaptive chunk sizing to optimize upload performance based on your connection speed. When enabled, the client dynamically adjusts the chunk size to target approximately 5 seconds per chunk upload.

file = await LargeFileUploadClient.upload(
    get_access_token,
    file,
    smart_chunk_size=True,  # Enable adaptive chunk sizing
    upload_chunk_size=320 * 1024,  # Initial chunk size (optional, default: 5.2MB)
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onedrive_personal_sdk-0.1.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

onedrive_personal_sdk-0.1.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file onedrive_personal_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: onedrive_personal_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for onedrive_personal_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9ed6caae181824c215c4a91fd557f0fa6212d352a858ae5ee5a4c58ce237e215
MD5 2caa88a15dad74b54e4548e48a715307
BLAKE2b-256 5a418ab86525894eb38c7f20eaafc9f7c248ac17eec06d3340f0989c7c130f89

See more details on using hashes here.

Provenance

The following attestation bundles were made for onedrive_personal_sdk-0.1.1.tar.gz:

Publisher: pypi.yaml on zweckj/onedrive-personal-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onedrive_personal_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for onedrive_personal_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6d6dc979605df966bc4b3ae1079fd80866362000dafce78578a74b0ff2b6372
MD5 0b992794aff2150c46f91a47a12a6904
BLAKE2b-256 eefa7d99855d55a2e043508206a39bb894250807264115ec98977b5645add3de

See more details on using hashes here.

Provenance

The following attestation bundles were made for onedrive_personal_sdk-0.1.1-py3-none-any.whl:

Publisher: pypi.yaml on zweckj/onedrive-personal-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page