Build an image gallery from a bucket
Store images in an Object Storage bucket and serve them as a gallery page from a Function, through an Application and a Workload.
In this tutorial, you will build an image gallery whose images live in an Object Storage bucket. You will upload two images to a bucket, write a Function that lists the bucket and serves each image, and publish the Function through an Application and a Workload.
Every resource the tutorial creates links to one created before it:
- The bucket holds the images. Its Workloads Access is read only, so Functions read it and do not change it.
- The Function reads the bucket with the
azion:storagemodule, and builds the gallery page. - The Application has Functions turned on, a function instance that attaches the Function, and a request rule that runs it on every path.
- The Workload holds the domain that receives requests, and its deployment sets the Application that answers them.
The resources are the same in Azion Console, the Azion CLI, and the API. Select the interface you use, and every stage shows its steps.
Prerequisites
Console
- An Azion account. To create one, refer to Create an account.
- Access to Azion Console. To sign in, refer to Access Azion Console.
curl, to request the gallery.
1. Create the images
Create a directory for the tutorial and go to it. Then write two SVG images, an orange square and a blue circle:
The directory holds orange.svg and blue.svg.
2. Create the bucket and upload the images
Choose a name for the bucket. A bucket name is unique across all Azion accounts and takes 6 to 63 characters: lowercase letters, numbers, and hyphens, starting and ending with a letter or a number. Wherever a step shows <your-bucket-name>, use your name.
Console
To create the bucket:
- Access Azion Console > Object Storage.
- Select + Bucket.
- In Name, enter the name of your bucket.
- Keep Read Only in Workloads Access.
- Select Save.
The bucket appears in the list of buckets.
To upload the images:
- In the list of buckets, select the name of your bucket.
- Drag
orange.svgandblue.svgonto the area that reads Drag files here to add them to your bucket.
The Console shows the message Upload Successful, and the bucket lists blue.svg with 121 Bytes and orange.svg with 118 Bytes.
The bucket holds the two images.
3. Write the Function
Create an index.js file with this code. In the third line, replace <your-bucket-name> with the name of your bucket:
The Function answers two routes, both read from the bucket:
GET /lists the bucket withstorage.list(). Each entry carries the key of an object with a leading slash, such as/orange.svg, and the Function writes one<figure>per SVG.GET /images/<key>reads one object withstorage.get(key)and returns its content. A key with no object answers404.
For every method of the module, refer to Object Storage API.
4. Create the Function
Console
To create the Function in Azion Console:
- Access Azion Console > Functions.
- Select + Function.
- In Name, enter
gallery. - Keep Application selected in Execution Environment.
- Select the Code tab.
- Replace the sample code in the editor with the content of
index.js. - Select Save.
The Console shows the message Your function has been created, and gallery appears in the list of Functions.
The account holds the Function. It runs only after an Application attaches it.
5. Create the Application
The Application needs three parts: the Application itself with Functions turned on, a function instance that attaches the Function, and a rule that runs the instance.
Console
To create the Application:
- Access Azion Console > Applications.
- Select + Application.
- In Name, enter
gallery. - In Modules, turn on Functions.
- Select Create.
The Console opens the new Application on its Main Settings tab.
To attach the Function:
- Select the Functions Instances tab.
- Select + Function.
- In Name, enter
gallery. - In Function, select
gallery. - Select Save.
To run the Function on every request:
- Select the Rules Engine tab.
- Select + Rule.
- In Name, enter
Run gallery. - Keep Request Phase selected.
- In Criteria, keep
${uri}and starts with, and enter/as the argument. - In Behaviors, replace Deliver with Run Function.
- In the function instance field that Run Function adds, select the
galleryinstance. - Select Save.
The rule appears in the request phase of the Rules Engine tab.
The Application runs the Function on every request it receives.
6. Create the Workload
The Workload holds the domain, and its deployment sets the Application that answers its requests.
Console
To create the Workload and its deployment:
- Access Azion Console > Workloads.
- Select + Workload.
- In Name, enter
gallery. - In Deployment Settings, select
galleryin Application. - Select Save.
The Console shows the message Your Workload has been created. The list of Workloads shows the domain in the Workload Domain column, in the format xxxxxxxxxxx.map.azionedge.net.
Your domain has the same format as the examples, with different letters.
7. Open the gallery
A new Workload can take about ten minutes to propagate across the Azion network. Until then, the domain answers 404 with an HTML error page, and it can alternate between 404 and the Function’s response for a few minutes.
If you used the Console, set DOMAIN to your domain first, with DOMAIN=<your-domain>. Request the gallery page:
Repeat the request every minute until the page returns three times in a row. It lists one figure per image:
Request one image and print its status and type:
Open https://<your-domain>/ in a browser to see the two images, each with its file name.
8. (Optional) Delete the resources
The gallery keeps running on your domain after the tutorial. If you want to remove the resources, delete them in the reverse order of their creation. Deleting the Workload also deletes its deployment, and deleting the Application also deletes its instance and its rule.
A bucket is deleted only when it is empty and no object was removed from it in the last 24 hours. To remove the bucket too, delete the objects first, and the bucket one day later.
Console
To delete the Workload:
- Access Azion Console > Workloads.
- In the
galleryrow, open the actions menu and select Delete. - In the Delete Workload dialog, enter
gallery. - Select Delete.
Repeat the steps in Applications for the gallery Application, then in Functions for the gallery Function.
To delete the images:
- Access Azion Console > Object Storage.
- In the list of buckets, select the name of your bucket.
- In the
blue.svgrow, open the actions menu and select Delete. - In the Delete File dialog, enter
blue.svg. - Select Delete.
Repeat the steps for orange.svg. The bucket is empty.
One day later, delete the bucket:
- Access Azion Console > Object Storage.
- In the list of buckets, open the actions menu of your bucket and select Delete.
- In the Delete bucket dialog, enter the name of your bucket.
- Confirm the deletion.
Before the 24 hours pass, the Console answers with this error:
The account no longer holds the Function, the Application, the Workload, or the images. The removal of the Workload also propagates, so the domain can keep answering for a few minutes.