How to list objects in an Object Storage bucket
This guide walks you through listing the objects in an Object Storage bucket using the Azion API, Azion CLI, and Azion Runtime.
Listing a bucket’s objects
Requirements
To list the objects in an Object Storage bucket:
azion list edge-storage object --bucket-name bucketname
You can create an function to list your objects in a bucket:
- Access Azion Console > Functions.
- Click the + 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 function is ready, you need to create an applications that will proxy the listing process for the bucket and instantiate the function.
Go to Applications first stepsGo to how to instantiate an functionTo 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 } ]}