import os
import requests
def upload_file(base_url, api_key, namespace, repo_name, branch,
local_file_path, remote_file_path, commit_message,
author_name, author_email):
# Check if file exists
if not os.path.exists(local_file_path):
print(f"✗ File not found: {local_file_path}")
return None
# Construct URL with branch and directory path
resource = f"{branch}/{remote_file_path}"
url = f"{base_url}/api/repos/{namespace}/{repo_name}/file/{resource}"
headers = {
'Authorization': f'Bearer {api_key}'
}
# Prepare file for upload
files = [
("file", (os.path.basename(local_file_path), open(local_file_path, 'rb')))
]
payload = {
'message': commit_message,
'name': author_name,
'email': author_email
}
response = requests.put(url, data=payload, files=files, headers=headers)
# Close the file
files[0][1][1].close()
if response.status_code == 200:
result = response.json()
print(f"✓ File uploaded successfully")
if 'commit' in result:
print(f" Commit ID: {result['commit'].get('id', 'N/A')}")
return result
else:
print(f"✗ Failed: {response.status_code}")
print(response.text)
return None
# Usage
upload_file(
base_url="https://hub.oxen.ai",
api_key="your-api-key",
namespace="your-username",
repo_name="my-dataset",
branch="main",
local_file_path="./example.txt",
remote_file_path="data/example.txt",
commit_message="Add example file",
author_name="Your Name",
author_email="[email protected]"
)