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

Azion Edge Storage allows you to create buckets and use them as a source of content through Origins. This guide covers how to create and serve a static application retrieved from an Edge Storage bucket using the Azion API and Real-Time Manager.

go to Edge Storage reference

See the guides on How to create and modify an Azion Edge Storage bucket for bucket operations and How to upload and download objects from an Azion Edge Storage bucket for object operations.


Create a bucket and upload files

Section titled 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 name with the desired bucket name to create a read-write 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_write"
}'
  1. You should receive the following response:
{
"state": "executed",
"data": {
"name": "app-origin",
"edge-access": "read_write"
}
}
  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:
    Terminal window
    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:
    Terminal window
    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

Section titled 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.

To facilitate this process, use the Edge Application Proxy template:

  1. Access Real-Time Manager (RTM).
  2. On the homepage, select the Start with a template option.
  3. Find the Edge Application Proxy card and select it.
  4. Click the Settings tab to open the configuration form.
  5. Give your application an easy-to-remember name.
  6. In Origin Address, type httpbin.org.
    • This is a temporary origin that will later be replaced with an Edge Storage origin.
  7. In Route to bypass, type / to specify the root path of the application.
  8. Wait for the deployment process to finish.
  9. Copy the domain of the application, in the format http://xxxxxxxxxx.map.azionedge.net/.

Now you need to configure a new Edge Storage origin and determine that your application should retrieve content from the bucket and prefix you created. To do so:

  1. Access RTM > Edge Application.
  2. Click the proxy application you created in the previous steps.
  3. Navigate to the Origins tab.
  4. Click the Add Origin button.
  5. Give your origin an easy-to-remember name.
  6. In Origin Type, select Edge Storage.
  7. In Bucket Name, add the name of the bucket you created in the previous steps.
  8. In Prefix, add /src, which is the prefix you added to the object keys uploaded previously.
  9. Click the Save button.

To activate the origin in your edge application:

  1. Navigate to the Rules Engine tab.
  2. Select the Default Rule.
  3. Under Behaviors, in the Set Origin behavior, replace the Default Origin with the origin you created for your bucket.
  4. Click the Save button.
  5. Wait some time for the changes to propagate to the edge.

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

  1. Run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token:
Terminal window
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": "overwrite",
"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:
Terminal window
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 bucket with the name of the bucket you created:
Terminal window
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:
Terminal window
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:
Terminal window
curl --location --request PATCH 'https://api.azionapi.net/edge_applications/<edge_application_id>/rules_engine/phase/rules/<rule_id>' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token azion12d49b3ff51f32313f9c7f8b77a02bf0e69' \
--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.


Contributors