Shared data directory (Experimental)¶
In this tutorial we will describe a shared directory data
. Qiskit Functions can produce and consume files in the data
directory. The files in the data
directory are shared among Qiskit Functions.
QiskitServerless
has file_download
, file_upload
, file_delete
and files
functions that provide file upload, file download, file delete and list files function.
Limitations:
only
tar
andh5
files are supportedtar
orh5
file should be saved in/data
director during your function execution to be visible by.files()
method callonly
/data
directory is supported,/data/other_folder
will not be visiblethe working directory of these functions are
/data
as a provider you have access to
/function-data
, it works in a similar way as the/data
folder with the distinction that users don’t have access to it. Only the providers of the specific functions can see files under that path.
⚠ This interface is experimental, therefore it is subjected to breaking changes.
⚠ This provider is set up with default credentials to a test cluster intended to run on your machine. For information on setting up infrastructure on your local machine, check out the guide on local infrastructure setup.
[1]:
import os
from qiskit_serverless import ServerlessClient, QiskitFunction
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost"),
# If you are using the kubernetes approach the URL must be http://localhost
)
serverless
[1]:
<gateway-client>
Upload a file in the data
directory
[2]:
import tarfile
filename= "uploaded_file.tar"
file= tarfile.open(filename,"w")
file.add("manage_data_directory.ipynb")
file.close()
[3]:
serverless.file_upload(filename)
[3]:
'{"message":"/usr/src/app/media/mockuser/uploaded_file.tar"}'
Produce a file in the data
directory
[4]:
function = QiskitFunction(
title="file-producer", entrypoint="produce_files.py", working_dir="./source_files/"
)
serverless.upload(function)
job = serverless.run("file-producer")
job.result()
[4]:
{'Message': 'my_file.txt archived into my_file.tar'}
List files in the data
directory
[5]:
serverless.files()
[5]:
['uploaded_file.tar', 'my_file.tar']
Consume the file procuded by file-producer
Qiskit Function
[6]:
function = QiskitFunction(
title="file-consumer", entrypoint="consume_files.py", working_dir="./source_files/"
)
serverless.upload(function)
job = serverless.run("file-consumer")
job.result()
[6]:
{'Message': "b'Hello!'"}
List files
[7]:
serverless.files()
[7]:
['uploaded_file.tar', 'my_file.tar']
Delete files
[8]:
serverless.file_delete("uploaded_file.tar")
[8]:
'Requested file was deleted.'
[9]:
serverless.files()
[9]:
['my_file.tar']