Uploading data with Python
This tutorial shows how to upload files to Nexus using the Python programming language via the sync tasks upload endpoint. A script like this can be useful to periodically upload data sources to Nexus that are not connected to the internet or that only need refreshing every once in a while.
Info
This tutorial assumes that you have:
- Python installed on your computer
- obtained a valid Nexus Web API token (with Data Supplier role; see Web API basics for more info)
See Upload limits for the maximum size of the files you can upload this way.
Code¶
We use the 'requests' package for making the actual API calls.
import requests
base_url = "https://nexus.stellaspark.com/api/v1"
token = "{web_api_token}"
# Step 1: find the sync task ID for your uploadable data source
r = requests.get(f"{base_url}/sync_tasks/?uploadable=true&token={token}")
r.raise_for_status()
tasks = r.json()["results"]
sync_task_id = {sync_task_id} # Enter the sync task ID to upload to
# Step 2: upload the file
with open("path/to/my_file.csv", "rb") as f:
r = requests.post(
f"{base_url}/sync_tasks/{sync_task_id}/upload/?token={token}",
files={"upload_file": f},
)
r.raise_for_status() # Throw an error if something goes wrong