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:

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

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.

Last updated

Was this helpful?