Use a bucket as an application origin
Serve the objects of an Object Storage bucket through an application, with a storage connector and a Rules Engine rule that sends requests to it.
You can serve the objects of an Object Storage bucket through an application by pairing a connector to the bucket with a Rules Engine rule. A request to the domain of the workload that serves the application then returns the object stored under the matching key. To connect an application to an HTTP server instead, refer to Connect an application to an origin.
An account that has not migrated to API v4 builds the same path through the legacy Origins of the application. For more information, refer to Origins.
Prerequisites
Console
- A bucket whose
workloads_accessisread_onlyorread_write. Arestrictedbucket cannot back an application. To create a bucket, refer to Object Storage quickstart. - An application served by a workload. To create both, refer to Applications quickstart.
- A personal token, sent in the
Authorizationheader asToken [TOKEN VALUE], because this page uploads the objects with the API. To create a token, refer to Manage a personal token. curl, or another HTTP client.- Access to Azion Console. To sign in, refer to Access Azion Console.
Upload the objects to serve
The connector serves what the bucket already holds, so the objects go in first. The example on this page serves a page of two objects, both stored under the src prefix of a bucket named app-origin:
In a local src directory, create index.html. The page links its stylesheet by a relative path:
In a styles directory inside src, create style.css, the file the page links to:
To upload the HTML file with the API, send a POST request with the object key in the path. Content-Type sets the type the object is stored and served with:
The API answers 201 with the key the object is stored under:
To upload the stylesheet, send the same request with its own key and type:
The response carries the key of the second object:
The bucket holds the two objects. The src/ segment of each key is part of the key, not a folder, and the platform creates it with the object. Without a Content-Type header, the platform detects the type of the object. Azion Console and the Azion CLI upload objects too. For more information, refer to Object Storage quickstart.
Create a connector to the bucket
A connector to a bucket names the bucket an application reads from and the prefix inside it. The prefix sets where the application’s paths start. With the prefix /src, the object stored under src/index.html answers at /index.html, and src/styles/style.css answers at /styles/style.css. The prefix is optional in the API and required in Azion Console.
In Azion Console, you set up connectors in the Connectors menu, not in a tab of the application. For each field of the Console form and of the request body, refer to Connector settings.
Console
To create the connector in Azion Console:
Access Azion Console > Connectors.
In the General section, enter app-origin-connector in Name.
In the Connector Type section, select Object Storage.
Select app-origin as the bucket the connector reads from.
Enter /src in Prefix, the prefix the objects were uploaded under.
Azion Console shows Connector successfully created.
Send requests to the bucket
A connector receives no request until a rule sets it. The rule in this section runs in the Request Phase of the application. Its criterion matches every path, with the ${uri} variable, the starts_with operator, and / as the argument, so the whole application reads from the bucket. Its behavior sets the connector.
The ${uri} variable works on every application. A criterion on ${request_uri} needs Application Accelerator on the application. For every variable and operator, refer to Rules Engine for Applications.
Console
To create the rule in Azion Console:
Access Azion Console > Applications, then select your application.
Select + Rule.
In the General section, enter a Name, such as serve-the-bucket.
In the Phase section, select Request Phase. A rule keeps the phase it is created in.
In the Criteria section, set the variable to ${uri}, the operator to starts_with, and the argument to /.
In the Behaviors section, select Set Connector, then select the connector to the bucket in Connector.
Select Save.
The rule appears in the Rules Engine tab of the application, under Request.
For the behavior and its attributes, refer to Set Connector.
Confirm that the application serves the bucket
The rule takes some time to propagate. Until then, the domain of the workload does not answer with the objects.
To confirm the delivery path, request the page through the domain of your workload, with that domain in place of <your-workload-domain>:
The response body is the object stored under src/index.html, which the /src prefix serves at /index.html. It carries the heading Hello world! and the paragraph I am an object from a bucket. A workload’s domain ends in .map.azionedge.net, and the API returns it in workload_domain when it creates the workload.
Go to the same address in a browser. The browser renders the page with the stylesheet applied, which it loads from /styles/style.css under the same prefix.
If the domain does not return the object yet, the rule has not propagated. Send the request again, and look for another cause only after that. For more information, refer to Troubleshoot Applications.
In the Deliver an on-demand video library use case, expect:
- The library is served from the bucket with its type. A request for
https://videos.example.com/course-101/master.m3u8carries200andcontent-type: application/vnd.apple.mpegurl, the type stored at upload.
Serve the bucket from a v3 application
An account that runs on API v3 has no connectors. The bucket is an origin of the application, of the Object Storage type, and a rule points the application at it. To find out which model your account runs on, refer to Verify your account migration.
The objects you uploaded need no change. To serve them from a v3 application in Azion Console:
Access Azion Console > Applications > your application.
In the Origins tab, select + Origin and name it.
In Origin Type, select Object Storage.
In Bucket Name, enter the name of your bucket. In Prefix, enter the prefix of the object keys you want to serve, such as /src. Leave it empty to serve the whole bucket.
Select Save.
In the Rules Engine tab, select Default Rule. Under Behaviors, in the Set Origin behavior, replace Default Origin with the origin you created, and select Save.
Set Origin is the v3 behavior that Set Connector replaced. For every field of the origin, refer to Origins.