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
1
const AWS = require('aws-sdk');
2
3
const s3 = new AWS.S3({
4
apiVersion: '2006-03-01',
5
accessKeyId: 'Filebase-Access-Key',
6
secretAccessKey: 'Filebase-Secret-Key',
7
endpoint: 'https://s3.filebase.com',
8
region: 'us-east-1',
9
s3ForcePathStyle: true
10
});
Copied!

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
1
const AWS = require('aws-sdk');
2
3
const s3 = new AWS.S3({
4
apiVersion: '2006-03-01',
5
accessKeyId: 'Filebase-Access-Key',
6
secretAccessKey: 'Filebase-Secret-Key',
7
endpoint: 'https://s3.filebase.com',
8
region: 'us-east-1',
9
s3ForcePathStyle: true
10
});
11
12
s3.listBuckets(function (err, data) {
13
if (err) {
14
console.log("Error when listing buckets", err);
15
} else {
16
console.log("Success when listing buckets", data);
17
}
18
});
Copied!

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 Storj network.
1
const AWS = require('aws-sdk');
2
3
const s3 = new AWS.S3({
4
apiVersion: '2006-03-01',
5
accessKeyId: 'Filebase-Access-Key',
6
secretAccessKey: 'Filebase-Secret-Key',
7
endpoint: 'https://s3.filebase.com',
8
region: 'us-east-1',
9
s3ForcePathStyle: true
10
});
11
12
const params = {
13
Bucket: "New-Filebase-Bucket"
14
}
15
16
s3.createBucket(params, (err, data)=>{
17
if(err){
18
console.log(err)
19
} else {
20
console.log("Bucket created", data)
21
}
22
})
Copied!

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
1
const AWS = require('aws-sdk');
2
3
const s3 = new AWS.S3({
4
apiVersion: '2006-03-01',
5
accessKeyId: 'Filebase-Access-Key',
6
secretAccessKey: 'Filebase-Secret-Key',
7
endpoint: 'https://s3.filebase.com',
8
region: 'us-east-1',
9
s3ForcePathStyle: true
10
});
11
12
const params = {
13
Bucket: "filebase-bucket",
14
MaxKeys: 20
15
};
16
17
s3.listObjectsV2(params, function (err, data) {
18
if (err) {
19
console.log("Error when listing objects", err);
20
} else {
21
console.log("Success when listing objects", data);
22
}
23
});
Copied!

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
1
const AWS = require('aws-sdk');
2
3
const s3 = new AWS.S3({
4
apiVersion: '2006-03-01',
5
accessKeyId: 'Filebase-Access-Key',
6
secretAccessKey: 'Filebase-Secret-Key',
7
endpoint: 'https://s3.filebase.com',
8
region: 'us-east-1',
9
s3ForcePathStyle: true
10
});
11
12
const params = {
13
Bucket: 'filebase-bucket',
14
Key: '/path/to/file/filename.png',
15
ContentType: 'image/png',
16
Body: myPictureFile,
17
ACL: 'public-read',
18
};
19
20
const request = s3.putObject(params);
21
request.send();
Copied!
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 [email protected]