# s3-dir-utils

> get data from s3 bucket

Latest version **1.0.5** (published 2019-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install s3-dir-utils
pnpm add s3-dir-utils
yarn add s3-dir-utils
bun add s3-dir-utils
```

## 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.0.5 |
| Published | 2019-09-16 |
| First published | 2019-09-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | jishnu mohan |
| Maintainers | jishnu-mpr |
| Keywords | s3-dir-utils, s3, structure, directory, aws, jishnu-mpr |

## Links

- npm: https://www.npmjs.com/package/s3-dir-utils
- Repository: https://github.com/jishnu-mohan/s3-dir-utils
- Issues: https://github.com/jishnu-mohan/s3-dir-utils/issues
- npm.io page: https://npm.io/package/s3-dir-utils

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2019-09-16
- 1.0.4 — 2019-09-15
- 1.0.3 — 2019-09-15
- 1.0.2 — 2019-09-13
- 1.0.1 — 2019-09-13
- 1.0.0 — 2019-09-13

## README

# s3 dir utils

![npm](https://img.shields.io/npm/v/s3-dir-utils) ![GitHub](https://img.shields.io/github/license/jishnu-mohan/s3-dir-utils)

## Installation

`npm i s3-dir-utils`

## Features

This module features 3 APIs
1. `getStructure` - returns the file structure of the bucket as a JSON object.
2.  `getAllFiles` - returns an array of all files inside the bucket.
3. `fileExists` - to check whether a file is present inside the bucket.


## Options

The module requires an option object 
``` javascript
{
 s3
 bucket
 folder
}
```
where:

`s3`	AWS s3 instance

`bucket`	name of the bucket 

`folder` (optional) to get the result for a folder

## Usage

### To get the file structure of the bucket

Returns the file structure of the bucket as a JSON object

``` javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>"}

try {
  const  bucketStructure  =  await  s3B.getStructure(options)
  console.log('bucketStructure -->>', bucketStructure)
}
catch (e) {
  console.log('ERROR', e)
}

/* As a promise */

s3B.getStructure(options)
.then((bucketStructure) => {
console.log('bucketStructure -->>', bucketStructure)
})
.catch((err) => {
console.log('error', err)
})
```
### To get file structure of a folder inside the bucket

Returns the file structure of a folder inside the bucket as JSON object

```javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>", folder: "<Folder Name>"}

try {
  const  folderStructure  =  await  s3B.getStructure(options)
  console.log('folderStructure -->>', folderStructure)
}
catch (e) {
  console.log('ERROR', e)
}

/* As a promise */

s3B.getStructure(options)
.then((folderStructure) => {
console.log('folderStructure -->>', folderStructure)
})
.catch((err) => {
console.log('ERROR', err)
})
```

### To check whether a file is present inside the bucket

Returns a boolean:

`true` if the file is present inside the bucket or

`false` if the file is not present inside the bucket

``` javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>"}
const file = "<File Name>"

try {
  const isFileExists  =  await  s3B.fileExists(options, file)
  console.log('File Exists -->>', isFileExists)
}
catch (e) {
  console.log('ERROR', e)
}

/* As a promise */

s3B.fileExists(options, file)
.then((isFileExists) => {
console.log('File Exists -->>', isFileExists)
if(isFileExists) {
  // do something if the file exists inside the bucket 
}
else {
  // do something if the file does not exist inside the bucket
}
})
.catch((err) => {
console.log('error', err)
})
```
### To check whether a file exists inside a folder in the bucket

Returns a boolean:

`true` if the file is present inside the folder or

`false` if the file is not present inside the folder

``` javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>", folder: "<Folder Name>"}
const file = "<File Name>"

try {
  const isFileExists  =  await  s3B.fileExists(options, file)
  console.log('File Exists -->>', isFileExists)
}
catch (e) {
  console.log('ERROR', e)
}


/* As a promise */

s3B.fileExists(options, file)
.then((isFileExists) => {
console.log('File Exists -->>', isFileExists)
if(isFileExists) {
  // do something if the file exists inside the folder 
}
else {
  // do something if the file does not exist inside the folder
}
})
.catch((err) => {
console.log('ERROR', err)
})
```

### To get a list of all files inside the bucket

Returns an array of file names inside the bucket

``` javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>"}

try {
  const  files  =  await  s3B.getAllFiles(options)
  console.log('files inside bucket -->>', files)
}
catch (e) {
  console.log('ERROR', e)
}

/* As a promise */

s3B.getAllFiles(options)
.then((files) => {
console.log('files inside bucket -->>', files)
})
.catch((err) => {
console.log('ERROR', err)
})
```

### To get a list of all files inside a folder in the bucket

Returns an array of all file names present inside the folder

``` javascript
const s3B = require('s3-dir-utils')

const  s3  =  new  AWS.S3({
accessKeyId:  "<Access Key Here>",
secretAccessKey:  "<Secret Access Key Here>"
})

const  options  = {s3:  s3, bucket:  "<Bucket Name>", folder: "<Folder Name>"}

try {
  const  files  =  await  s3B.getAllFiles(options)
  console.log('bucketStructure -->>', bucketStructure)
}
catch (e) {
  console.log('ERROR', e)
}

console.log('files inside folder -->>', files)

/* As a promise */

s3B.getAllFiles(options)
.then((files) => {
console.log('files inside folder -->>', files)
})
.catch((err) => {
console.log('ERROR', err)
})
```
### License

The package is licensed under the terms of [MIT License](https://github.com/jishnu-mohan/s3-dir-utils/blob/master/LICENSE)

**[⬆ back to top](#Usage)**

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