Skip to content

Managing file versions

QDL is capable of storing multiple versions of the same file. To achieve this, the concept of local path versus QDL path needs to be fully understood, and will be addressed first. Secondly, the code snippets are given to manage multiple file versions in QDL.

Local path versus QDL path

In the quickstart, managing the file structure within a dataset is explained when adding files or directories. This is sufficient information to be able to upload the files once and immediately after adding them, but inadequate if different workflows are desired.

Local paths are not saved in the dataset metadata, only the QDL path. Thus, when a file is added to a dataset, but not uploaded, it needs to be re-added, otherwise the upload will fail. The code snippet below shows this failure.

from qdl import Dataset

scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
file_path = "random_file.txt"

with open(file=file_path, mode="w", encoding="utf-8") as file:
    file.write("hello world!")

# only add file metadata to the dataset
dataset = Dataset(scope_uid=scope, name=uid)
dataset.add_file(local_path=file_path)
dataset.update()

# upload will fail as a local path is not defined
dataset = Dataset.get(scope_uid=scope, uid=uid)
dataset.upload()

Upload multiple versions of the same file

To create multiple versions of the same file, the QDL path needs to be identical in each case. The local path however, can differ, as each user could have a different path to the local file. This is demonstrated with the code snippets below.

The first snippet mimics the user who creates the dataset and adds the initial version of the file. The second snippet could be another user who retrieves the dataset from QDL, and adds a new file version. To achieve this, the overwrite parameter is set to true when calling the upload method.

User 1

from qdl import Dataset

scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"

local_path_user_1 = "random_file.txt"
qdl_path = "measurement.txt"

with open(file=local_path_user_1, mode="w", encoding="utf-8") as file:
    file.write("hello world!")

dataset = Dataset(scope_uid=scope, name=uid)
dataset.add_file(local_path=local_path_user_1, qdl_path=qdl_path)
dataset.upload()

User 2

from qdl import Dataset

scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"

local_path_user_2 = "another_random_file.txt"
qdl_path = "measurement.txt"

with open(file=local_path_user_2, mode="w", encoding="utf-8") as file:
    file.write("hello world!")

dataset = Dataset.get(scope_uid=scope, uid=uid)
dataset.add_file(local_path=local_path_user_2, qdl_path=qdl_path)
dataset.upload(overwrite=True)

Download a specific file version

Each version of a file is saved in the metadata of the dataset. By default, the latest version is selected when downloading a file. A specific version can be selected as well, demonstrated in the code snippet below.

from qdl import Dataset

scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
qdl_path = "<QDL_PATH>"

dataset = Dataset.get(scope_uid=scope, uid=uid)

# select a specific version of the file
print(dataset.get_file_versions(qdl_path=qdl_path))

version_id = "<VERSION_ID>"

# download the latest version
dataset.download_file(qdl_path=qdl_path)

# download a specific version
dataset.download_file(qdl_path=qdl_path, version_id=version_id)