List Files via API
How to Use the Python Script to List Files
This article guides you through the process of using a Python script to list files from an API. The script requires specific credentials that you can obtain from your server's configuration files.
Obtaining CLIENT_ID and CLIENT_SECRET
To authenticate and interact with the API, you will need a CLIENT_ID
and CLIENT_SECRET
. These can be obtained by accessing your server's configuration files:
SSH into your server where the API is hosted.
Once logged in, open the configuration file by running:
cat /opt/swiftgw/application.properties
Locate the entries for CLIENT_ID
and CLIENT_SECRET
within the file. Note these down as they will be used in your script to authenticate API requests.
Using the Provided Script
Below is the Python script you will use to list files from the API. It is pre-configured with placeholder values for the variables you will replace with actual credentials and paths:
import requests
import base64
import urllib3
# Suppress warnings for self-signed SSL certificates
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Variables to be configured
BASE_URL = "https://3.238.156.250/backend"
CLIENT_ID = "Your_CLIENT_ID_here"
CLIENT_SECRET = "Your_CLIENT_SECRET_here"
USERNAME = "Your_Username_here"
PASSWORD = "Your_Password_here"
DIRECTORY_PATH = "/desired/directory/path"
def get_oauth_token():
url = f"{BASE_URL}/login"
credentials = f"{CLIENT_ID}:{CLIENT_SECRET}"
auth_header = base64.b64encode(credentials.encode()).decode()
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": f"Basic {auth_header}"
}
data = {
"grant_type": "password",
"username": USERNAME,
"password": PASSWORD
}
response = requests.post(url, headers=headers, data=data, verify=False)
if response.status_code == 200:
return response.json().get("access_token")
else:
print(f"Error getting token: {response.text}")
return None
if __name__ == "__main__":
token = get_oauth_token()
def list_files(token):
"""List files in a specific directory in StorageLink."""
url = f"{BASE_URL}/1.0.0/filesystem{DIRECTORY_PATH}"
headers = {
"Authorization": f"Bearer {token}"
}
response = requests.get(url, headers=headers, verify=False)
if response.status_code == 200:
response_json = response.json()
files = response_json.get('content', []) # Access 'content' which contains files and directories
# Get the current directory from the first element if available, otherwise use DIRECTORY_PATH
current_directory = files[0]['absolutePath'] if files else DIRECTORY_PATH
print(f"Current Directory: {current_directory}")
print("Files:")
for file in files:
if file['type'] == 'REGULAR':
# For regular files, print only name and size
print(f"{file['name']} - {file['size']} bytes")
elif file['type'] == 'DIRECTORY':
# For directories, include the type
print(f"{file['name']} (Type: {file['type']})")
else:
print(f"Error listing files: {response.text}")
if __name__ == "__main__":
token = get_oauth_token()
if token:
list_files(token)
Replace the placeholder values in the script with the credentials you obtained and the specific paths relevant to your API's environment.
Executing the Script
To execute the script:
Save the script to a local file with a .py extension, for example, list_files.py
.
Open a command line interface (CLI), navigate to the directory where your script is saved.
Run the script by typing:
python list_files.py
This will execute the script, and it should output the authentication status followed by the files listed from the specified directory if successful.
Understanding and Configuring Script Variables
The script contains several variables that you need to configure according to your environment. Here's what each variable means and how you can adjust them:
BASE_URL
: The URL of the API backend. Replace this with the URL provided to you by your API administrator.CLIENT_ID
andCLIENT_SECRET
: These are credentials provided to you by the API provider. They are used to authenticate your script's access to the API.USERNAME
andPASSWORD
: Your API user credentials. These may differ depending on your role or the permissions assigned to you.DIRECTORY_PATH
: The path to the directory whose files you want to list. Change this path based on where the files are stored in your API's file system.