Python SDK

🐍 Python SDK for Openfiles

The Openfiles Python SDK provides a simple and intuitive way to interact programmatically with Openfiles and TON Storage.

Installation

Install the SDK via pip:

pip install openfiles

Initialization

Initialize the SDK client using your API token:

from openfiles import OpenfilesClient
 
# Option 1: Pass API token directly
client = OpenfilesClient(api_token="your_api_token")
 
# Option 2: Use environment variable
# export OPENFILES_API_TOKEN="your_api_token"
client = OpenfilesClient()

Methods & Examples

Upload File

Upload a file to Openfiles:

response = client.upload_file(file_path="path/to/file.txt", description="File description")
print("Uploaded file with Bag ID:", response.bag_id)

Upload Folder

Upload an entire folder as a single entity, ideal for static sites:

response = client.upload_folder(folder_path="path/to/folder", description="Folder description")
print("Uploaded folder with Bag ID:", response.bag_id)

Import by Bag ID

Import an existing bag from TON Storage using its ID:

client.add_by_bag_id(bag_id="bag_id_here")
print("Bag imported successfully!")

Download File

Download a file using its Bag ID:

# Save to specific path
client.download_file(bag_id="bag_id_here", destination="downloads/my_file.txt")
 
# Or retrieve content as bytes
file_content = client.download_file(bag_id="bag_id_here")

Delete File

Delete a file from storage:

client.delete_file(bag_id="bag_id_here")

Get User Information

Retrieve your account information:

user_info = client.get_user_info()
print(f"Space left: {user_info.space_left}/{user_info.capacity}")

List User Files

Get a list of files you've uploaded:

files = client.get_user_files_list()
for file in files:
    print(f"Filename: {file.filename}, Size: {file.size}, Uploaded at: {file.uploaded_at}")

Data Models

BagResponse

  • bag_id: Identifier for the uploaded file or folder.

FileInfoResponse

  • filename: Name of the file.
  • size: Size of the file in bytes.
  • uploaded_at: Timestamp when the file was uploaded.
  • description: Description provided during upload.
  • bag_id: Identifier of the file.

UserResponse

  • uid: User identifier.
  • space_left: Remaining storage space.
  • capacity: Total storage capacity.

Error Handling

The SDK handles HTTP errors by raising exceptions. Be prepared to handle exceptions like HTTPError, FileNotFoundError, and validation errors when using the SDK.

Next Steps

Happy coding with the Openfiles Python SDK! 🚀