Thorn Tech Marketing Ad
Skip to main content
Version: 1.2.1

Batch Add Users from a CSV File

Draft

This article is a draft and has not been reviewed yet. Please verify all steps before following them in production.

Overview

StorageLink does not have a built-in CSV import feature, but you can use the REST API to create users programmatically. This article provides a Python script that reads users from a CSV file and creates them via the API.

Prerequisites

  • Python 3.6+
  • The requests library (pip install requests)
  • Admin credentials for the StorageLink web interface
  • The security.client-id and security.client-secret from /opt/swiftgw/application.properties on the StorageLink server
info

Make sure your IP address is whitelisted in the Security Group / Firewall for port 443, since the script sends requests over HTTPS.

Step 1: Prepare the CSV File

Create a CSV file called users.csv with the following format:

username,password,homeFolderPath,canList,canUpload,canDownload,canOverwriteDelete
alice,P@ssw0rd123,/users/alice,true,true,true,false
bob,S3cur3!Pass,/users/bob,true,true,false,false
charlie,Ch@ng3Me1,/data/charlie,true,true,true,true

Column reference:

ColumnRequiredDescription
usernameYesThe username for the new user
passwordYesThe user's password
homeFolderPathNoHome folder path (defaults to /users/<username>)
canListNoList files permission (defaults to true)
canUploadNoUpload permission (defaults to true)
canDownloadNoDownload permission (defaults to true)
canOverwriteDeleteNoOverwrite/delete permission (defaults to false)

Step 2: Get the Client Credentials

On the StorageLink server, run:

cat /opt/swiftgw/application.properties

Look for the security.client-id and security.client-secret values. You will need these in the next step.

Step 3: Create the Script

Save the following script as batch-create-users.py:

import csv
import sys
import requests
from urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# ---- Configuration (update these values) ----
STORAGELINK_HOST = "https://YOUR-STORAGELINK-IP"
CLIENT_ID = "your-client-id"
CLIENT_SECRET = "your-client-secret"
ADMIN_USERNAME = "admin"
ADMIN_PASSWORD = "your-admin-password"
CSV_FILE = "users.csv"
# ----------------------------------------------

API_URL = f"{STORAGELINK_HOST}/backend"


def get_access_token():
"""Authenticate and return an OAuth access token."""
response = requests.post(
f"{API_URL}/login",
auth=(CLIENT_ID, CLIENT_SECRET),
data={
"grant_type": "password",
"username": ADMIN_USERNAME,
"password": ADMIN_PASSWORD,
},
verify=False,
)
response.raise_for_status()
return response.json()["access_token"]


def create_user(token, user_data):
"""Create a single user via the StorageLink API."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
response = requests.post(
f"{API_URL}/1.0.0/users",
headers=headers,
json=user_data,
verify=False,
)
response.raise_for_status()
return response.json()


def parse_bool(value, default=False):
"""Parse a boolean string from CSV."""
if not value:
return default
return value.strip().lower() in ("true", "1", "yes")


def main():
csv_file = CSV_FILE
if len(sys.argv) > 1:
csv_file = sys.argv[1]

token = get_access_token()
print("Authenticated successfully.\n")

success_count = 0
fail_count = 0

with open(csv_file, newline="") as f:
reader = csv.DictReader(f)
for row in reader:
username = row["username"].strip()

user_data = {
"username": username,
"password": row["password"],
"enabled": True,
"homeFolderPermissions": {
"listable": parse_bool(row.get("canList"), default=True),
"uploadable": parse_bool(row.get("canUpload"), default=True),
"downloadable": parse_bool(row.get("canDownload"), default=True),
"deletable": parse_bool(row.get("canOverwriteDelete"), default=False),
},
}

home_folder = row.get("homeFolderPath", "").strip()
if home_folder:
user_data["homeFolderPath"] = home_folder

try:
result = create_user(token, user_data)
print(f" Created user: {username} (id: {result.get('id')})")
success_count += 1
except requests.exceptions.HTTPError as e:
print(f" FAILED to create user: {username} - {e.response.text}")
fail_count += 1

print(f"\nDone. {success_count} created, {fail_count} failed.")


if __name__ == "__main__":
main()

Step 4: Run the Script

# Install the requests library (if not already installed)
pip install requests

# Run the script with the default CSV file (users.csv)
python batch-create-users.py

# Or specify a different CSV file
python batch-create-users.py my-users.csv

Example output:

Authenticated successfully.

Created user: alice (id: 12)
Created user: bob (id: 13)
Created user: charlie (id: 14)

Done. 3 created, 0 failed.

Tips

  • Test with one user first. Create a CSV with a single test user and verify it works before importing a large list.
  • Password requirements. Make sure passwords meet your organization's complexity requirements.
  • Home folder paths. If the homeFolderPath is omitted, StorageLink defaults to /users/<username>.
  • Duplicate usernames. The API will return an error if the username already exists. The script will log the failure and continue with the remaining users.
  • SSL certificates. The script disables SSL verification (verify=False) for self-signed certificates. If you have a valid SSL certificate, you can remove the verify=False parameter.

API Reference

For more details on the StorageLink REST API, see Uploading Files via the API.

The full API specification is available in OpenAPI format: https://thorntech-public-documents.s3.amazonaws.com/storagelink/KB/storagelink-api-documentation-swagger.json

You can view it in the Swagger Editor.