AWS SDK - JavaScript

Learn how to use the AWS SDK for JavaScript with Filebase.

What is AWS SDK - JavaScript?

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - JavaScript.

The AWS SDK for JavaScript supports three runtimes:

  • JavaScript

  • Node.js

  • React Native for mobile development

AWS SDK - JavaScript also supports cross-runtime. Cross-runtime is a service client package that can be run on browsers, Node.js, and React-Native without needing any change to the code.

Prerequisites:

This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.

Declare the S3 Client

The following code example defines the S3 client, and does not return any output. Replace the following values in the code to match your configuration:

  • accessKeyId: Your Filebase Access Key

  • secretAccessKey: Your Filebase Secret Key

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: 'Filebase-Access-Key',
  secretAccessKey: 'Filebase-Secret-Key',
  endpoint: 'https://s3.filebase.com',
  region: 'us-east-1',
  s3ForcePathStyle: true
});

Retrieving Buckets

The following code example returns a list of buckets under your Filebase account. Replace the following values in the code to match your configuration:

  • accessKeyId: Your Filebase Access Key

  • secretAccessKey: Your Filebase Secret Key

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: 'Filebase-Access-Key',
  secretAccessKey: 'Filebase-Secret-Key',
  endpoint: 'https://s3.filebase.com',
  region: 'us-east-1',
  s3ForcePathStyle: true
});

s3.listBuckets(function (err, data) {
  if (err) {
    console.log("Error when listing buckets", err);
  } else {
    console.log("Success when listing buckets", data);
  }
});

Creating a Bucket

The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:

  • accessKeyId: Your Filebase Access Key

  • secretAccessKey: Your Filebase Secret Key

  • Bucket: Your Filebase Bucket Name To Be Created

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.

Buckets created through this method will be automatically created on the IPFS network.

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: 'Filebase-Access-Key',
  secretAccessKey: 'Filebase-Secret-Key',
  endpoint: 'https://s3.filebase.com',
  region: 'us-east-1',
  s3ForcePathStyle: true
});

const params = {
    Bucket: "New-Filebase-Bucket"
}

s3.createBucket(params, (err, data)=>{
    if(err){
        console.log(err)
    } else {
        console.log("Bucket created", data)
    }
})

Listing Files in a Bucket

The following code example returns a list of objects in the bucket name provided. Replace the following values in the code to match your configuration:

  • accessKeyId: Your Filebase Access Key

  • secretAccessKey: Your Filebase Secret Key

  • Bucket: Your Filebase Bucket Name

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: 'Filebase-Access-Key',
  secretAccessKey: 'Filebase-Secret-Key',
  endpoint: 'https://s3.filebase.com',
  region: 'us-east-1',
  s3ForcePathStyle: true
});

const params = {
  Bucket: "filebase-bucket",
  MaxKeys: 20
};

s3.listObjectsV2(params, function (err, data) {
  if (err) {
    console.log("Error when listing objects", err);
  } else {
    console.log("Success when listing objects", data);
  }
});

Uploading Objects

The following code example uploads an object to the bucket name provided. Replace the following values in the code to match your configuration:

  • accessKeyId: Your Filebase Access Key

  • secretAccessKey: Your Filebase Secret Key

  • Bucket: Your Filebase Bucket Name

  • Key: The Local Path To The Object To Be Uploaded

  • Content Type: The Type of Object Being Uploaded

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: 'Filebase-Access-Key',
  secretAccessKey: 'Filebase-Secret-Key',
  endpoint: 'https://s3.filebase.com',
  region: 'us-east-1',
  s3ForcePathStyle: true
});

const params = {
  Bucket: 'filebase-bucket',
  Key: '/path/to/file/filename.png',
  ContentType: 'image/png',
  Body: myPictureFile,
  ACL: 'public-read',
};

const request = s3.putObject(params);
request.send();

For more information on the AWS SDK for JavaScript, check out the documentation here.

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

Last updated