---
name: azion-build-an-image-gallery-from-a-bucket
description: >-
  Store images in an Object Storage bucket and serve them as a gallery page from a Function, through an Application and a Workload.
---

# Build an image gallery from a bucket

In this tutorial, you will build an image gallery whose images live in an [Object Storage](/en/documentation/platform/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:

1. The bucket holds the images. Its **Workloads Access** is read only, so Functions read it and do not change it.
2. The [Function](/en/documentation/platform/functions/) reads the bucket with the `azion:storage` module, and builds the gallery page.
3. The [Application](/en/documentation/platform/applications/) has Functions turned on, a function instance that attaches the Function, and a request rule that runs it on every path.
4. The [Workload](/en/documentation/platform/workloads/) 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](/en/documentation/fundamentals/creating-account/).
- Access to Azion Console. To sign in, refer to [Access Azion Console](/en/documentation/guides/platform/account-and-billing/how-to-access-azion-console/).
- `curl`, to request the gallery.

### CLI

- An Azion account. To create one, refer to [Create an account](/en/documentation/fundamentals/creating-account/).
- The Azion CLI. The commands on this page match Azion CLI 4.26.1. To install it, refer to [Azion CLI](/en/documentation/devtools/cli/).
- The Azion CLI authorized with a personal token, with `azion -t <your-personal-token>`. The next command can ask whether you agree to share anonymous usage data. To create a token, refer to [Personal tokens](/en/documentation/fundamentals/personal-tokens/).
- `curl`, to request the gallery.
- `jq`, to read the domain from the CLI output.
- One terminal session for every command of the tutorial, because later commands read the shell variables that earlier ones set.

### API

- An Azion account. To create one, refer to [Create an account](/en/documentation/fundamentals/creating-account/).
- A personal token. To create one, refer to [Personal tokens](/en/documentation/fundamentals/personal-tokens/).
- `curl`, to call the API and request the gallery.
- `jq`, to read IDs from the API responses.
- One terminal session for every command of the tutorial, because later commands read the shell variables that earlier ones set.

---

## 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:

```bash
echo '<svg xmlns="http://www.w3.org/2000/svg" width="160" height="160"><rect width="160" height="160" fill="orange"/></svg>' > orange.svg
echo '<svg xmlns="http://www.w3.org/2000/svg" width="160" height="160"><circle cx="80" cy="80" r="70" fill="steelblue"/></svg>' > blue.svg
```

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:

