# AWS SDK for 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:

* 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.

## What is a .car file?

A .car file is a type of compressed archive file that contains multiple files, similar to a ZIP file. .car files are used by the FileCoin and IPFS networks, which utilize the metadata fields to include the IPFS CIDs of each file within the archive.

Read below to learn how to pin files and folders to IPFS using the AWS SDK for JavaScript.

{% hint style="success" %}

### Prerequisites:

* [x] [Download and install](https://aws.amazon.com/cli/) AWS CLI.
* [x] [Configure AWS CLI](https://docs.filebase.com/configurations/cli-tools-and-resources/cli-tools/aws-cli) for use with your Filebase account.
* [x] [Download and install](https://github.com/aws/aws-sdk-js-v3#getting-started) the AWS SDK for Javascript.
* [x] [Sign up](https://filebase.com/signup) for a free Filebase account.
* [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 %}

## Pinning a Single File to IPFS

### 1. Click on the ‘Buckets’ option from the menu to open the Buckets dashboard.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FoOi5Ubzeb2Hgzphe8uG9%2Fimage.png?alt=media&#x26;token=7f8993aa-1e3d-496d-a879-1b4223e1f9bd" alt=""><figcaption></figcaption></figure>

### 2. Once at the Buckets dashboard, create a new bucket by clicking the ‘Create Bucket’ option in the top right corner.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FgORFmXlaw2w7pONxcLVf%2Fimage.png?alt=media&#x26;token=0eaed23a-ac1f-4b9d-a865-96cdbad9bf14" alt=""><figcaption></figcaption></figure>

### 3. Enter a bucket name and choose your storage network.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FBx29MWEcJSidl4HD25r7%2Fimage.png?alt=media&#x26;token=e6df5f41-da33-413a-9247-37af1b061486" alt=""><figcaption></figcaption></figure>

### 4. Create a new file with the following AWS SDK for JavaScript code:

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

* **accessKeyId**: Filebase Access Key
* **secretAccessKey**: Filebase Secret Key
* **Bucket**: Your Filebase Bucket Name
* **Key**: The Local Path To The Object To Be Uploaded To IPFS

```jsx
const AWS = require('aws-sdk');
const fs = require('fs');

const s3 = new AWS.S3({
    accessKeyId: 'Filebase-Access-Key',
    secretAccessKey: 'Filebase-Secret-Key',
    endpoint: 'https://s3.filebase.com',
    region: 'us-east-1',
    signatureVersion: 'v4',
});

fs.readFile('image.png', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    
    const params = {
        Bucket: 'my-ipfs-bucket',
        Key: 'ipfs-file.png',
        Body: data,
	Metadata: { import: "car" }
    };
    
    const request = s3.putObject(params);
    request.on('httpHeaders', (statusCode, headers) => {
        console.log(`CID: ${headers['x-amz-meta-cid']}`);
    });
    request.send();
});
```

## Pinning a Folder to IPFS using the AWS SDK for JavaScript

### **1. Download the ipfs-car package with the following command:**

`git clone https://github.com/web3-storage/ipfs-car`

### **2. Install the ipfs-car package:**

`npx ipfs-car`

### **3. Next, use ipfs-car to package a folder of files into a .car file.**

This has been tested with .car archives containing 10,000 or more files. Use either of the following commands, depending on your desired workflow:

#### **Write a .car file to the current working directory**

`ipfs-car --pack path/to/file/or/dir`

#### **Write a .car file to a specific directory with a specified name**

`ipfs-car --pack path/to/files --output path/to/write/ipfs-car.car`

### 4. Create a new file with the following AWS SDK for JavaScript code:

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

* **accessKeyId**: Filebase Access Key
* **secretAccessKey**: Filebase Secret Key
* **Bucket**: Your Filebase Bucket Name
* **Key**: The Local Path To The Object To Be Uploaded To IPFS

#### AWS SDK v2:

```javascript
const AWS = require('aws-sdk');
const fs = require('fs');

const s3 = new AWS.S3({
    accessKeyId: 'Filebase-Access-Key',
    secretAccessKey: 'Filebase-Secret-Key',
    endpoint: 'https://s3.filebase.com',
    region: 'us-east-1',
    signatureVersion: 'v4',
});

fs.readFile('ipfs-car.car', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    
    const params = {
        Bucket: 'my-ipfs-bucket',
        Key: 'ipfs-car.car',
        Body: data,
	Metadata: { import: "car" }
    };
    
    const request = s3.putObject(params);
    request.on('httpHeaders', (statusCode, headers) => {
        console.log(`CID: ${headers['x-amz-meta-cid']}`);
    });
    request.send();
});
```

#### AWS SDK v3:

```javascript
const command = new PutObjectCommand({...});

command.middlewareStack.add(
  (next) => async (args) => {
    // Check if request is incoming as middleware works both ways
    const response = await next(args);
    if (!response.response.statusCode) return response;

    // Get cid from headers
    const cid = response.response.headers["x-amz-meta-cid"];
    console.log(cid);
    return response;
  },
  {
    step: "build",
    name: "addCidToOutput",
  },
);

const res = await client.send(command);
```
