Azion Lib quickstart
Install the @aziontech/storage package, set your personal token, and create and list Object Storage buckets with Azion Lib from Node.js.
This guide instructs you through your first Object Storage bucket created with Azion Lib.
- Install the
@aziontech/storagepackage. - Set your personal token in the
AZION_TOKENenvironment variable. - Create a bucket with a Node.js script.
- List the buckets of your account and read the response envelope.
The scripts work with two objects, in this order:
- The personal token authorizes every call. The package reads it from
AZION_TOKENand sends it to Azion API v4. - The bucket is the Object Storage container that the first script creates. Every Storage function finds a bucket by its name.
Prerequisites
- An Azion account. To create one, refer to Create an account.
- A personal token. To create one, refer to Personal Tokens.
- Node.js and npm. The scripts on this page are ES modules that use top-level
await, and they run with thenodecommand.
Install the Storage package
The @aziontech/storage package holds the Azion Lib functions for Object Storage buckets and objects. In an empty directory for the project, install the package:
npm installs the package in the node_modules directory of the project, where the scripts import it from.
Set your personal token
The Storage functions read your personal token from the AZION_TOKEN environment variable. In the terminal where you run the scripts, set the variable. Replace [TOKEN VALUE] with your token:
Every script you start from this terminal reads the token, until the terminal closes. A client created with createClient takes the token in its token field instead. For more information, refer to Storage.
Create a bucket
The createBucket function creates a bucket from a name and a workloads_access access level. A bucket name is unique across every Azion account, so replace my-bucket in the script with a name of your own. For the naming rules and the access levels, refer to Buckets and objects.
Create a file named create-bucket.mjs with the following code:
Run the script:
The script prints the name of the bucket it created:
Your account has a bucket named my-bucket, with the read_only access level.
List the buckets and read the response envelope
Every Storage function returns a response envelope, { data, error }, instead of throwing an error. On success, data holds the result. On failure, data is absent and error holds two fields: message, the reason, and operation, the call that failed.
The getBuckets function lists the buckets of your account, one page at a time. Create a file named list-buckets.mjs with the following code:
Run the script:
The script prints the length of the page and the number of buckets in the account. The page holds at most the 10 buckets that page_size sets:
In data, buckets holds the page of buckets, and count holds the number of buckets in the account, not the length of the page. If the terminal has no AZION_TOKEN, the call returns error instead: message reads Authentication credentials were not provided. and operation reads get all buckets.
The count includes the bucket that create-bucket.mjs created. For every error message the Storage functions return, refer to Storage.