How to download an object from an Edge Storage bucket

This guide walks you through downloading an object from an Edge Storage bucket using the Azion API and Azion Runtime.

Downloading an object from a bucket

You can create an edge function to download objects from your bucket using a GET console command. To do so:

  1. Access Azion Console > Edge Functions.
  2. Click the + Edge Function button.
  3. Name your function. Example: my-bucket GET.
  4. In the Code tab, add the following JavaScript code, passing your bucket’s name and the object key:
import Storage from "azion:storage";
async function handleRequest(event) {
try{
const bucket = "mybucket";
const storage = new Storage(bucket);
const key = "test";
const storageObject = await storage.get(key);
return new Response(storageObject.content);
}catch(error){
return new Response(error, {status:500});
}
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event));
});
  1. Click the Save button.

Once you have the edge function ready, you need to create an edge application that will proxy the process for the bucket and instantiate the edge function.

Go to Edge Application first stepsGo to how to instantiate an edge function