{ "cells": [ { "cell_type": "markdown", "id": "412699e8-ba4b-4051-977e-30be868e1b0a", "metadata": {}, "source": [ "# Shared data directory (Experimental)\n", "\n", "In this tutorial we will describe a shared directory `data`. Qiskit patterns can produce and consume files in the `data` directory. The files in the `data` directory are shared among Qiskit patterns.\n", "\n", "`QiskitServerless` has `file_download`, `file_upload`, `file_delete` and `files` functions that provide file upload, file download, file delete and list files function.\n", "\n", "Limitations:\n", "\n", "- only `tar` and `h5` files are supported\n", "- `tar` or `h5`file should be saved in `/data` director during your pattern execution to be visible by `.files()` method call\n", "- only `/data` directory is supported, `/data/other_folder` will not be visible\n", "- The working directory of these functions are `/data`\n", "\n", "> ⚠ This interface is experimental, therefore it is subjected to breaking changes.\n", "\n", "> ⚠ 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](https://qiskit.github.io/qiskit-serverless/deployment/local.html)." ] }, { "cell_type": "code", "execution_count": 1, "id": "429d8e73-aa75-47ca-9dcd-ed3b33a2cdf8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "from qiskit_serverless import ServerlessClient, QiskitFunction\n", "\n", "serverless = ServerlessClient(\n", " token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n", " host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", ")\n", "serverless" ] }, { "cell_type": "markdown", "id": "bc286ee3-2b3b-47ca-a60e-b4db8a57ee06", "metadata": {}, "source": [ "Upload a file in the `data` directory" ] }, { "cell_type": "code", "execution_count": 2, "id": "4f35187e-952e-4f2b-8871-1c971e28a739", "metadata": {}, "outputs": [], "source": [ "import tarfile\n", "\n", "filename= \"uploaded_file.tar\"\n", "file= tarfile.open(filename,\"w\")\n", "file.add(\"manage_data_directory.ipynb\")\n", "file.close() " ] }, { "cell_type": "code", "execution_count": 3, "id": "71d0cd3f-95fd-42ed-ab77-c363433e4172", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"message\":\"/usr/src/app/media/mockuser/uploaded_file.tar\"}'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "serverless.file_upload(filename)" ] }, { "cell_type": "markdown", "id": "3266eb64-a211-4bdc-bae6-700ab58b5a6c", "metadata": {}, "source": [ "Produce a file in the `data`directory" ] }, { "cell_type": "code", "execution_count": 4, "id": "d0009872-7da2-45c9-8bd6-d42be9da67a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Message': 'my_file.txt archived into my_file.tar'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function = QiskitFunction(\n", " title=\"file-producer\", entrypoint=\"produce_files.py\", working_dir=\"./source_files/\"\n", ")\n", "\n", "serverless.upload(function)\n", "job = serverless.run(\"file-producer\")\n", "job.result()" ] }, { "cell_type": "markdown", "id": "ddc854ab-b727-4a81-88a1-d6bc89cce0ba", "metadata": {}, "source": [ "List files in the `data` directory" ] }, { "cell_type": "code", "execution_count": 5, "id": "81ca879c-352f-41ae-a97a-5eb08b4b2c19", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['uploaded_file.tar', 'my_file.tar']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "serverless.files()" ] }, { "cell_type": "markdown", "id": "e47905b1-8fc6-4d22-8916-4724fa854ae2", "metadata": {}, "source": [ "Consume the file procuded by `file-producer` Qiskit Pattern" ] }, { "cell_type": "code", "execution_count": 6, "id": "06c345d7-b7dd-49b4-8c67-e8158acdcf33", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Message': \"b'Hello!'\"}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function = QiskitFunction(\n", " title=\"file-consumer\", entrypoint=\"consume_files.py\", working_dir=\"./source_files/\"\n", ")\n", "\n", "serverless.upload(function)\n", "job = serverless.run(\"file-consumer\")\n", "job.result()" ] }, { "cell_type": "markdown", "id": "3946e3d8-76f2-470c-be23-08e917606ef4", "metadata": {}, "source": [ "List files" ] }, { "cell_type": "code", "execution_count": 8, "id": "da35c2cd-8b26-4d36-83f3-02fda396d56d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['uploaded_file.tar', 'my_file.tar']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "serverless.files()" ] }, { "cell_type": "markdown", "id": "7099a7fc-72ef-4c55-8ee2-b02a0a7d8ec4", "metadata": {}, "source": [ "Delete files" ] }, { "cell_type": "code", "execution_count": 9, "id": "c826ed42-9ca6-44f4-83c3-20b2f28c09fc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Requested file was deleted.'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "serverless.file_delete(\"uploaded_file.tar\")" ] }, { "cell_type": "code", "execution_count": null, "id": "cd6a3009-80bf-4c70-a731-63c5d58bfa6a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }