Azion `Storage` library

The Edge Storage library provides methods to interact with the Edge Storage API, allowing you to manage buckets and objects. This client is configurable and supports both debug mode and environment variable-based configuration.

Go to Azion Libraries Overview

You can interact with the API using a client or calling the methods directly from the library. When making direct calls, you can use the environment variables to configure the client without passing the token and debug parameters directly.

This is an example of how a .env file with your environment variables may look like:

Terminal window
AZION_TOKEN=<your-api-token>
AZION_DEBUG=true
VariableDescription
AZION_TOKENYour Azion API token.
AZION_DEBUGEnable debug mode (true/false).

If you want to create a specific client for interacting with Storage, do it by calling the createClient method from the library:

import { createClient } from 'azion/storage';
import type { AzionStorageClient, AzionStorageResponse, AzionStorage } from 'azion/storage';
const client: AzionStorageClient = createClient({ token: 'your-api-token', options: { debug: true } });
const { data, error } = await client.createBucket('my-new-bucket');
if (data) {
console.log(`Bucket created with ID: ${data.id}`);
} else {
console.error('Failed to create bucket', error);
}

The createClient method has the following parameters and return value:

Parameters:

ParameterTypeDescription
configPartial<{ token: string; options?: OptionsParams }>Configuration options for the Storage client.

Returns:

Return TypeDescription
AzionStorageClientAn object with methods to interact with Storage.

Usage

createBucket

Creates a new bucket.

Example:

import { createBucket } from 'azion/storage';
import type { AzionStorageResponse, AzionBucket } from 'azion/storage';
const { data, error }: AzionStorageResponse<AzionBucket> = await createBucket({
name: 'my-new-bucket',
edge_access: 'public',
});
if (data) {
console.log(`Bucket created with name: ${data.name}`);
} else {
console.error('Failed to create bucket', error);
}

Parameters:

ParameterTypeDescription
namestringThe name of the new bucket.
edge_accessstringThe edge access configuration for the bucket.
options?AzionClientOptionsOptional parameters for the request.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucket>>The created bucket object or error.

deleteBucket

Deletes a bucket by its name.

Example:

import { deleteBucket, AzionDeletedBucket, AzionStorageResponse } from 'azion/storage';
const { data, error }: AzionStorageResponse<AzionDeletedBucket> = await deleteBucket({ name: 'my-bucket' });
if (data) {
console.log(`Bucket ${data.name} deleted successfully`);
} else {
console.error('Failed to delete bucket', error);
}

Parameters:

ParameterTypeDescription
namestringThe name of the bucket to be deleted.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionDeletedBucket>>Object confirming deletion or error.

getBuckets

Retrieves a list of buckets with optional filtering and pagination.

Example:

import { getBuckets, AzionStorageResponse, AzionBucketCollection } from 'azion/storage';
const { data: buckets, error }: AzionStorageResponse<AzionBucketCollection> = await getBuckets({
params: { page: 1, page_size: 10 },
});
if (buckets) {
console.log(`Retrieved ${buckets.count} buckets`);
} else {
console.error('Failed to retrieve buckets', error);
}

Parameters:

ParameterTypeDescription
options?AzionBucketCollectionOptionsOptional parameters for filtering and pagination.
page?numberThe page number for pagination.
page_size?numberThe number of items per page.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucketCollection>>Array of bucket objects or error.

getBucket

Retrieves a bucket by its name.

Example:

import { getBucket, AzionBucket } from 'azion/storage';
const { data: bucket, error }: AzionStorageResponse<AzionBucket> = await getBucket({ name: 'my-bucket' });
if (bucket) {
console.log(`Retrieved bucket: ${bucket.name}`);
} else {
console.error('Bucket not found', error);
}

Parameters:

ParameterTypeDescription
namestringThe name of the bucket to be retrieved.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucket>>The retrieved bucket object or error if not found.

updateBucket

Updates an existing bucket.

Example:

import { updateBucket, AzionBucket, AzionStorageResponse } from 'azion/storage';
const { data: updatedBucket, error }: AzionStorageResponse<AzionBucket> | null = await updateBucket({
name: 'my-bucket',
edge_access: 'private',
});
if (updatedBucket) {
console.log(`Bucket updated: ${updatedBucket.name}`);
} else {
console.error('Failed to update bucket', error);
}

Parameters:

ParameterTypeDescription
namestringThe name of the bucket to be updated.
edge_accessstringThe new edge access configuration for the bucket.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucket>>The updated bucket object or error if update failed.

createObject

Creates a new object in a specific bucket.

Example:

import { createObject, AzionBucketObject, AzionStorageResponse } from 'azion/storage';
const { data: newObject, error }: AzionStorageResponse<AzionBucketObject> = await createObject({
bucketName: 'my-bucket',
key: 'new-file.txt',
file: 'File content',
});
if (newObject) {
console.log(`Object created with key: ${newObject.key}`);
console.log(`Object content: ${newObject.content}`);
} else {
console.error('Failed to create object', error);
}

Parameters:

ParameterTypeDescription
bucketNamestringThe name of the bucket to create the object in.
objectKeystringKey (name) of the object to be created.
filestringThe content of the file to be uploaded.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise< AzionBucketObject | null>The created object or null if creation failed.

getObjectByKey

