Skip to content

IoT File Services Client for Python

Introduction

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Import the RestClientConfig and UserToken from mindsphere_core module
from mindsphere_core import RestClientConfig
from mindsphere_core import UserToken

# Import the MindsphereError from mindsphere_core.exceptions module
from mindsphere_core.exceptions import MindsphereError

# Import the FileServiceClient from iotfile module
from iotfile.clients.file_service_client import FileServiceClient

# Import all required models from iotfile.models
from iotfile.models import *

# Create the RestClientConfig and UserToken objects
config = RestClientConfig(proxy_host = "<proxy_host>", proxy_port = <proxy_port>)
credentials = UserToken(authorization = "<bearer_token>")

# Create the FileServiceClient object using the RestClientConfig and UserToken objects
files_client = FileServiceClient(rest_client_config = config, mindsphere_credentials = credentials)

try:
    # Create the request object
    request = PutFileRequest(
        file="<file>",
        if_match="<if_match>",
        filepath="<filepath>",
        description="<description>",
        entity_id="<entity_id>",
        type="<file type>",
        timestamp="<timestamp>"
    )

    # Initiate the API call to write a file
    response = files_client.put_file(request)

except MindsphereError as err:
    # Exception Handling

Read a File

Read a file for an asset from the specified path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = GetFileRequest(
        filepath="<filepath>",
        entity_id="<entity_id>"
    )

    # Initiate the API call to read a file
    response = files_client.get_file(request)

except MindsphereError as err:
    # Exception Handling

Delete a File

Delete a file of an asset and from the specified path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = DeleteFileRequest(
        filepath="<filepath>",
        entity_id="<entity_id>"
    )

    # Initiate the API call to delete a file
    response = files_client.delete_file(request)

except MindsphereError as err:
    # Exception Handling

Search a File

Search one or multiple files of an asset in the specified path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = SearchFilesRequest(
        filter="<filter>",
        offset=<offset>,
        limit=<limit>,
        count=<count>,
        entity_id="<entity_id>",
        order="<order>",
    )

    # Initiate the API call to search a file
    response = files_client.search_files(request)

except MindsphereError as err:
    # Exception Handling

List Multi Part Uploads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = GetFileListRequest(
        filepath="<filepath>",
        entity_id="<entity_id>"
    )

    # Initiate the API call to list files
    response = files_client.get_file_list(request)

except MindsphereError as err:
    # Exception Handling

Initiate a New Multi Part Upload

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>"
    )

    # Initiate the API call to start a multi part upload
    response = files_client.initiate_multi_part_upload(request)

except MindsphereError as err:
    # 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        file="<file>",
        entity_id="<entity_id>",
        filepath="<filepath>",
        description="<description>",
        timestamp="<timestamp>",
        type="<file_type>",
        part=<part_number>
    )

    # Initiate the API call for part upload
    response = files_client.create_multi_part_file(request)

except MindsphereError as err:
    # Exception Handling

Complete a Multi Part Upload

Complete a multi part upload using the PutFileRequest model.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>"
    )

    # Initiate the API call to complete a multi part upload
    response = files_client.complete_multi_part_upload(request)

except MindsphereError as err:
    # Exception Handling

Abort a Multi Part Upload

Abort a multi part upload using the PutFileRequest model.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>"
    )

    # Initiate the API call to abort a multi part upload
    response = files_client.abort_multi_part_upload(request)

except MindsphereError as err:
    # 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>",
        if_match="<if_match>"
    )

    # Initiate the API call to start an update of a multi part upload
    response = files_client.initiate_multi_part_upload(request)

except MindsphereError as err:
    # 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        file="<file>",
        entity_id="<entity_id>",
        filepath="<filepath>",
        description="<description>",
        timestamp="<timestamp>",
        type="<file_type>",
        part=<part_number>,
        if_match="<if_match>
    )

    # Initiate the API call to update an uploaded part
    response = files_client.update_multi_part_file(request)

except MindsphereError as err:
    # 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>",
        if_match="<if_match>"
    )

    # Initiate the API call to complete the update of a multi part upload
    response = files_client.complete_multi_part_upload(request)

except MindsphereError as err:
    # 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create the FileServiceClient object as shown above

try:
    # Create the request object
    request = PutFileRequest(
        entity_id="<entity_id>",
        filepath="<filepath>",
        if_match="<if_match>"
    )

    # Initiate the API call to abort the update of a multi part upload
    response = files_client.abort_multi_part_upload(request)

except MindsphereError as err:
    # Exception Handling

Any questions left?

Ask the community


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