After reading this article, you’ll know the following:
Store file step and its functionality
How to use this step in your application
What is Store file step?
Store file action step in Betty Blocks allows you to store files using a provided URL or configuration variable. This functionality is particularly useful when you need to save files from external sources directly into your application’s storage.
The step is typically used with an HTTP(s) step that fetches the file from an external source (e.g. API). Upon successful upload, the step returns a reference to the Store file step in the database. This reference can then be used in further steps, such as Create or Update records within your application.
Imagine this case: you are building an application that manages articles for an online magazine. Each article may have an associated image. By using the HTTP(s) step, you can fetch images from an external data API, store them within your application with the Store file step, and then associate them with specific articles with Update record step.
Options
From the Store file, Options tab is open by default. Here you can manage the following options:
Model: The model containing the property where the reference to the stored file will be saved
Property: The specific property of the chosen model where the file reference will be stored. This property must be designated to handle file references
URL: The URL of the externally hosted file to be stored. Only HTTP and HTTPS URLs are supported
Headers: Key-value pairs of headers to be included in the request to fetch the file from the URL. Usually, it is an authentication key provided by an API source.
As: The name of the variable that will store the reference to the stored file. This variable can be used further in the flow to create or update records with the file reference.
Variables tab is used to add more variables to the step, which can be used for dynamic value mapping. This is particularly useful when the values that need to be assigned depend on other variables or calculations within your application.
Read more about working with variables here.
Using Store file step
Let’s take a basic use case for this example. Assume you want to retrieve an image from the Pexels API using a photo ID (Number), and store it in the new article record in your application.
Create a new action in your application and add the HTTP(s) step to the action flow and configure it according to Get a photo API info:
Choose GET method to retrieve the file(s)
Headers will contain the authentication name and value (password)
Protocol is defined as HTTPS
URL contains the initial storage along with image ID:
https://api.pexels.com/v1/photos/2014422
Finish the configuration with a schema model as a temporary storage
As: Set a variable name for the response data and save it as an object (e.g. pexels_article_image)
2. Add the Store file step to your action flow and configure it:
Model: Here you select a model you want to store the image in and select the property accordingly (Article model with article_image - a file or image type)
URL repeats the initial one from the HTTP(s) step
Headers: If additional headers are needed for the store request, add them here
As: Set a variable name for storing (e.g. uploaded_article_image)
3. After saving the Store file step, add Create record step:
Select a model to create a new record in (Article)
Add properties:
Article image with uploaded_article_image (from Store file) as a variable
Number with selected_articles.id variable (image ID saved from HTTP(s) step)
Define the name for the result variable and save your configuration
This flow will fetch the image from the Pexels API using the specified photo ID, store it within your database, and then create a new article record in the Article model with the image file reference stored in the image (or file) property.
Mind that the Store file action step is not used for uploading images to the front end of your application. For such things, we use the Image upload form component. Find out how it’s done in Creating a document upload feature article.