Upload and Download Files

The files can be large, so the client streams them in both directions: it reads an upload from a file object in chunks, and gives a download as a Download, whose content you read in chunks or copy to a file.

Attachments of Submodel Elements

A File element refers to a file, which the server stores as its attachment.

import io
import tempfile

import requests

import aas_core3_2.types as aas_types

from aas_client3_2 import (
    aas_repository as aas_client_aas_repository,
    http as aas_client_http,
    requests_transport as aas_client_requests_transport,
    submodel_repository as aas_client_submodel_repository,
)

transport = aas_client_requests_transport.RequestsTransport(
    requests.Session()
)
submodel_repository_server = aas_client_http.Server(
    transport=transport, base_url="http://localhost:5005"
)
aas_repository_server = aas_client_http.Server(
    transport=transport, base_url="http://localhost:5004"
)

submodel = aas_types.Submodel(
    id="https://example.com/ids/sm/getting-started/files",
    id_short="Documents",
    submodel_elements=[
        aas_types.File(
            id_short="Manual", content_type="application/pdf"
        ),
    ],
)
aas_client_submodel_repository.post_submodel(
    submodel_repository_server, submodel
)

Upload the content, either as bytes or from a binary file object:

content = b"%PDF-1.4 ... an example manual ..."

aas_client_submodel_repository.put_file_by_path(
    submodel_repository_server,
    submodel.id,
    "Manual",
    io.BytesIO(content),
    file_name="manual.pdf",
    content_type="application/pdf",
)

Download it. The download is valid only within its with block, since the connection is released at its end:

with aas_client_submodel_repository.get_file_by_path(
    submodel_repository_server, submodel.id, "Manual"
) as download:
    print(download.file_name, download.content_type)

    with tempfile.TemporaryFile() as fid:
        download.write_to(fid)

        fid.seek(0)
        assert fid.read() == content

Read the chunks yourself with iter_chunks() instead, for example, to compute a checksum on the fly.

Remove the attachment:

aas_client_submodel_repository.delete_file_by_path(
    submodel_repository_server, submodel.id, "Manual"
)

Thumbnails of Shells

The thumbnail of a shell works the same way, in the AAS Repository:

shell = aas_types.AssetAdministrationShell(
    id="https://example.com/ids/aas/getting-started/files",
    asset_information=aas_types.AssetInformation(
        asset_kind=aas_types.AssetKind.INSTANCE,
        global_asset_id="https://example.com/ids/asset/getting-started/files",
    ),
)
aas_client_aas_repository.post_asset_administration_shell(
    aas_repository_server, shell
)

thumbnail = b"\x89PNG\r\n\x1a\n ... an example image ..."
aas_client_aas_repository.put_thumbnail(
    aas_repository_server,
    shell.id,
    thumbnail,
    file_name="thumbnail.png",
    content_type="image/png",
)

with aas_client_aas_repository.get_thumbnail(
    aas_repository_server, shell.id
) as download:
    content = b""
    for chunk in download.iter_chunks():
        content += chunk

assert content == thumbnail

aas_client_aas_repository.delete_thumbnail(
    aas_repository_server, shell.id
)

AASX packages are uploaded and downloaded the same way, see aasx_file_server.

Clean Up

Delete what we created in this chapter, so that you can run the code of this chapter again. The identifiers are unique, so the server would refuse to create the submodel and the shell a second time:

aas_client_submodel_repository.delete_submodel_by_id(
    submodel_repository_server, submodel.id
)
aas_client_aas_repository.delete_asset_administration_shell_by_id(
    aas_repository_server, shell.id
)