How to use an Edge Storage bucket as the origin of a static edge application

This guide walks you through setting a bucket as origin of an edge application using the Azion API, Azion CLI, and Azion Runtime.

Go to Edge Storage reference

Create a bucket and upload files

This section describes how you can upload objects to a bucket and maintain project structure using the Azion API. In this scenario, you’ll create a static application using two files, distributed into folders as follows:

src/index.html
src/styles/style.css
  1. Create the following index.html file inside of a local src directory:
src/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<h1>Hello world!</h1>
<p>I am an object from a bucket.</p>
</body>
</html>
  1. Now create the style.css file, which is referenced in the HTML, under the styles folder in the same directory:
src/styles/style.css
body {
background-color: black;
}
h1, p {
color: #F3652B;
}
  1. Run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token and writing the desired bucket name in name to create a read-only bucket:
Terminal window
curl --location 'https://api.azion.com/v4/storage/buckets' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
"name": "app-origin",
"edge_access": "read_only"
}'
  1. You should receive the following response:
{
"state": "executed",
"data": {
"name": "app-origin",
"edge_access": "read_only"
}
}
  1. Run the following POST requests in your terminal for each file, replacing <bucket_name> with the name of the bucket you just created:

    • For the index.html file, run the following command using src/index.html as the object key, and adding the object path as data:
    curl --location 'https://api.azion.com/v4/storage/buckets/<bucket_name>/objects/src/index.html' \
    --header 'Accept: application/json; version=3' \
    --header 'Content-Type: text/html' \
    --header 'Authorization: Token [TOKEN VALUE]' \
    --data '@./src/index.html'
    
    • For the styles.css file, run the following command using src/styles/style.css as the object key, and adding the object path as data:
    curl --location 'https://api.azion.com/v4/storage/buckets/<bucket_name>/objects/src/styles/style.css' \
    --header 'Accept: application/json; version=3' \
    --header 'Content-Type: text/css' \
    --header 'Authorization: Token [TOKEN VALUE]' \
    --data '@./src/styles/style.css'
    
  2. You should receive the following responses for the files:

{
"state": "executed",
"data": {
"object_key": "src/index.html"
}
}
{
"state": "executed",
"data": {
"object_key": "src/styles/style.css"
}
}

Create an edge application and set origin type to Edge Storage

Now that your bucket is populated with files, you can create a new edge application and a domain to set the bucket as the origin of the content and serve these objects.

  1. Run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token:
curl --location 'https://api.azionapi.net/edge_applications' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{ "name": "edge storage app", "delivery_protocol": "http", "http3": false, "origin_type": "single_origin", "address": "httpbin.org", "origin_protocol_policy": "preserve", "host_header": "${host}", "browser_cache_settings": "honor", "browser_cache_settings_maximum_ttl": 0, "cdn_cache_settings": "override", "cdn_cache_settings_maximum_ttl": 0
}'
  1. You should receive a response similar to:
{ "results": { "id": <edge_application_id>, "name": "edge storage app", "delivery_protocol": "http" ... }, "schema_version": 3
}
  1. Copy the value from the edge_application_id field and paste it in a text editing app to save it for later requests.
  2. Run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token and <edge_application_id> with the ID of the edge application you created:
curl --location 'https://api.azionapi.net/domains' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{ "name": "edge storage domain", "cname_access_only": false, "digital_certificate_id": null, "edge_application_id": <edge_application_id>
}'
  1. You should receive a response similar to:
{ "results": { "id": <domain_id>, "name": "edge storage domain", ... "edge_application_id": <edge_application_id>, "is_active": true, "domain_name": "xxxxxxxxxx.map.azionedge.net", ... }, "schema_version": 3
}
  1. Copy the URL in the domain_name value and paste it in a text editing app to access later.
  2. Run the following POST request to create an edge storage origin for the application, replacing [TOKEN VALUE] with your personal token, <edge_application_id> with the ID of the edge application, and the value of bucket with the name of the bucket you created:
curl --location 'https://api.azionapi.net/edge_applications/<edge_application_id>/origins' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{ "name": "edge storage origin", "origin_type": "object_storage", "bucket": "app-origin", "prefix": "/src"
}'
  1. You should receive a response similar to:
{ "results": { "origin_id": <origin_id>, "origin_key": "bdcd7003-ba53-4ed8-8ca0-05b1357cdafd", "name": "New Edge Storage origin", "origin_type": "object_storage", ... "bucket": "new-bucket-rw", "prefix": "/" }, "schema_version": 3
}
  1. Copy the value from the origin_id field and paste it in a text editing app to save it for later requests.
  2. Run the following GET request in your terminal to retrieve the ID of the default rule of your edge application’s Rules Engine, replacing <edge_application_id> variable with the edge application ID you copied previously:
curl --location 'https://api.azionapi.net/edge_applications/<edge_application_id>/rules_engine/request/rules' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]'
  1. You should receive a response similar to:
{ ... "results": [ { "id": <rule_id>, "name": "Default Rule", "phase": "default", "behaviors": [ { "name": "set_origin", "target": "<origin_id>" }, { "name": "set_cache_policy", "target": "<cache_setting_id>" } ], ... "description": "" } ]
}
  1. Copy the value from the rule_id field and paste it in a text editing app to save it for later requests.
  2. Run the following PATCH request to modify the default rule, replacing <rule_id> with the ID of the rule you retrieved in the previous step, leaving the set_cache_policy object the same as received in the response:
curl --location --request PATCH 'https://api.azionapi.net/edge_applications/<edge_application_id>/rules_engine/request/rules/<rule_id>' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{ "behaviors": [ { "name": "set_origin", "target": "<origin_id>" }, { "name": "set_cache_policy", "target": "<cache_setting_id>" } ]
}'
  1. Wait some time for the changes to propagate.

Once the changes have been made, access the domain you created, in the format http://xxxxxxxxxx.map.azionedge.net/index.html, to see the HTML file you uploaded with the applied CSS style.

Learn how to use an Edge Storage bucket as the origin of a static edge application. Watch the video below:


Contributors