# AWS SDK - JavaScript

## 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:&#x20;

* JavaScript&#x20;
* Node.js&#x20;
* 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.&#x20;

{% hint style="success" %}

### Prerequisites:

* [x] [Download and install](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) npm in your environment if using the CLI.&#x20;
* [x] [Download and install](https://github.com/aws/aws-sdk-js-v3#getting-started) the AWS SDK for Javascript.&#x20;
* [x] [Sign up](https://filebase.com/signup) for a free Filebase account.&#x20;
* [x] Have your Filebase Access and Secret Keys. Learn how to view your access keys [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#working-with-access-keys).
  {% endhint %}

{% hint style="danger" %}
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.
{% endhint %}

### 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

```jsx
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

```jsx
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

{% hint style="success" %}
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.
{% endhint %}

```javascript
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

```jsx
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

```jsx
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](https://github.com/aws/aws-sdk-js-v3).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.filebase.com/code-development-+-sdks/code-development/aws-sdk-javascript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
