Storage
Azion Lib functions of the @aziontech/storage package that create, list, read, update, and delete Object Storage buckets and objects.
The @aziontech/storage package is the Azion Lib library for Object Storage. Its functions create, list, read, update, and delete buckets and the objects inside them through Azion API v4. Each function takes one object argument and returns a response envelope instead of throwing.
Install the package:
The samples on this page are TypeScript ES modules that use top-level await, and they run in Node.js. They import types with import type, which keeps them loadable when the type annotations are stripped.
Authentication
The functions read your personal token from the AZION_TOKEN environment variable. A client created with createClient takes the token in its token field instead.
| Variable | Description |
|---|---|
AZION_TOKEN | Your Azion personal token. |
AZION_DEBUG | With true, the functions log the response bodies the API returns. |
For how the Azion Lib packages resolve the token and the debug setting, refer to How Azion Lib works.
Response envelope
Every function returns an AzionStorageResponse object, { data?, error? }. On success, data holds the bucket, the object, or the list. On failure, error holds { message, operation }, where operation names the call that failed, such as create bucket or get object by key.
A successful deleteBucket or deleteObject returns no data: the envelope holds only error, set to undefined. Check error after a delete, not data.
createClient
Creates a client that holds a token and request options and exposes the bucket functions as methods. createClient is also the default export of the package.
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | No | Your Azion personal token. |
options | AzionClientOptions | No | Request options for every call the client makes. |
Returns an AzionStorageClient. Its methods take the same object as the matching function on this page, without options. The client has no object methods: read and write objects with the object functions or the bucket methods.
This sample creates a client and a bucket with it:
Output:
setupStorage
Returns a bucket by its name, and creates it first when it does not exist. The function reads the bucket, and only when that read finds nothing does it create one with the workloads_access you pass.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket to return or create. |
workloads_access | EdgeAccessType | Yes | The access level of the bucket if the function creates it. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucket, existing or created. The bucket carries the bucket methods, so you can write to it right away.
This sample gets a bucket that exists and writes a JSON object to it:
Output:
createBucket
Creates a bucket.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket. |
workloads_access | EdgeAccessType | Yes | The access level of the bucket. |
options | AzionClientOptions | No | Request options. |
Returns data as the created AzionBucket. A bucket has no id: every other function finds it by name.
Output:
getBuckets
Lists the buckets of the account, one page at a time.
| Parameter | Type | Required | Description |
|---|---|---|---|
params | AzionBucketCollectionParams | No | Pagination, search, ordering, and field selection. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucketCollection: buckets holds the page, and count holds the number of buckets in the account, not the length of the page.
Output:
getBucket
Returns one bucket by its name.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucket, with the bucket methods. A name that matches no bucket returns error with the message The specified bucket does not exist..
Output:
updateBucket
Changes the access level of a bucket.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket to update. |
workloads_access | EdgeAccessType | Yes | The access level to set on the bucket. |
options | AzionClientOptions | No | Request options. |
Returns data as the updated AzionBucket.
Output:
deleteBucket
Deletes a bucket by its name. The API deletes a bucket only while it holds no objects, and not within 24 hours of the last object deletion in it. A bucket that never held an object is deleted at once.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket to delete. |
options | AzionClientOptions | No | Request options. |
On success, the envelope holds no data, so the sample checks error. A refused delete fills error; the messages are in Errors.
Output:
createObject
Creates an object in a bucket.
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket to create the object in. |
key | string | Yes | The key (name) of the object. |
content | ContentObjectStorage | Yes | The content of the object: a string, an ArrayBuffer, a ReadableStream, or a Uint8Array. |
params | { content_type?: string } | No | Object settings. content_type sets the content type of the object. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucketObject with key, content_type, and state. The created object carries no content; read it back with getObjectByKey.
Output:
getObjectByKey
Returns one object, with its content, by its key.
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket that holds the object. |
key | string | Yes | The key of the object. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucketObject with key and content. A key that matches no object returns error with the message The specified bucket object does not exist..
Output:
getObjects
Lists the objects of a bucket.
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket to list. |
params | AzionObjectCollectionParams | No | The maximum number of objects to return. Without it, the function requests max_object_count=10000. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionBucketObjects: objects and count. Each listed object carries key, size, and last_modified. The API also returns is_folder on each object, which the type does not declare.
Output:
updateObject
Replaces the content of an object.
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket that holds the object. |
key | string | Yes | The key of the object to update. |
content | ContentObjectStorage | Yes | The content that replaces the current one: a string, an ArrayBuffer, a ReadableStream, or a Uint8Array. |
params | { content_type?: string } | No | Object settings. content_type sets the content type of the object. |
options | AzionClientOptions | No | Request options. |
Returns data as the updated AzionBucketObject, with key and content.
Output:
deleteObject
Deletes an object from a bucket.
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket that holds the object. |
key | string | Yes | The key of the object to delete. |
options | AzionClientOptions | No | Request options. |
On success, the envelope holds no data, so the sample checks error. A key that matches no object fills error with The specified bucket object does not exist.. Deleting an object also blocks deleteBucket on that bucket for 24 hours.
Output:
Bucket methods
A bucket that getBucket, setupStorage, or createBucket returns carries five methods that act on that bucket. Each takes one object and returns the same envelope as the matching function:
| Method | Argument | Returns data as |
|---|---|---|
getObjects | { params: AzionObjectCollectionParams } (params is required) | AzionBucketObjects |
getObjectByKey | { key: string } | AzionBucketObject |
createObject | { key: string; content: ContentObjectStorage; params?: { content_type?: string } } | AzionBucketObject |
updateObject | { key: string; content: ContentObjectStorage; params?: { content_type?: string } } | AzionBucketObject |
deleteObject | { key: string } | none on success; check error |
This sample reads a bucket, then lists, reads, updates, and deletes an object through its methods:
Output:
Errors
A failed call returns these messages in error.message. error.operation names the call, such as get all buckets or delete bucket.
| Message | Cause | What to do |
|---|---|---|
Authentication credentials were not provided. | No token reached the call: AZION_TOKEN is unset and no client token was passed. | Set AZION_TOKEN, or pass token to createClient. |
Invalid authentication credentials. | The token is not valid. | Use a valid personal token. |
This field is required. | The create request has no workloads_access. | Pass both name and workloads_access. |
The specified bucket does not exist. | No bucket in the account has that name. | Check the name with getBuckets. |
The specified bucket object does not exist. | The bucket holds no object with that key. | Check the key with getObjects. |
Unable to delete a non-empty bucket. Additionally, objects deleted within the last 24 hours are also taken into consideration. | The bucket holds objects, or an object was deleted from it within the last 24 hours. | Delete every object, then wait 24 hours after the last deletion. For more information, refer to Buckets and objects. |
Types
The package exports these types. Import them with import type.
AzionStorageClient
The client that createClient returns. Every method takes one object.
| Method | Argument | Returns |
|---|---|---|
getBuckets | { params?: AzionBucketCollectionParams } (optional) | Promise<AzionStorageResponse<AzionBucketCollection>> |
getBucket | { name: string } | Promise<AzionStorageResponse<AzionBucket>> |
createBucket | { name: string; workloads_access: EdgeAccessType } | Promise<AzionStorageResponse<AzionBucket>> |
updateBucket | { name: string; workloads_access: EdgeAccessType } | Promise<AzionStorageResponse<AzionBucket>> |
deleteBucket | { name: string } | Promise<AzionStorageResponse<AzionDeletedBucket>> |
setupStorage | { name: string; workloads_access: EdgeAccessType } | Promise<AzionStorageResponse<AzionBucket>> |
AzionClientOptions
Request options that every function takes in options, and createClient takes for all its calls.
| Property | Type | Required | Description |
|---|---|---|---|
debug | boolean | No | Logs the response bodies the API returns. |
force | boolean | No | Forces the operation, even when it can destroy data. |
env | AzionEnvironment | No | The environment the calls go to. |
external | boolean | No | Forces the REST API instead of the API built into the runtime. |
AzionEnvironment
The environment a client calls.
AzionStorageResponse
The envelope every function returns. For how to read it, refer to Response envelope.
| Property | Type | Required | Description |
|---|---|---|---|
data | T | No | The result of the call. Absent after a successful delete. |
error | { message: string; operation: string } | No | The error message and the operation that failed. |
AzionBucket
A bucket.
| Property | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket. |
workloads_access | EdgeAccessType | Yes | The access level of the bucket. |
state | 'executed' | 'executed-runtime' | 'pending' | No | The state of the bucket. |
last_editor | string | No | The user who last edited the bucket. |
last_modified | string | No | When the bucket was last modified. |
product_version | string | No | The product version. |
getObjects, getObjectByKey, createObject, updateObject, deleteObject | functions | Yes | The bucket methods. |
AzionBucketCollection
A page of buckets.
| Property | Type | Required | Description |
|---|---|---|---|
buckets | AzionBucket[] | Yes | The buckets on the page. |
count | number | Yes | The number of buckets in the account. |
AzionBucketCollectionParams
Pagination and filtering for getBuckets.
| Property | Type | Required | Description |
|---|---|---|---|
page | number | No | The page number. |
page_size | number | No | The number of buckets per page. |
search | string | No | Matches part of a bucket name. |
ordering | string | No | The field that orders the results. |
fields | string | No | The fields to return, comma-separated. |
AzionBucketObject
An object in a bucket.
| Property | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The key of the object. |
state | 'executed' | 'executed-runtime' | 'pending' | No | The state of the object. |
size | number | No | The size of the object, in bytes. |
last_modified | string | No | When the object was last modified. |
content_type | string | No | The content type of the object. |
content | ContentObjectStorage | No | The content of the object. |
AzionBucketObjects
A list of objects.
| Property | Type | Required | Description |
|---|---|---|---|
objects | AzionBucketObject[] | Yes | The objects in the bucket. |
count | number | Yes | The number of objects in the list. |
AzionObjectCollectionParams
The limit for getObjects.
| Property | Type | Required | Description |
|---|---|---|---|
max_object_count | number | No | The maximum number of objects per request. |
AzionDeletedBucket
The type deleteBucket declares for data. A successful delete returns no data.
| Property | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the bucket. |
state | 'executed' | 'executed-runtime' | 'pending' | No | The state of the bucket. |
AzionDeletedBucketObject
The type deleteObject declares for data. A successful delete returns no data.
| Property | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The key of the deleted object. |
state | 'executed' | 'executed-runtime' | 'pending' | No | The state of the deletion. |
ContentObjectStorage
The content an object takes.
EdgeAccessType
The access level of a bucket: what the Azion platform may do with it when an application serves it. For what each value allows, refer to Access levels.