How To Utilize Filebase with NodeJS

Learn how to utilize Filebase with NodeJS.

What is NodeJS?

NodeJS is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code in environments other than web browsers. It’s primarily used for traditional websites and back-end API services.

Read below to learn how to utilize Filebase with NodeJS.

Filebase would like to thank @figurestudios on Medium for the original version and publication of this tutorial guide.

Prerequisites:

1. Open a Node.js command prompt. To create a folder to store both our files and our code, run the command:

mkdir hello-filebase

2. Then navigate into our new folder with the command:

cd hello-filebase

3. Open Visual Studio Code, or any other IDE of your choice and create a new JavaScript file called main.js that is stored in your hello-filebase folder.

4. This tutorial will build off of the Filebase NodeJS sample repository.

This sample showcases getting buckets and uploading objects, but in this tutorial we’ll download objects as well.

5. In your new main.js file, add the following lines to import the dependencies for this project:

require('dotenv').config();
const AWS = require('aws-sdk');
const s3 = new AWS.S3({endpoint: 'https://s3.filebase.com', signatureVersion: 'v4'});

6. Then, add a function to upload objects:

function Upload(bucket, name) {
   var params = {
       Bucket: bucket,
       Key: name,
       ContentType: 'text/plain'
   };
   s3.putObject(params, function(error, data) {
       if (error) {
         console.error(error);
       } else {
         console.log('Successfully uploaded file' + name + ":" + bucket);
       }
   });
}

7. We’ll also add a second function for downloading objects with the following code:

function Download(bucket, key, callback) {
 var params = {
   Key: key,
   Bucket: bucket
 };
 try {
   s3.getObject(params, function(error, data) {
     if (error) {
       console.log("Error while reading file " + key + ":" + bucket);
       return callback("Error!");
     } else {
       console.log("Returning contents from " + key + ":" + bucket);
       return callback(Buffer.from(data.Body, 'utf8').toString());
     }
   });
 } catch (error) {
   console.error(error);
   return callback("Error!");
 }
}

8. Now let's make a Filebase bucket. To do this, navigate to console.filebase.com.

If you don’t have an account already, sign up, then log in.

9. Select ‘Buckets’ from the left side bar menu, or navigate to console.filebase.com/buckets.

Select ‘Create Bucket’ in the top right corner to create a new bucket.

10. Enter a bucket name and choose the IPFS storage network to create the bucket.

In this guide, we’re calling the bucket filebase-IPFS.

Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.

11. Now at the end of our main.js code, add the following line, replacing the filebase-bucket-name with your bucket name, and object-name with the file name for your object to be uploaded.

Upload("filebase-bucket-name", "object-name");

12. In the NodeJS Command Prompt, install the following dependencies:

npm install aws-sdk

npm install dotenv

13. Create a new file, either in your IDE or from the command line, called .env with the following content:

AWS_ACCESS_KEY_ID=FILEBASE_ACCESS_KEY AWS_SECRET_ACCESS_KEY=FILEBASE_SECRET_KEY

Replace the values with your FILEBASE_ACCESS_KEY and FILEBASE_SECRET_KEY.

14. In the NodeJS Command Prompt, we’re ready to run our code with the following command:

node main.js

15. The uploaded object will now be reflected in your Filebase bucket.

16. Then, you can download the object. To do this, replace the last line of your main.js file that calls the Upload function with the following line:

Download("filebase-bucket-name", "object-name");

17. Then run the node main.js command again. This will download the object to your local directory.

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated