Search Our Database

How to upload and delete files in buckets using JavaScript

Last updated on |
by

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.

# Debian based distributions
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

 

# Red Hat Enterprised Linux based distributions
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:

wget https://www.ipserverone.info/wp-content/uploads/2023/10/javascript.tar.gz
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 client.js with a text editor of your choice.

vim client.js

 

Edit lines 5, 6, 8 and 9 in client.js to include your access key, secret key, endpoint and region.

accessKeyId: 'ACCESS_KEY',
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:

endpoint: 'https://ap-southeast-mys1.oss.ips1cloud.com',
region: 'ap-southeast-mys1'

 

Next, configure the package.json of your project. Open package.json with your preferred text editor.

vim package.json

 

Edit the main parameter in line 5 to specify which module you want to use. If you intend to upload files, Set the main field to “uploadFile.js”:

"main": "uploadFile.js"

 

For deleting files, set the main field to “deleteFile.js”:

"main": "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”.

mkdir assets
echo "Hello world" > assets/sample_upload.txt

 

Now, configure the sample code in uploadFile.js.

vim uploadFile.js

 

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:

Bucket: 'sample-bucket-01',
Key: 'uploaded_text.txt',
Body: readFileSync('./assets/sample_upload.txt')

 

With uploadFile.js configured, ensure that package.json is set to run uploadFile.js and run the script using:

node uploadFile.js

 

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 deleteFile.js.

vim deleteFile.js

 

Edit lines 8 and 9 to specify the bucket and the file to be deleted. For example:

Bucket: 'sample-bucket-01',
Key: 'uploaded_text.txt',

 

With deleteFile.js configured, ensure that package.json is set to run deleteFile.js and run the script using:

node deleteFile.js

 

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.