How to list objects in an Edge Storage bucket
This guide walks you through listing the objects in an Edge Storage bucket using the Azion API, Azion CLI, and Azion Runtime.
Listing a bucket’s objects
To list the objects in a bucket, run the following GET
request in your terminal, replacing [TOKEN VALUE]
with your personal token and <bucket_name>
with the name of your bucket:
curl --location 'https://api.azion.com/v4/storage/buckets/<bucket_name>/objects?page_size=10&page=1' \
--header 'Accept: application/json' \
--header 'Authorization: Token [TOKEN VALUE]'
You should receive the following response:
{
"continuation_token": null,
"results": [
{
"key": "index.html",
"last_modified": "2024-01-18T18:47:18.886000Z",
"size": 217
}
]
}
Requirements
To list the objects in an Edge Storage bucket:
azion list edge-storage object --bucket-name bucketname
You can create an edge function to list your objects in a bucket:
- Access Azion Console > Edge Functions.
- Click the + Edge Function button.
- Name your function. Example:
list-objects
. - In the Code tab, add the following JavaScript code:
import Storage from "azion:storage";
async function handleRequest(event) { try{ const bucket = "mybucket"; const storage = new Storage(bucket); const objectsList = await storage.list(); for (const entry of objectsList.entries) { console.log(`key: ${entry.key} length: ${entry.content_length}`); } return new Response("Ok"); }catch(error){ return new Response(error, {status:500}); }}
addEventListener("fetch", (event) => { event.respondWith(handleRequest(event));});
- Click the Save button.
Once the edge function is ready, you need to create an edge application that will proxy the listing process for the bucket and instantiate the edge function.
Go to Edge Application first stepsGo to how to instantiate an edge functionContributors