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.
Choose the interface you work in. The prerequisites and the procedures change with your choice.
Prerequisites
- 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.
In the API, the connector’s type is storage, and its attributes carry bucket, the name of the bucket, and prefix. For the example on this page, bucket is app-origin and prefix is /src.
Create the connector with a POST request to /v4/workspace/connectors. Then copy the ID of the connector, which the API returns in data.id: the rule that sends requests to the bucket names the connector by this ID.
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.
To create the rule with the API, send a POST request to the request_rules endpoint of the application. Replace <application-id> with the ID of your application, and <connector-id> with the ID of the connector to the bucket:
The API answers 202 and returns the rule:
The rule is the first of the application’s Request Phase, at order 0.
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.