REST APIs on File Repository
Introduction
We have already seen how you can upload, edit and share files using the platform. Well then, there is a REST API available to perform these functions.
We will first see how to use this API directly, using a REST client such as Postman; then we will see how we can use this REST API through the available Java client.
Authentication
As you need authentication to perform these operations, except for GET in some cases, you need to authenticate using OAuth. To do this, you must send a GET request to the path '/api-ops/login/username/{username}/password/{password}', replacing the parameters in {curly braces}.
Â
If the credentials are correct, you will receive an access token, "access_token".This is the token you will use in all your requests as an authentication header.
This header will look like this:
NOTE: Remember to include "Bearer" before the access token.
CRUD Operation
Uploading a file (POST)
To upload a post, you must make a POST request to the 'controlpanel/binary-repository' endpoint, with two parameters in the body:
Binary file ("file", mandatory). Maximum size 50 MB.
Metadata ("metadata", optional). This parameter must be sent in text format.
Repository ("repository", optional, default defecto Mongo Grid FS). This parameter specifies where you want to stgore the binary files. There are two options, "MONGO_GRIDFS", and "FILE". By default, it's stored in Mongo.
Plus the authentication header.
You will receive in return an identifier that can be used to edit and upload the file later.
Downloading a file (GET)
To download a binary file, you need to have that file's identifier, then make a GET request to the '/controlpanel/binary-repository/{id}' endpoint, replacing id with the file's identifier, which will be returned by the platform after every insert.
If the binary file is not public, then you need to insert the authentication header, same as in the other operations.
If you write this link in a browser, you will receive the binary file instead of the JSON.
Editing a file (PUT)
If you have permissions to modify a file, then you can do it in the '/controlpanel/binary-repository/{id}' endpoint, with PUT method, replacing id with the binary file's identifier.
Same as when uploading files, you need to specify:
Metadata (optional).
Binary file ("file"). Maximum size 50MB.
The file will be updated if you receive a HTTP 202.
Deleting a file (DELETE)
Lastly, to delete a file on which you have permissions, you need to do a DELETE request to the '/controlpanel/binary-repository/{id}' endpoint, replacing id with the binary file's identifier.
Same as with the PUT, you will receive a HTTP 202 if everything went fine and the file was deleted from the platform.
Using the Java API
If you intend to work with binaries from Java, there is an API you can use.
This API contains the operations this article previously described.
To use it, you need to import the two classes from the package:
- import com.minsait.onesait.platform.binaryrepository.*;
This package contains:
Client:Â BinaryRepositoryClient.java , in charge of implementing the binary repository CRUD via REST.
Model:Â BinaryDataFile.java , this model will be used to encapsulate the binary file that will be requested to the platform.
Client initialization
You will need to initialize the client, providing your user and password, along with the server or domain where the platform is deployed, as the arguments:
The client will reach OAuth authentication with your credentials.
Uploading a File
To upload a file, you only need to call the client's addBinaryFile(File file, String metadata) method. The maximum file size is 50MB.
Downloading a File
To download a file, you must use the getBinary(String id) method, which will return a BinaryDataFile -type object, with the binary encapsulated in it.
Editing a File
To update a file, you must use the updateBinaryFile(String id, File newFile, String newMetadata) method. The maximum file size is50MB.
Deleting a File
Lastly, to delete a file, you must call the removeBinaryFile(String id) method.