Search Our Database
How to upload and delete files in buckets using JavaScript
This guide will help you upload and delete files in a bucket programmatically using JavaScript. To begin, ensure that you have Node.js set up in your environment. In this example, we will be installing Node.js version 18. Make sure to use the appropriate commands based on your distribution.
sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=18
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update && sudo apt-get install nodejs -y
sudo dnf module install nodejs:18
We have provided some sample code that allows you to upload and delete files in a bucket. To download the sample code and install the necessary project dependencies, use the following commands:
tar -xf javascript.tar.gz
cd javascript
npm install
Pre-configurations
Before you start using the provided scripts to manage files, you need to configure your access credentials for the bucket and and ensure that the endpoint is correctly specified. Open
with a text editor of your choice.
Edit lines 5, 6, 8 and 9 in
to include your access key, secret key, endpoint and region.
secretAccessKey: 'SECRET_KEY',
endpoint: 'https://ap-southeast-mys1.oss.ips1cloud.com',
region: 'ap-southeast-mys1'
If you’re unsure about the endpoint of your bucket, access the customer portal and check the object storage tab. In our example, we have a bucket named “sample-bucket-01” which has “ap-southeast-mys1.oss.ips1cloud.com” as an endpoint.
Configure your endpoint and region accordingly:
region: 'ap-southeast-mys1'
Next, configure the
of your project. Open
with your preferred text editor.
Edit the
parameter in line 5 to specify which module you want to use. If you intend to upload files, Set the
field to “uploadFile.js”:
For deleting files, set the
field to “deleteFile.js”:
Uploading files
To upload files, Create a directory to store assets that you will upload to the bucket. In this example, we create a directory named “assets” and a file called “sample_upload.txt”.
echo "Hello world" > assets/sample_upload.txt
Now, configure the sample code in
.
Edit lines 9 to 11 to specify the bucket name, the path of the file to be uploaded, and the name of the file in the bucket after uploading. For example:
Key: 'uploaded_text.txt',
Body: readFileSync('./assets/sample_upload.txt')
With
configured, ensure that
is set to run
and run the script using:
If you receive a HTTP 200 status code, it indicates a successful file upload to your bucket.
Deleting files
To delete a file from the bucket, configure
.
Edit lines 8 and 9 to specify the bucket and the file to be deleted. For example:
With
configured, ensure that
is set to run
and run the script using:
If you receive a HTTP 204 status code, it indicates that the file has been successfully deleted from your bucket.
For a guide on how to programmatically upload and delete files from buckets using PHP, refer to the following guide.