|
| 1 | +|pipe| Adding Blob storage bindings |
| 2 | +==================================== |
| 3 | + |
| 4 | +You should now have a working function that collects data from the StackExchange API. |
| 5 | + |
| 6 | +In this section, we will: |
| 7 | + |
| 8 | +- Complete the function to store the data in AzureBlob storage |
| 9 | +- Create a second function that identifies the addition of a file to Azure Blob storage and triggers a second function |
| 10 | +- Create a database to store our cleaned data and modify the function to store the database |
| 11 | + |
| 12 | +.. tip:: The repository containing all the scripts and solutions to this tutorial can be found at `<https://github.com/trallard/pycon2020-azure-functions>`_. |
| 13 | + |
| 14 | + 👉🏼 The code for this section is located in `<https://github.com/trallard/pycon2020-azure-functions/tree/master/solutions/02-timer-function-Blob-binding>`_ |
| 15 | + |
| 16 | + |
| 17 | +|light| Triggers and bindings |
| 18 | +-------------------------------- |
| 19 | + |
| 20 | +- **Triggers**: these cause a function to run. They can be an HTTP request, a queue message or an event grid. Each function **must** have one trigger. |
| 21 | + |
| 22 | +- **Binding**: is a connection between a function and another resource or function. They can be *input bindings, output bindings* or both. These are optional, and a function can have one or more bindings. |
| 23 | + |
| 24 | +1. Create Azure Blob Storage |
| 25 | +****************************************** |
| 26 | + |
| 27 | +We already created a Storage Account in the :ref:`deployfn` section. The next step is to create a Blob Storage container so we can start saving the data collected through your function. |
| 28 | + |
| 29 | +#. Head over to |azureportal| and click on **Storage accounts** on the left sidebar and then on your function storage account. |
| 30 | + |
| 31 | + .. image:: _static/images/snaps/storagedashboard.png |
| 32 | + :align: center |
| 33 | + :alt: Storager dashboard |
| 34 | + |
| 35 | +#. Click on either of the **Containers** section (see image). |
| 36 | + |
| 37 | + .. image:: _static/images/snaps/containers.png |
| 38 | + :align: center |
| 39 | + :alt: Containers screenshot |
| 40 | + |
| 41 | +#. Click on **+ Container** at the top of the bar and provide a name for your Blob container. |
| 42 | + |
| 43 | +#. Without leaving your dashboard, click on **Access keys** on the sidebar menu and copy the Connection string. |
| 44 | + |
| 45 | + .. image:: _static/images/snaps/access.png |
| 46 | + :align: center |
| 47 | + :alt: Storage dashboard access |
| 48 | + |
| 49 | +.. _attachblob: |
| 50 | + |
| 51 | +2. Attach Blob binding |
| 52 | +****************************************** |
| 53 | + |
| 54 | +Now that you created the Blob container, you need to add the binding to your function. |
| 55 | + |
| 56 | +1. Back in VS Code click on the **Azure** extension on the sidebar and then right-click on your function name > **Add binding**. |
| 57 | +2. Since we want to store the outputs in the container, we need to select the **OUT** direction followed by **Azure Blob Storage**. |
| 58 | +3. Assign a name for the binding a path for the blob: |
| 59 | + |
| 60 | + .. code-block:: |
| 61 | +
|
| 62 | + functionblob/{DateTime}.csv |
| 63 | +
|
| 64 | + Notice that I am using the name of the container I created before and the binding expression ``DateTime`` which resolves to ``DateTime.UtcNow``. The following blob path in a ``function.json`` file creates a blob with a name like ``2020-04-16T17-59-55Z.txt``. |
| 65 | + |
| 66 | +4. Select **AzureWebJobsStorage** for your local settings. |
| 67 | + |
| 68 | +Once completed, your ``function.json`` file should look like this: |
| 69 | + |
| 70 | + .. literalinclude:: ../solutions/02-timer-function-Blob-binding/timer-function/function.json |
| 71 | + :language: json |
| 72 | + :caption: function.json |
| 73 | + |
| 74 | + |
| 75 | +5. Add the **Storage access key** that you copied before to your ``local.settings.json``. If you added your storage account through the Azure functions extensions, this should already be populated. |
| 76 | + |
| 77 | + .. code-block:: |
| 78 | + :caption: local.settings.json |
| 79 | + :emphasize-lines: 4 |
| 80 | +
|
| 81 | + { |
| 82 | + "IsEncrypted": false, |
| 83 | + "Values": { |
| 84 | + "AzureWebJobsStorage": <Your key>, |
| 85 | + "FUNCTIONS_WORKER_RUNTIME": "python", |
| 86 | + "AzureWebJobs.timer-function.Disabled": "false" |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | +.. _blobfunction: |
| 91 | + |
| 92 | +3. Update your function |
| 93 | +***************************** |
| 94 | + |
| 95 | +We now need to update the function so that: |
| 96 | + |
| 97 | +- Save the collected API items in a CSV file |
| 98 | +- Store the file in the Blob container |
| 99 | + |
| 100 | +Updating the `main_function.py` file: |
| 101 | + |
| 102 | + .. literalinclude:: ../solutions/02-timer-function-Blob-binding/timer-function/main_function.py |
| 103 | + :language: python |
| 104 | + :caption: main_function.py |
| 105 | + :emphasize-lines: 7, 33-53, 61-63, 87-93 |
| 106 | + |
| 107 | +Notice these lines in the above code: |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | +
|
| 112 | + def main( |
| 113 | + mytimer: func.TimerRequest, |
| 114 | + outputBlob: func.Out[bytes], |
| 115 | + context: func.Context |
| 116 | + ) -> None: |
| 117 | +
|
| 118 | +The ``outputBlob: func.Out[bytes]`` specifies the binding we just created and ``context: func.Context`` allows the function to get the context from the `host.json` file. |
| 119 | + |
| 120 | +And also the script to access the StackExchange API: |
| 121 | + |
| 122 | + .. literalinclude:: ../solutions/02-timer-function-Blob-binding/timer-function/utils/stack.py |
| 123 | + :language: python |
| 124 | + :caption: utils/stack.py |
| 125 | + |
| 126 | +If you want, you can follow the steps in section :ref:`localdebug` to run and debug your function locally. |
| 127 | + |
| 128 | +Otherwise, you can deploy and execute your function as we did in section :ref:`deployandrun` (except for the variables setting section as your storage details should be there already). |
| 129 | + |
| 130 | + |
| 131 | +.. tip:: When deploying your function, you can click on the pop-up window **output window** link to track the deployment status/progress. |
| 132 | + |
| 133 | + .. image:: _static/images/snaps/explore.png |
| 134 | + :align: center |
| 135 | + :alt: Explore deploy |
| 136 | + |
| 137 | +After running your function you can head over to **Storage accounts > <your account> > Containers** and click on your function Blob container. |
| 138 | + |
| 139 | +If all runs smoothly, you should be able to see the created file. |
| 140 | + |
| 141 | +.. image:: _static/images/snaps/blob_file.png |
| 142 | + :align: center |
| 143 | + :alt: Blob file |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +|floppy| Additional resources and docs |
| 148 | +--------------------------------------- |
| 149 | + |
| 150 | +- `ARM template for Blob Storage container <https://github.com/trallard/pycon2020-azure-functions/tree/master/storage-blob-container>`_ |
| 151 | +- `Azure functions triggers and bindings <https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings?WT.mc_id=pycon_tutorial-github-taallard>`_ |
| 152 | +- `Azure functions supported bindings <https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#supported-bindings?WT.mc_id=pycon_tutorial-github-taallard>`_ |
| 153 | +- `Azure Storage documentation <http://azure.microsoft.com/documentation/articles/storage-create-storage-account?WT.mc_id=pycon_tutorial-github-taallard>`_ |
| 154 | +- `Binding expressions docs <https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns?WT.mc_id=pycon_tutorial-github-taallard>`_ |
| 155 | +- `Azure function reference output <https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#outputs?WT.mc_id=pycon_tutorial-github-taallard>`_ |
| 156 | +- `Python type hints cheatsheet <https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html>`_ |
0 commit comments