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.
Further implementation of the IOT File Service 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-python-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¶
# 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 iotfileservices import FileServiceClient
# Import all required models from iotfile.models
from iotfileservices 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.
# 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.
# 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.
# 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¶
# 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¶
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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