Thorn Tech Marketing Ad
Skip to main content
Version: Next

Uploading Files via the API

Overview

In this article, we'll go over the process of how to upload files to StorageLink via the API.

In my examples, I am going to be using Postman, which you can download here: https://www.postman.com/downloads/

View the API documentation

Here is a link to the StorageLink API documentation file (it will open in your browser): https://thorntech-public-documents.s3.amazonaws.com/storagelink/KB/storagelink-api-documentation-swagger.json

This file is in OpenAPI JSON format, which is designed to be viewed using Swagger. Go to http://editor.swagger.io/, an online Swagger viewer.

Paste in the JSON API. When prompted to convert your JSON to YAML, click OK. You will then see the API documentation for StorageLink.

info

When running these commands from your workstation/Postman, make sure your IP address is whitelisted for the port 443 rule in your Security Group/Firewall since that's the port we're sending the requests to.

Generating the OAuth token

In order to make the necessary API calls, we need to obtain an OAuth token, which can be generated via Postman.

To generate the OAuth token, you will need 4 values:

security.client-id
security.client-secret
Web Admin Username
Web Admin Password

To get the security client id & secret, they are located on the server at /opt/swiftgw/application.properties and to show these values you can run the command:

cat /opt/swiftgw/application.properties

Then, in Postman, use the POST method and set the endpoint as this value (Use your own IP/Hostname):

https://172.172.244.176/backend/login

Under the Authorization tab, select Basic Auth and put your client id as the Username and client secret as the Password.

Next, go to the Body tab and use the x-www-form-urlencoded option. Then, enter in these Key & Values:

grant_type: The literal string: password
username: The username of the web admin user
password: The password of the web admin user

If you click Send, this will then generate the OAuth / access_token:

Now that we have the OAuth token, we can upload files via the API, which is the next section.

Also, here is the curl command for my example:

curl --silent --location 'https://172.172.244.176/backend/login' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Authorization: Basic SjFBWFRSTzdGQ0VWTTBPSTpQV1RZVTMzQlFMM1JWUFY1' --data-urlencode 'grant_type=password' --data-urlencode 'username=admin' --data-urlencode 'password=Password123!'

Uploading files via the API

In Postman, open a new tab and use this endpoint:

https://172.172.244.176/backend/1.0.0/filesystem/

The above endpoint points to the root / directory. If you'd like to upload to a specific path, specify it after the last forward slash, for example:

https://172.172.244.176/backend/1.0.0/filesystem/Uploads/Bryce

In the new tab, set the Authorization tab to use a Bearer Token and paste in the access_token you generated in the previous section.

Next, go to the Body tab and this time we will use the form-data option.

For the Key, set this to data and change it from Text to File. This way, you're able to select a file from your local machine to upload to the server.

Once you've selected the files from your local machine, to upload them, click Send. The server will return a 204 No Content response, but if you check StorageLink, those files will be uploaded.

Here is an example screenshot of me uploading a file called API-Testing-StorageLink.txt to the /Uploads/Bryce directory.

View in StorageLink:

Also, here is the curl command for my example:

curl --silent --location 'https://172.172.244.176/backend/1.0.0/filesystem/Uploads/Bryce' --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImF1ZCI6IkoxQVhUUk83RkNFVk0wT0kiLCJyZXNldFBhc3N3b3JkIjpmYWxzZSwibmJmIjoxNzA5OTMxODI1LCJpc3MiOiJodHRwczovL3Rob3JudGVjaC5jb20iLCJleHAiOjE3MDk5NjA2MjUsImlhdCI6MTcwOTkzMTgyNSwibm9uY2UiOiI5MDYzMjJkMi04ODZiLTRhYWItOTY0NC01NDdmNWE1MGQ0ZjciLCJhdXRob3JpdGllcyI6WyJST0xFX0FETUlOIiwiUk9MRV9TWVNURU1fQURNSU4iXX0.7UF6_cE7EYpH1HJGddlYT15_RMY-yMYO6QazPWxZ9wQ' --form 'data=@"/C:/Users/bryce/Downloads/API-Testing-StorageLink.txt"'