Retrieves an object from a specific bucket by its key.

Example:

import { getObjectByKey, AzionBucketObject, AzionStorageResponse } from 'azion/storage';
const { data: object, error }: AzionStorageResponse<AzionBucketObject> = await getObjectByKey({
bucketName: 'my-bucket',
key: 'file.txt',
});
if (object) {
console.log(`Retrieved object: ${object.key}`);
} else {
console.error('Object not found', error);
}

Parameters:

ParameterTypeDescription
bucketNamestringThe name of the bucket containing the object.
objectKeystringThe key of the object to be retrieved.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
`Promise< AzionBucketObjectnull>`

getObjects

Retrieves a list of objects in a specific bucket.

Example:

import { getObjects, AzionBucketObject, AzionStorageResponse } from 'azion/storage';
const { data: objectResult, error }: AzionStorageResponse<AzionBucketObjects> = await getObjects({
bucketName: 'my-bucket',
});
if (objectResult) {
console.log(`Retrieved ${objectResult.count} objects from the bucket`);
} else {
console.error('Failed to retrieve objects', error);
}

Parameters:

ParameterTypeDescription
bucketNamestringThe name of the bucket to retrieve objects from.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucketObjects>>Array of bucket objects or error.

updateObject

Updates an existing object in a specific bucket.

Example:

import { updateObject, AzionBucketObject } from 'azion/storage';
const { data: updatedObject, error }: AzionStorageResponse<AzionBucketObject> = await updateObject({
bucketName: 'my-bucket',
key: 'file.txt',
file: 'Updated content',
});
if (updatedObject) {
console.log(`Object updated: ${updatedObject.key}`);
console.log(`New content: ${updatedObject.content}`);
} else {
console.error('Failed to update object', error);
}

Parameters:

ParameterTypeDescription
bucketNamestringThe name of the bucket containing the object.
objectKeystringThe key of the object to be updated.
filestringThe new content of the file.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionBucketObject>>The updated object or error if update failed.

deleteObject

Deletes an object from a specific bucket.

Example:

import { deleteObject, AzionDeletedBucketObject, AzionStorageResponse } from 'azion/storage';
const { data: result, error }: AzionStorageResponse<AzionDeletedBucketObject> = await deleteObject({
bucketName: 'my-bucket',
key: 'file.txt',
});
if (result) {
console.log(`Object ${result.key} deleted successfully`);
} else {
console.error('Failed to delete object', error);
}

Parameters:

ParameterTypeDescription
bucketNamestringThe name of the bucket containing the object.
objectKeystringThe key of the object to be deleted.
debug?booleanEnables debug mode for detailed logging.

Returns:

Return TypeDescription
Promise<AzionStorageResponse<AzionDeletedBucketObject>>Confirmation of deletion or error if deletion failed.

Types

These are the types used by the Storage library and its methods:

ClientConfig

Configuration options for the Storage client.

ParameterTypeDescription
token?stringYour Azion API token.
debug?booleanEnables debug mode for detailed logging.

StorageClient

An object with methods to interact with Storage.

MethodParametersReturn Type
getBucketsoptions?: BucketCollectionOptionsPromise<AzionStorageResponse<AzionBucketCollection>>
createBucketname: string, edge_access: stringPromise<AzionStorageResponse<AzionBucket>>
updateBucketname: string, edge_access: stringPromise<AzionStorageResponse<AzionBucket>>
deleteBucketname: stringPromise<AzionStorageResponse<AzionDeletedBucket>>
getBucketname: stringPromise<AzionStorageResponse<AzionBucket>>

AzionStorageResponse<T>

The response object from a bucket operation.

PropertyTypeDescription
dataT (optional)The data generic object.
error{ message: string; operation: string; } (optional)The error details if the operation fails.

AzionBucket

The bucket object.

PropertyTypeDescription
namestringThe name of the bucket.
edge_accessstring (optional)The edge access of the bucket.
state'executed' | 'pending' (optional)The state of the bucket.
getObjects() => Promise<AzionStorageResponse<AzionBucketObjects>> (optional)A method to get all objects in the bucket.
getObjectByKey(objectKey: string) => Promise<AzionStorageResponse<AzionBucketObject>> (optional)A method to get an object by its key.
createObject(objectKey: string, file: string) => Promise<AzionStorageResponse<AzionBucketObject>> (optional)A method to create a new object in the bucket.
updateObject(objectKey: string, file: string) => Promise<AzionStorageResponse<AzionBucketObject>> (optional)A method to update an existing object in the bucket.
deleteObject(objectKey: string) => Promise<AzionStorageResponse<AzionDeletedBucketObject>> (optional)A method to delete an object from the bucket.

AzionBucketObject

The bucket object.

PropertyTypeDescription
keystringThe key of the object.
state'executed' | 'pending' (optional)The state of the object.
sizenumber (optional)The size of the object.
last_modifiedstring (optional)The last modified date of the object.
content_typestring (optional)The content type of the object.
contentstring (optional)The content of the object.

AzionDeletedBucket

The response object from a delete bucket request.

PropertyTypeDescription
namestringThe name of the bucket.
state`‘executed''pending’`

AzionDeletedBucketObject

The response object from a delete object request.

PropertyTypeDescription
keystringThe key of the deleted object.
state`‘executed''pending’`

Contributors