npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s3-dir-utils

v1.0.5

Published

get data from s3 bucket

Downloads

7

Readme

s3 dir utils

npm GitHub

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

{
 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

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

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

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

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

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

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

⬆ back to top