Upload a folder to a bucket from Node.js
Upload every file of a local folder to an Object Storage bucket, keyed by its path and typed by its extension, from Node.js with @aziontech/storage.
You can upload every file of a local folder to an Object Storage bucket from a Node.js script. The script uses the @aziontech/storage package of Azion Lib. Each object takes the file’s path inside the folder as its key. The script picks the object’s content type from the file extension.
Prerequisites
- Node.js 24.21.0 or later, and npm. The script is an ES module that reads the folder with the
recursiveoption ofreaddirand theparentPathof each entry. - A personal token. To create one, refer to Manage personal tokens.
- A bucket. To create one from Node.js, refer to Create a bucket. The examples use a bucket named
site-assets-4940. - A folder to upload. The examples use a
publicfolder with three files:index.html,css/site.css, andimg/logo.svg.
Install the package
The script imports @aziontech/storage from the node_modules directory of its project.
In the directory that holds the folder to upload, create the project and install the package:
npm prints the new package.json, and the install ends with:
The project has @aziontech/storage in its package.json dependencies.
Write the upload script
The script first reads the bucket with getBucket, which looks it up by its exact name. When the bucket does not exist, the script stops. For each file of the folder, the createObject function then creates an object. Its key is the file’s path inside the folder, with / between the parts, such as css/site.css.
createObject stores the content type you pass in params.content_type, and Object Storage returns it in the Content-Type header of the object. Without one, createObject sends application/octet-stream, and the object keeps that type. Object Storage detects a type only for an upload that carries no Content-Type header. The script picks the content type from the file extension.
The getObjects function then lists the objects of the bucket. The script counts how many of its uploaded keys are in that list. Objects the bucket held before therefore do not change the count. Each function reads your personal token from the AZION_TOKEN environment variable.
getObjects asks for at most 10,000 objects by default. In a larger bucket, an uploaded key can be missing from the list.
The script takes the folder and the bucket name as its two arguments. Create an upload.mjs file:
The script prints the error or the count and exits with code 1 when:
- The bucket does not exist.
- An upload fails.
- An uploaded key is missing from the list.
To upload another file type with its content type, add its extension to contentTypes.
The script uploads every file of the folder, hidden files such as .DS_Store included. Remove the files you do not want to publish before you run it.
Upload the folder
The script uploads the files one at a time and stops at the first upload that fails.
To upload the folder:
-
In the terminal where you run the script, store your personal token in the
AZION_TOKENvariable. Replace[TOKEN VALUE]with your token:Every script you start from this terminal reads the token, until the terminal closes.
-
Run the script with the folder and the name of your bucket:
Text -
(Optional) Run the script again after you change a file. An upload to an existing key replaces the object, so the count stays the same:
Text
The bucket holds one object per file of the folder, each with its content type.
When no bucket has the name you passed, such as site-assets, the script prints Upload failed: The specified bucket does not exist. and uploads nothing.