Skip to content

IoT File Client for Java

Introduction

The IoT File Java client allows you to manage files related to assets. Refer to IoT File Service for more information about the service.

Further implementation of the IoT file client SDK library has been shown in a sample project that you can download and test in local or on Insights Hub application. Please refer to this repository: industrial-iot-java-sdk-examples

Hint

In the IoT context, assets are referred to as entity and aspects as propertyset.
Placeholders in the following samples are indicated by angular brackets < >.

File Operations

Client name: FileServiceClient

Create or Update a File

This section shows two options for creating or updating a file with the provided content for an asset in the specified path.

// Construct the FileServiceClient object
FileServiceClient file_service_client = FileServiceClient.builder()
                                            .mindsphereCredentials(<credentials>)
                                            .restClientConfig(<config>)
                                            .build();

try {
  file_service_client.putFile(<bytes_array>, <entity_id>, <file_path>, <ifMatch>, <timestamp>, <description>, <type>);
} catch (MindsphereException e) {
  // Exception handling
}

Alternatively, use the PutFileRequest model.

// Construct the FileServiceClient object as shown above
PutFileRequest request_object = new PutFileRequest();
request_object.setFile(<bytes_array>);
request_object.setEntityId(<entity_id>);
request_object.setFilepath(<file_path>);
request_object.setTimestamp("2018-09-10T05:03:42.363Z");
request_object.setDescription("lorem test3");
request_object.setType("txt");

try {
  file_service_client.putFile(request_object);
} catch (MindsphereException e) {
  // Exception handling
}

Read a File

This section shows two options for reading a file for an asset from the specified path.

// Construct the FileServiceClient object as shown above
byte[] file_data;
try {
    file_data = file_service_client.getFile(<entity_id>, <file_path>, <ifNoneMatch>);
} catch (MindsphereException e) {
    // Exception handling
}

Alternatively, use the GetFileRequest model.

// Construct the FileServiceClient object as shown above
GetFileRequest request_object = new GetFileRequest();
request_object.setEntityId(<entity_id>);
request_object.setFilepath(<file_path>);

byte[] fileData;
try {
    fileData = file_service_client.getFile(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

Delete a File

This section shows two options for deleting a file from an asset at the specified path.

// Construct the FileServiceClient object as shown above
try {
    file_service_client.deleteFile(<entity_id>, <file_path>)
} catch (MindsphereException e) {
    // Exception handling
}

Alternatively, use the DeleteFileRequest model.

// Construct the FileServiceClient object as shown above
DeleteFileRequest request_object = new DeleteFileRequest();
request_object.setEntityId(<entity_id>);
request_object.setFilepath(<file_path>);

try {
    file_service_client.deleteFile(request_object)
} catch (MindsphereException e) {
    // Exception handling
}

Search a File

This section shows two options for searching one or multiple files of an asset in the specified path.

// Construct the FileServiceClient object as shown above
List<File> file_search_response;
try {
    file_search_response = file_service_client.searchFiles(<entity_id>, <offset>, <limit>, <count>, <order>, <filter>);
} catch (MindsphereException e) {
    // Exception handling
}

Alternatively, use the GetFileRequest model.

// Construct the FileServiceClient object as shown above
SearchFilesRequest request_object = new SearchFilesRequest();
request_object.setEntityId(<entity_id>);

List<File> file_search_response;
try {
    file_search_response = file_service_client.searchFiles(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

List Multi Part Uploads

This section shows two options for listing multi part uploads.

// Construct the FileServiceClient object as shown above
List<Fileslist> file_list;
try {
    file_list = file_service_client.getFileList(<entity_id>, <file_path>);
} catch (MindsphereException e) {
    // Exception handling
}

Alternatively, use the GetFileListRequest model.

//Create client object "fileServiceClient" as shown above
GetFileListRequest request_object = new GetFileListRequest();
request_object.setEntityId(<entity_id>);
request_object.setFilepath(<file_path>);

List<Fileslist> file_list;
try {
    file_list = file_service_client.getFileList(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

Initiate a New Multi Part Upload

Initiate a new multi part file upload for an asset in the specified path.

// Construct the FileServiceClient object as shown above
// Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>).filepath(<file_path>);

try {
    file_service_client.initiateMultiPartUpload(request_object);
} catch(MindsphereException e) {
    // Exception handling
}

Upload Parts for a Multi Part Upload

Upload parts of an initiated multi part upload with the provided content in the specified path using the PutFileRequest model.

// Construct the PutFileRequest object
PutFileRequest request_object = new PutFileRequest()
                               .entityId(<entity_id>).filepath(<file_path>)
                               .type(<type_of_file>).file(<file_as_bytes>).part(<part_no>)
                               .timestamp(<timestamp>).description(<description>);

try {
    file_service_client.createMultiPartFile(request_object);
} catch(MindsphereException e) {
    // Exception handling
}

Complete a Multi Part Upload

Complete a multi part upload using the PutFileRequest model.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>).filepath(<file_path>);

try {
    file_service_client.completeMultiPartUpload(request_object);
} catch(MindsphereException e) {
    // Exception handling
}

Abort a Multi Part Upload

Abort a multi part upload using the PutFileRequest model.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>).filepath(<file_path>);

try {
    file_service_client.abortMultiPartUpload(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

Initiate the Update of an Existing Multi Part Upload

To update an existing multi part upload, it is required to re-initiate the multi part upload for an asset in the specified path. Note that the ifMatch parameter is mandatory for this action.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>)
                                 .filepath(<file_path>).ifMatch(<ifMatch>);

try {
    file_service_client.initiateMultiPartUpload(request_object);
} catch(MindsphereException e) {
    // Exception handling
}

Update an Existing Multi Part Upload

Update a part an existing multi part upload with the provided content in the specified path using the PutFileRequest model. Note that the ifMatch parameter is mandatory for this action.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest()
                               .entityId(<entity_id>).filepath(<file_path>).ifMatch(<ifMatch>)
                               .type(<type_of_file>).file(<file_as_bytes>).part(<part_no>)
                               .timestamp(<timestamp>).description(<description>);

try {
    file_service_client.updateMultiPartFile(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

Complete the Update of an Existing Multi Part Upload

Complete the update of an existing multi part upload using the PutFileRequest model. Note that the ifMatch parameter is mandatory for this action.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>)
                                 .filepath(<file_path>).ifMatch(<ifMatch>);

try {
    file_service_client.initiateMultiPartUpload(request_object);
} catch(MindsphereException e) {
    // Exception handling
}

Abort the Update of an Existing Multi Part Upload

Abort the update of an existing multi part file upload using the PutFileRequest model. Note that the ifMatch parameter is mandatory for this action.

// Construct the PutFileRequest object as shown above

//Create the request object
PutFileRequest request_object = new PutFileRequest().entityId(<entity_id>).filepath(<file_path>).ifMatch(<ifMatch>);

try {
    file_service_client.abortMultiPartUpload(request_object);
} catch (MindsphereException e) {
    // Exception handling
}

Last update: February 23, 2024

Except where otherwise noted, content on this site is licensed under the Development License Agreement.