A Python library for synchronizing files between a local directory and a UbiOps bucket. This library provides utilities for downloading files from UbiOps buckets, uploading files to buckets, and automatically watching local directories for changes to keep them in sync with remote storage.
Project description
UbiOps File Sync
A Python library for synchronizing files between a local directory and a UbiOps bucket. This library provides utilities for downloading files from UbiOps buckets, uploading files to buckets, and automatically watching local directories for changes to keep them in sync with remote storage.
Features
- File Download: Download individual files or entire directories from a UbiOps bucket to a local folder
- File Upload: Upload individual files or entire directories from a local folder to a UbiOps bucket
- Automatic Synchronization: Watch a local directory for file changes and automatically upload new or modified files
- Smart Conflict Resolution: Optionally preserve newer files when syncing to avoid overwriting recent changes
- Retry Logic: Built-in exponential backoff retry mechanism for handling network errors and API exceptions
- Parallel Operations: Bulk upload and download operations use parallel processing for improved performance
Installation
Install the library using pip:
pip install ubiops-file-sync
Or using uv:
uv pip install ubiops-file-sync
Configuration
The library is configured via environment variables. You need to set the following variables before using the library:
| Environment Variable | Description | Required |
|---|---|---|
UBIOPS_API_HOST |
UbiOps API host URL (default: https://api.ubiops.com/v2.1) |
No |
UBIOPS_API_TOKEN |
UbiOps API token (with or without "Token " prefix) | Yes |
BUCKET_PROJECT_NAME |
Name of the UbiOps project containing the bucket | Yes |
BUCKET_NAME |
Name of the UbiOps bucket to sync with | Yes |
BUCKET_DIR |
Directory/prefix within the bucket to sync (can be empty for root) | Yes |
LOCAL_SYNC_DIR |
Local directory path to sync with the bucket | Yes |
OVERWRITE_NEWER_FILES |
Whether to overwrite newer files. Set to true/1/yes to enable smart conflict resolution (preserves newer files), or false/0/no to always overwrite |
Yes |
Configuration Example
export UBIOPS_API_TOKEN="your_api_token_here"
export BUCKET_PROJECT_NAME="my-project"
export BUCKET_NAME="my-bucket"
export BUCKET_DIR="data"
export LOCAL_SYNC_DIR="/path/to/local/folder"
export OVERWRITE_NEWER_FILES="true"
Usage
Downloading Files
Download a Single File
Download a specific file from the UbiOps bucket:
from ubiops import FileItem
from ubiops_file_sync.downloader import download_file
remote_file = FileItem(file="stub.txt")
download_file(remote_file)
Download All Files from Bucket
Download all files from the configured bucket directory to your local folder:
from ubiops_file_sync.downloader import download_from_bucket
download_from_bucket()
When OVERWRITE_NEWER_FILES is true, this function will:
- Skip downloading files if the local version is newer than the remote version
- Only download files that are newer on the remote or don't exist locally
When OVERWRITE_NEWER_FILES is false, all remote files are downloaded, overwriting local files if they exist.
Uploading Files
Upload a Single File
Upload a specific local file to the UbiOps bucket:
from pathlib import Path
from ubiops_file_sync.uploader import upload_file
upload_file(local_path=Path("tests/input/another_stub.txt"))
Upload All Files to Bucket
Upload all files from your local sync directory to the UbiOps bucket:
from ubiops_file_sync.uploader import upload_to_bucket
upload_to_bucket()
When OVERWRITE_NEWER_FILES is true, this function will:
- Skip uploading files if the remote version is newer than the local version
- Only upload files that are newer locally or don't exist remotely
When OVERWRITE_NEWER_FILES is false, all local files are uploaded, overwriting remote files if they exist.
Watching for Changes
Watch Local Directory and Auto-Upload
Continuously monitor the local sync directory for file changes and automatically upload new or modified files:
from ubiops_file_sync.watcher import watch_local_and_upload, shutdown
# Start watching for changes
watch_local_and_upload()
# ... your application code ...
# Gracefully shut down the watcher when done
shutdown()
The watcher:
- Runs in a background thread
- Monitors the local directory recursively
- Automatically uploads files when they are closed (saved)
- Respects the
OVERWRITE_NEWER_FILESsetting - Uses a queue-based system for reliable file processing
Note: The watcher will automatically clean up when the program exits via atexit, but you can also call shutdown() explicitly for graceful termination.
Full Sync and Watch
Perform an initial download from the bucket and then start watching for local changes:
from ubiops_file_sync.sync import sync_and_watch
# Download all files from bucket, then start watching for local changes
sync_and_watch()
This is a convenience function that:
- First downloads all files from the remote bucket to the local directory
- Then starts the file watcher to monitor for new or changed local files
- Automatically handles shutdown on program exit
Complete Example
Here's a complete example showing typical usage:
from pathlib import Path
from ubiops import FileItem
from ubiops_file_sync.downloader import download_file, download_from_bucket
from ubiops_file_sync.uploader import upload_file, upload_to_bucket
from ubiops_file_sync.watcher import shutdown, watch_local_and_upload
# Download a specific file
remote_file = FileItem(file="stub.txt")
download_file(remote_file)
# Download all files from bucket
download_from_bucket()
# Upload a specific file
upload_file(local_path=Path("tests/input/another_stub.txt"))
# Upload all local files to bucket
upload_to_bucket()
# Start watching for changes
watch_local_and_upload()
# ... your application continues running ...
# Clean shutdown when done
shutdown()
How It Works
File Comparison Logic
When OVERWRITE_NEWER_FILES is enabled (true), the library compares file modification times to determine which version is newer:
- For downloads: If a local file exists and its modification time is newer than the remote file's creation time, the download is skipped.
- For uploads: If a remote file exists and its creation time is newer than the local file's modification time, the upload is skipped.
This ensures that newer changes are preserved in both directions.
Retry Mechanism
All network operations use exponential backoff retry logic to handle transient errors:
- Maximum retry attempts: 5
- Maximum retry time: 300 seconds (600 seconds for file listing)
- Handles:
RequestException,Timeout,ConnectionError, andUbiOps.ApiException
Directory Structure
The library maintains the directory structure between local and remote storage. Files in subdirectories are preserved, and the BUCKET_DIR acts as a prefix for all remote file paths.
Requirements
- Python >= 3.12
- UbiOps API access and credentials
- A configured UbiOps bucket
Dependencies
backoff: Exponential backoff retry logicpydantic: Configuration validationubiops: UbiOps Python client librarywatchdog: File system monitoring for the QT event loop integration
License
See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ubiops_file_sync-0.1.6.tar.gz.
File metadata
- Download URL: ubiops_file_sync-0.1.6.tar.gz
- Upload date:
- Size: 72.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8dcad1a95bad65a5738e9a3eab2f900a7b6687c83f608805060b63a2ba6a501
|
|
| MD5 |
8d7ef5ff5ace0341b95d245d0eda6c4e
|
|
| BLAKE2b-256 |
97539d48372901274609bcd76d437e26ef2a5c5839560664d04f79150f4c9064
|
File details
Details for the file ubiops_file_sync-0.1.6-py3-none-any.whl.
File metadata
- Download URL: ubiops_file_sync-0.1.6-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cb31c06378a656e34a8d340b60264259dc8b7a4bfaaabd7bb662ef641abfb02
|
|
| MD5 |
4d84cc73160f3c003966e8f7998ed7e0
|
|
| BLAKE2b-256 |
5bf254e2a86eaa3bd771bf02a51fda07d13dea7194887f69da525ccd8db53f6c
|