# AWS SDK - JavaScript

Please read the following to understand how to use the AWS SDK - JavaScript with the 4EVERLAND Bucket. Combine the AWS SDK API for the JavaScript language with the 4EVERLAND Bucket to achieve file uploads to the IPFS or Arweave storage networks.

## Preparation

* [Download and install](https://github.com/aws/aws-sdk-js-v3#getting-started) the AWS SDK for JavaScript.
* [Register](https://www.4everland.org/) a free 4EVERLAND account.
* If you need to use Arweave storage, you need to create an Arweave bucket. Click to[ ](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible)[learn more](https://docs.4everland.org/storage/bucket/guides).
* To obtain the corresponding API key in the bucket, click to[ learn more](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible).

## Development Examples

### Install the S3 client

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

`endpoint`：<https://endpoint.4everland.co>

`accessKey`：Bucket-Access Keys-API Key

`secretKey`：Bucket-Access Keys-API Secret

`region`: Default filled in with "4everland"

```javascript
import { S3 } from '@aws-sdk/client-s3';

const s3 = new S3({
  endpoint:"https://endpoint.4everland.co",
  credentials: {
    accessKeyId: "4EVERLAND-Bucket-APIKey",
    secretAccessKey: "4EVERLAND-Bucket-APISecret"
  },
  region: "4EVERLAND",
});
```

### Create a bucket

`bucket`: The desired bucket name to create

```javascript
const params = {
    Bucket: "bucketname",
}
s3.createBucket(params,(err, data)=>{
    if(err){
        console.log(err)
    } else {
        console.log('Bucket created', data)
    }
});
```

### Upload a file

`bucket`: Your bucket name

`Key`: The file path for the file to be uploaded

`ContentType`: The type of the file to be uploaded

```javascript
const params = {
  Bucket: "4everland-bucketname",
  Key: "/path/to/4everland/4ever.png",
  ContentType: "image/png",
  Body: "data content",
}

s3.putObject(params, (err, data)=>{
     if(err){
        console.log(err)
    } else {
        console.log('Upload success', data)
    }
};
```

### List the files in the bucket

`Bucket`: Your bucket name

`Prefix`: Files will be listed according to the provided path. When the input is empty, all files in the bucket will be listed

```javascript
const params = {
  Bucket: "4everland-bucketname",
  Prefix: "/path/to/" 
};
s3.listObjectsV2(params, function (err, data) {
  if (err) {
    console.log("Error when listing objects", err);
  } else {
    console.log("Success when listing objects", data);
  }
});
```

### Requesting IPFS CID and Arweave Hash

`Bucket`: The bucket name where the target file is stored

`Key`: The path of the target file

```javascript
s3.headObject({
    Bucket: "4everlandbucket",
    Key: "/path/to/4everland/4ever.png",
},(err, data)=>{
    if(!err) {
        console.log(data)
        console.log("ipfs cid:", data.Metadata['ipfs-hash'])
        // If it is an Arweave type of bucke
        console.log("arweave hash:", data.Metadata['arweave-hash']) 
    }
})
```

{% hint style="info" %}
If you have any questions, please join our [Discord server](https://discord.com/invite/Cun2VpsdjF), or send us an email at <contact@4everland.org>.
{% endhint %}
