File download (Experimental)

In this tutorial we will describe a way to retrieve files produced by Qiskit Functions.

This function provides a way to download files produced by functions during execution. All you need is to call QiskitServerless.file_download function and pass a file name and the Qiskit Function to start downloading the file. Or you can list all available files to you by calling QiskitServerless.files.

Limitations:

  • files should be saved in /data directory during your function execution to be visible by .files() method call.

  • only /data directory is supported, /data/other_folder will not be visible.

  • 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.

  • Qiskit Functions created by you and Qiskit Functions created by others don’t share directories.

⚠ 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.

[2]:
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
[2]:
<gateway-client>

Let’s create a Qiskit Function to write tar file into /data folder

[3]:
function = QiskitFunction(
    title="file-producer", entrypoint="produce_files.py", working_dir="./source_files/"
)

serverless.upload(function)
[3]:
QiskitFunction(file-producer)
[4]:
my_function = serverless.get("file-producer")
my_function
[4]:
QiskitFunction(file-producer)
[5]:
job = my_function.run()
job
[5]:
<Job | 4868d039-3342-4f3e-b843-c0d15ad52fe6>
[6]:
job.result()
[6]:
{'Message': 'my_file.txt archived into my_file.tar'}

Now we can look at files available using files method

[8]:
available_files = serverless.files(my_function)
available_files
[8]:
['my_file.tar']

And download them if needed using download method

[10]:
serverless.file_download(available_files[0], my_function)
100%|██████████| 200/200 [00:00<00:00, 309kiB/s]
[10]:
'downloaded_8d3f92ba_my_file.tar'