1. Access [Azion Console](https://console.azion.com/) > **Object Storage**.
2. Select **+ Bucket**.
3. In **Name**, enter the name of your bucket.
4. Keep **Read Only** in **Workloads Access**.
5. Select **Save**.

The bucket appears in the list of buckets.

To upload the images:

1. In the list of buckets, select the name of your bucket.
2. Drag `orange.svg` and `blue.svg` onto 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.

### CLI

Store the name of your bucket in a shell variable:

```bash
BUCKET=<your-bucket-name>
```

Create the bucket with read-only access for Workloads:

```bash
azion create storage bucket --name $BUCKET --workloads-access read_only
```

```text
Bucket created successfully
```

Upload the two images. `--source` takes a path relative to the current directory:

```bash
azion create storage object --bucket-name $BUCKET --object-key orange.svg --source orange.svg
```

```text
Object created successfully
```

```bash
azion create storage object --bucket-name $BUCKET --object-key blue.svg --source blue.svg
```

```text
Object created successfully
```

List the objects of the bucket:

```bash
azion list storage object --bucket-name $BUCKET
```

```text
blue.svg    2026-11-13 20:03:31.088 +0000 UTC
orange.svg  2026-11-13 20:03:28.553 +0000 UTC
```

### API

Store your personal token and the name of your bucket in shell variables. Replace `<your-personal-token>` with your token:

```bash
export AZION_API_TOKEN="<your-personal-token>"
BUCKET=<your-bucket-name>
```

Create the bucket with read-only access for Workloads:

```bash
curl --silent https://api.azion.com/v4/workspace/storage/buckets \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data "{\"name\": \"$BUCKET\", \"workloads_access\": \"read_only\"}"
```

```json
{"state":"executed","data":{"name":"gallery-api-xcjstk","workloads_access":"read_only",...}}
```

Upload each image as the body of a `POST` request to its object key. `Content-Type` sets the type of the object:

```bash
for image in orange.svg blue.svg; do
  curl --silent https://api.azion.com/v4/workspace/storage/buckets/$BUCKET/objects/$image \
    --header "Authorization: Token $AZION_API_TOKEN" \
    --header 'Content-Type: image/svg+xml' \
    --data-binary @$image
  echo
done
```

```text
{"state":"executed","data":{"object_key":"orange.svg"}}
{"state":"executed","data":{"object_key":"blue.svg"}}
```

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:

```javascript title="index.js"
import Storage from 'azion:storage'

const BUCKET = '<your-bucket-name>'

export default {
  async fetch(request) {
    const storage = new Storage(BUCKET)
    const { pathname } = new URL(request.url)
    if (pathname === '/') {
      const { entries } = await storage.list()
      const figures = entries
        .filter((entry) => entry.key.endsWith('.svg'))
        .map((entry) => `<figure><img src="/images${entry.key}" alt="${entry.key.slice(1)}" width="160"><figcaption>${entry.key.slice(1)}</figcaption></figure>`)
        .join('')
      return new Response(`<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Gallery</title></head><body><h1>Gallery</h1>${figures}</body></html>`, {
        headers: { 'content-type': 'text/html; charset=utf-8' }
      })
    }
    if (pathname.startsWith('/images/')) {
      try {
        const object = await storage.get(pathname.slice('/images/'.length))
        return new Response(object.content, { headers: { 'content-type': 'image/svg+xml' } })
      } catch (error) {
        return new Response('Not found', { status: 404 })
      }
    }
    return new Response('Not found', { status: 404 })
  }
}
```

The Function answers two routes, both read from the bucket:

- `GET /` lists the bucket with `storage.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 with `storage.get(key)` and returns its content. A key with no object answers `404`.

For every method of the module, refer to [Object Storage API](/en/documentation/devtools/runtime/api-reference/storage/).

---

## 4. Create the Function

### Console

To create the Function in Azion Console:

1. Access [Azion Console](https://console.azion.com/) > **Functions**.
2. Select **+ Function**.
3. In **Name**, enter `gallery`.
4. Keep **Application** selected in **Execution Environment**.
5. Select the **Code** tab.
6. Replace the sample code in the editor with the content of `index.js`.
7. Select **Save**.

The Console shows the message **Your function has been created**, and `gallery` appears in the list of Functions.

### CLI

Create the Function from `index.js`:

```bash
azion create function --name gallery --code index.js --execution-environment application --active true
```

```text
Created function with ID 110033
```

Store the ID that your command printed. Replace `110033` with your ID:

```bash
FUNCTION_ID=110033
```

### API

Send a `POST` request to the functions endpoint. `jq` reads `index.js` into the `code` field:

```bash
jq -n --rawfile code index.js \
  '{name: "gallery", code: $code, runtime: "azion_js", execution_environment: "application"}' \
| curl --silent https://api.azion.com/v4/workspace/functions \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data @- \
| tee function.json
```

```json
{"state": "pending", "data": {"id": 110034, "name": "gallery", "active": true, "runtime": "azion_js", "execution_environment": "application", ...}}
```

```bash
FUNCTION_ID=$(jq .data.id function.json)
```

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:

1. Access [Azion Console](https://console.azion.com/) > **Applications**.
2. Select **+ Application**.
3. In **Name**, enter `gallery`.
4. In **Modules**, turn on **Functions**.
5. Select **Create**.

The Console opens the new Application on its **Main Settings** tab.

To attach the Function:

1. Select the **Functions Instances** tab.
2. Select **+ Function**.
3. In **Name**, enter `gallery`.
4. In **Function**, select `gallery`.
5. Select **Save**.

To run the Function on every request:

1. Select the **Rules Engine** tab.
2. Select **+ Rule**.
3. In **Name**, enter `Run gallery`.
4. Keep **Request Phase** selected.
5. In **Criteria**, keep `${uri}` and **starts with**, and enter `/` as the argument.
6. In **Behaviors**, replace **Deliver** with **Run Function**.
7. In the function instance field that **Run Function** adds, select the `gallery` instance.
8. Select **Save**.

The rule appears in the request phase of the **Rules Engine** tab.

### CLI

Create the Application with Functions turned on:

```bash
azion create application --name gallery --function true --active true
```

```text
Created Application with ID 1794609946
```

Store the ID. Replace `1794609946` with your ID:

```bash
APPLICATION_ID=1794609946
```

Attach the Function to the Application:

```bash
azion create function-instance --application-id $APPLICATION_ID --function-id $FUNCTION_ID --name gallery
```

```text
Created Function Instance with ID 64713
```

Store the ID. Replace `64713` with your ID:

```bash
INSTANCE_ID=64713
```

Write the rule that runs the instance on every path to a `rule.json` file:

```bash
cat > rule.json <<EOF
{
  "name": "Run gallery",
  "criteria": [[{ "variable": "\${uri}", "conditional": "if", "operator": "starts_with", "argument": "/" }]],
  "behaviors": [{ "type": "run_function", "attributes": { "value": $INSTANCE_ID } }]
}
EOF
```

Create the rule in the request phase:

```bash
azion create rules-engine --application-id $APPLICATION_ID --phase request --file rule.json
```

```text
Created Rules Engine with ID 1547629
```

### API

Create the Application with Functions turned on:

```bash
curl --silent https://api.azion.com/v4/workspace/applications \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{"name": "gallery", "modules": {"functions": {"enabled": true}}}' \
| tee application.json
```

```json
{"state": "pending", "data": {"id": 1794609947, "name": "gallery", "active": true, ...}}
```

```bash
APPLICATION_ID=$(jq .data.id application.json)
```

Attach the Function to the Application:

```bash
curl --silent https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/functions \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data "{\"name\": \"gallery\", \"function\": $FUNCTION_ID}" \
| tee instance.json
```

```json
{"state": "pending", "data": {"id": 64714, "name": "gallery", "function": 110034, "active": true, ...}}
```

```bash
INSTANCE_ID=$(jq .data.id instance.json)
```

Add the rule that runs the instance on every path:

```bash
curl --silent https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/request_rules \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data "{\"name\": \"Run gallery\", \"criteria\": [[{\"variable\": \"\${uri}\", \"conditional\": \"if\", \"operator\": \"starts_with\", \"argument\": \"/\"}]], \"behaviors\": [{\"type\": \"run_function\", \"attributes\": {\"value\": $INSTANCE_ID}}]}"
```

```json
{"state": "pending", "data": {"id": 1547630, "name": "Run gallery", "active": true, "order": 0, ...}}
```

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:

1. Access [Azion Console](https://console.azion.com/) > **Workloads**.
2. Select **+ Workload**.
3. In **Name**, enter `gallery`.
4. In **Deployment Settings**, select `gallery` in **Application**.
5. 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`.

### CLI

Create the Workload:

```bash
azion create workload --name gallery
```

```text
Created Workload with ID 1794600526
```

Store the ID. Replace `1794600526` with your ID:

```bash
WORKLOAD_ID=1794600526
```

Create the Workload's deployment, which sets the Application:

```bash
azion create workload-deployment --workload-id $WORKLOAD_ID --name gallery --strategy-type default --application-id $APPLICATION_ID
```

```text
Created Workload Deployment with ID 361370
```

Read the domain of the Workload:

```bash
DOMAIN=$(azion describe workload --workload-id $WORKLOAD_ID --format json | jq -r .workload_domain)
echo $DOMAIN
```

```text
rk5fkftzejq.map.azionedge.net
```

### API

Create the Workload:

```bash
curl --silent https://api.azion.com/v4/workspace/workloads \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{"name": "gallery"}' \
| tee workload.json
```

```json
{"state": "pending", "data": {"id": 1794600532, "name": "gallery", ..., "workload_domain": "zhxzrt9vp1c.map.azionedge.net", ...}}
```

```bash
WORKLOAD_ID=$(jq .data.id workload.json)
DOMAIN=$(jq -r .data.workload_domain workload.json)
```

Create the Workload's deployment, which sets the Application:

```bash
curl --silent https://api.azion.com/v4/workspace/workloads/$WORKLOAD_ID/deployments \
  --header "Authorization: Token $AZION_API_TOKEN" \
  --header 'Content-Type: application/json' \
  --data "{\"name\": \"gallery\", \"strategy\": {\"type\": \"default\", \"attributes\": {\"application\": $APPLICATION_ID}}}"
```

```json
{"state": "pending", "data": {"id": 361371, "name": "gallery", "active": true, "current": true, ...}}
```

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:

```bash
curl "https://$DOMAIN/"
```

Repeat the request every minute until the page returns three times in a row. It lists one figure per image:

```html
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Gallery</title></head><body><h1>Gallery</h1><figure><img src="/images/blue.svg" alt="blue.svg" width="160"><figcaption>blue.svg</figcaption></figure><figure><img src="/images/orange.svg" alt="orange.svg" width="160"><figcaption>orange.svg</figcaption></figure></body></html>
```

Request one image and print its status and type:

```bash
curl --silent --output /dev/null --write-out '%{http_code} %{content_type}\n' "https://$DOMAIN/images/orange.svg"
```

```text
200 image/svg+xml
```

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:

1. Access [Azion Console](https://console.azion.com/) > **Workloads**.
2. In the `gallery` row, open the actions menu and select **Delete**.
3. In the **Delete Workload** dialog, enter `gallery`.
4. Select **Delete**.

Repeat the steps in **Applications** for the `gallery` Application, then in **Functions** for the `gallery` Function.

To delete the images:

1. Access [Azion Console](https://console.azion.com/) > **Object Storage**.
2. In the list of buckets, select the name of your bucket.
3. In the `blue.svg` row, open the actions menu and select **Delete**.
4. In the **Delete File** dialog, enter `blue.svg`.
5. Select **Delete**.

Repeat the steps for `orange.svg`. The bucket is empty.

One day later, delete the bucket:

1. Access [Azion Console](https://console.azion.com/) > **Object Storage**.
2. In the list of buckets, open the actions menu of your bucket and select **Delete**.
3. In the **Delete bucket** dialog, enter the name of your bucket.
4. Confirm the deletion.

Before the 24 hours pass, the Console answers with this error:

```text
Unable to delete a non-empty bucket. Additionally, objects deleted within the last 24 hours are also taken into consideration.
```

### CLI

Delete the Workload, the Application, and the Function:

```bash
azion delete workload --workload-id $WORKLOAD_ID
```

```text
Workload 1794600526 was successfully deleted
```

```bash
azion delete application --application-id $APPLICATION_ID
```

```text
Application 1794609946 was successfully deleted
```

```bash
azion delete function --function-id $FUNCTION_ID
```

```text
Function 110033 was successfully deleted
```

Delete the images:

```bash
azion delete storage object --bucket-name $BUCKET --object-key orange.svg
```

```text
Object orange.svg was deleted successfully
```

```bash
azion delete storage object --bucket-name $BUCKET --object-key blue.svg
```

```text
Object blue.svg was deleted successfully
```

Schedule the deletion of the bucket. `--yes` answers the prompt that the bucket is not empty:

```bash
azion delete storage bucket --name $BUCKET --yes
```

```text
Delete all objects from bucket
Deleting objects...
Bucket deletion was scheduled successfully
```

Right after the command, the bucket still appears in `azion list storage bucket`.

### API

Delete the Workload, the Application, the Function, and the two images:

```bash
for resource in workloads/$WORKLOAD_ID applications/$APPLICATION_ID functions/$FUNCTION_ID storage/buckets/$BUCKET/objects/orange.svg storage/buckets/$BUCKET/objects/blue.svg; do
  curl --silent --request DELETE https://api.azion.com/v4/workspace/$resource \
    --header "Authorization: Token $AZION_API_TOKEN"
  echo
done
```

```text
{"state":"pending"}
{"state":"pending"}
{"state":"pending"}
{"state":"pending"}
{"state":"pending"}
```

One day later, delete the bucket:

```bash
curl --silent --request DELETE https://api.azion.com/v4/workspace/storage/buckets/$BUCKET \
  --header "Authorization: Token $AZION_API_TOKEN"
```

Before the 24 hours pass, the call answers `400` with the code `17006`:

```json
{"errors":[{"code":"17006","title":"Cannot Delete Non Empty Bucket","detail":"Unable to delete a non-empty bucket. Additionally, objects deleted within the last 24 hours are also taken into consideration.","status":"400","source":{"pointer":"/data"}}]}
```

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.

---

## Next steps

- [Object Storage API](/en/documentation/devtools/runtime/api-reference/storage.md): Every method of the azion:storage module, including put and delete for a bucket with write access.
- [Implement file upload with Functions](/en/documentation/guides/application-development/functions-and-runtime/file-upload-functions.md): Write objects to a bucket from a Function, behind an upload endpoint.
- [Use a bucket as an application origin](/en/documentation/guides/application-development/data/use-bucket-as-origin.md): Serve the objects of a bucket directly, through a connector and no code.
- [Object Storage limits](/en/documentation/platform/object-storage/limits.md): Object sizes, bucket counts, and the other limits of the account.
