# @root/s3

> A simple, lightweight s3 client with only 2 dependencies

Latest version **1.2.1** (published 2022-01-07) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install @root/s3
pnpm add @root/s3
yarn add @root/s3
bun add @root/s3
```

Provides the command `s3-download`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2022-01-07 |
| First published | 2020-03-12 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86 |
| Keywords | s3, lightweight, alternative |

## Links

- npm: https://www.npmjs.com/package/@root/s3
- Repository: https://git.rootprojects.org/root/s3.js
- npm.io page: https://npm.io/package/@root/s3

## Dependencies (2)

- [aws4](https://npm.io/package/aws4.md) ^1.9.1
- [@root/request](https://npm.io/package/@root/request.md) ^1.8.0

## Recent versions

- 1.2.1 (latest) — 2022-01-07
- 1.2.0 — 2021-01-27
- 1.1.3 — 2020-12-18
- 1.1.2 — 2020-12-17
- 1.1.0 — 2020-09-10
- 1.0.3 — 2020-03-17
- 1.0.2 — 2020-03-17
- 1.0.1 — 2020-03-14
- 1.0.0 — 2020-03-12

## README

# [s3.js](https://git.rootprojects.org/root/s3.js) | a [Root](https://rootprojects.org) project

> Minimalist S3 client \
> (for AWS, Minio, Digital Ocean Spaces, etc)

A lightweight alternative to the S3 SDK that uses only @root/request and aws4.

-   set()
-   get()
-   head()
-   delete()
-   sign()

### Download a file from S3

This library supports the same streaming options as [@root/request.js](https://git.rootprojects.org/root/request.js).

#### as a stream

```js
var resp = await s3.get({
    accessKeyId,        // 'AKIAXXXXXXXXXXXXXXXX'
    secretAccessKey,    // 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region,             // 'us-east-2'
    bucket,             // 'bucket-name'
    prefix,             // 'my-prefix/' (optional)
    key,                // 'data/stats.csv' (omits prefix, if any)
    stream              // fs.createWriteStream('./path/to/file.bin')
});

await resp.stream;
```

#### in-memory

```js
var resp = await s3.get({
    accessKeyId,        // 'AKIAXXXXXXXXXXXXXXXX'
    secretAccessKey,    // 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region,             // 'us-east-2'
    bucket,             // 'bucket-name'
    prefix,             // 'my-prefix/' (optional)
    key                 // 'data/stats.csv' (omits prefix, if any)
});

fs.writeFile(resp.body, './path/to/file.bin');
```

### Upload a new file to S3

```js
await s3.set({
    accessKeyId,
    secretAccessKey,
    region,
    bucket,
    prefix,
    key,
    body,               // Buffer.from("hello, world")
                        // or fs.createReadStream("./file.txt")

    size                // (await fs.stat("./file.txt")).size (required for streams)
});
```

### Check that a file exists

```js
var resp = await s3.head({
    accessKeyId,        // 'AKIAXXXXXXXXXXXXXXXX'
    secretAccessKey,    // 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region,             // 'us-east-2'
    bucket,             // 'bucket-name'
    prefix,             // 'my-prefix/' (optional)
    key                 // 'data/stats.csv' (omits prefix, if any)
});

console.log(resp.headers);
```

### Delete file

```js
var resp = await s3.delete({
    accessKeyId,        // 'AKIAXXXXXXXXXXXXXXXX'
    secretAccessKey,    // 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region,             // 'us-east-2'
    bucket,             // 'bucket-name'
    prefix,             // 'my-prefix/' (optional)
    key                 // 'data/stats.csv' (omits prefix, if any)
});

console.log(resp.headers);
```

### Return signed URL without fetching.

```js
s3.sign({
    method: 'get',
    accessKeyId,
    secretAccessKey,
    region,
    bucket,
    prefix,
    key
});
````

### A note on S3 terminology

|                         |                                                                                                                                                              |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `bucket`                | most similar to what most people think of as a "**folder**"<br>**MUST NOT** contain a slash `/`                                                              |
| `key`<br>("object key") | most similar to a "**file name**"<br>may contain "**/**"s as part of the name<br>**MUST NOT BEGIN** with a slash `/`                                         |
| `prefix`                | an informal term, refers to "**file path**"<br>what the AWS console uses for created virtual folder-like views and searches<br>**MUST END** with a slash `/` |

This library provides `prefix` (of `key`) for convenience.

`s3://bucket-name/long/prefix/data/stats.csv` can be represented equally well by any of the following:

(no prefix)

```json
{
    "bucket": "bucket-name",
    "prefix": "",
    "key": "long/prefix/data/stats.csv"
}
```

(with long prefix)

```json
{
    "bucket": "bucket-name",
    "prefix": "long/prefix/data/",
    "key": "stats.csv"
}
```

(with short prefix)

```json
{
    "bucket": "bucket-name",
    "prefix": "long/",
    "key": "prefix/data/stats.csv"
}
```

### Troubleshooting

If the body is a stream then `size` must be set to `fs.statSync(filePath).size`, or the request will fail:

```
501
<Code>NotImplemented</Code><Message>A header you provided implies functionality that is not implemented</Message>
```

---
_Source: https://npm.io/package/@root/s3 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
