How to upload and download objects from an Edge Storage bucket

Azion Edge Storage allows you to create buckets and integrate your object storage with your edge infrastructure. This guide covers how to upload and download objects from a bucket using the Azion API and Azion Runtime.

Go to Edge Storage reference

Read How to create and modify an Edge Storage bucket for bucket operations.


Via API

Uploading an object to a bucket

To upload an object, run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token, <bucket_name> with the name of your bucket, <object_key> with an ID or name for the object, the Content-Type header with the MIME type being submitted, and the object sent as a data binary:

Terminal window
curl --location 'https://api.azion.com/v4/storage/buckets/<bucket_name>/objects/<object_key>' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: text/csv' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '@./path/file.csv'

You should receive the following response:

{
"state": "executed",
"data": {
"object_key": "folder/csv-file"
}
}

Downloading an object from a bucket

To download the object, run the following GET request in your terminal, replacing [TOKEN VALUE] with your personal token, <bucket_name> with the name of your bucket, and <object_key> with the key created for the object:

Terminal window
curl --location 'https://api.azion.com/v4/storage/buckets/<bucket_name>/objects/<object_key>' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]'

You should now see the contents of the object in your terminal.


Via Azion Runtime

You can create an edge function to upload objects to your bucket using a POST console command and see if an object is available using a GET console command. To do so:

  1. Access Azion Console > Edge Functions.
  2. Click the Add Function button.
  3. Name your function. Example: my-bucket POST+GET.
  4. In the Code tab, add the following JavaScript code:
import Storage from "azion:storage";
async function doGet(path, bucket_name) {
console.log(`Path: ${path}`);
const storage = new Storage(bucket_name);
// content is an StorageObject
const asset = await storage.get(path);
return new Response(await asset.arrayBuffer(), {
headers: {
"Content-Type": asset.contentType
},
});
}
async function doPost(path, content_type, value, bucket_name) {
console.log(`Path: ${path}`);
let options = {
"Content-Type": content_type
}
console.log(`Options: ${options}`);
const storage = new Storage(bucket_name);
await storage.put(path, value, options);
return new Response("Object added.");
}
async function router(event) {
const request = event.request;
const method = request.method;
const path = decodeURI(new URL(request.url).pathname);
const bucket_name = event.args.bucket;
if (method === "POST") {
let content_type = request.headers.get("Content-Type");
let content = await request.arrayBuffer();
return doPost(path, content_type, content, bucket_name);
} else if (method === "GET") {
return doGet(path, bucket_name);
} else {
throw new Error(`Invalid method: ${method}. Expected POST or GET.`);
}
}
addEventListener("fetch", (event) => {
event.respondWith(
router(event)
);
});
VariableDescription
pathPath to the object. Example: ./path/file.csv
bucket_nameThe name of the bucket. Example: my-bucket
content_typeThe MIME type of the object. Example: text/csv
valueThe contents of the object as a data binary
  1. In the Arguments tab, add the object with the bucket property and, as a value, the name of the bucket as a string.
{
"bucket": "my-bucket"
}
  1. Click the Save button.

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

  1. Access Azion Console.
  2. Select the Start with a template option on the homepage.
  3. Select the Build from scratch option.
  4. Give your application an easy-to-remember name. Example: my-bucket Proxy.
  5. Select the option Run a Function.
  6. In Choose Edge Function, select the edge function you created in the previous steps.
  7. Click the Next button.
  8. Copy the link of the edge application. It should be in the format http://xxxxxxxxxx.map.azionedge.net.
  9. Run the following command in your terminal to upload an object:
curl -v http://xxxxxxxxxx.map.azionedge.net ./path/file.csv txt/csv --data-binary @./path/file.csv my-bucket
  1. Run the following command in your terminal to download an object:
curl -v http://xxxxxxxxxx.map.azionedge.net ./path/file.csv my-bucket

Contributors