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-list-bucket-stream

v1.0.0

Published

Node.js stream library that allows you to stream a list of objects from an S3 bucket

Downloads

3

Readme

s3-list-bucket-stream

Node.js stream library that allows you to stream a list of objects from an S3 bucket

npm version CircleCI Codecov coverage JavaScript Style Guide

Install

Using npm:

npm i --save s3-list-bucket-stream

or yarn

yarn add s3-list-bucket-stream

Note: In order to use this package you need to have the aws-sdk module installed (or any other library that allows you to instantiate an S3 client with the listBucketV2 method).

Usage

Here's a simple example that allows to list all the files in a bucket

const S3ListBucketStream = require('s3-list-bucket-stream')

// create the S3 client
const AWS = require('aws-sdk')
const s3 = new AWS.S3()

// create the instance for the stream
const listBucketStream = new S3ListBucketStream(s3, 'some-bucket', 'path/to/files')

// attach an 'on data' event which will start the stream flow
listBucketStream
  .on('data', (key) => console.log(key.toString()))

This will output:

path/to/files/file1
path/to/files/file2
path/to/files/file3
...

If you want to emit objects containing the entire S3 object metadata, you can do so by enabling the fullMetadata option while creating the stream instance:

const S3ListBucketStream = require('s3-list-bucket-stream')

// create the S3 client
const AWS = require('aws-sdk')
const s3 = new AWS.S3()

// create the instance for the stream
const listBucketStream = new S3ListBucketStream(
  s3,
  'some-bucket',
  'path/to/files',
  { fullMetadata: true }
)

// attach an 'on data' event which will start the stream flow
listBucketStream
  .on('data', console.log)

This will produce this output:

{ Key: 'path/to/files/file1',
  LastModified: 2019-02-08T11:11:19.000Z,
  ETag: '"7e97db1005fe07801a3e3737103ceab8"',
  Size: 49152,
  StorageClass: 'STANDARD' }
{ Key: 'path/to/files/file2',
  LastModified: 2019-02-07T11:11:19.000Z,
  ETag: '"6a97db1005fe07801a3e3737103ceab8"',
  Size: 39152,
  StorageClass: 'STANDARD' }
{ Key: 'path/to/files/file3',
  LastModified: 2019-02-05T11:11:19.000Z,
  ETag: '"b097db1005fe07801a3e3737103ceab8"',
  Size: 29152,
  StorageClass: 'STANDARD' }
...

Note: with this option enabled, the stream will automatically enable objectMode.

Programmatic API

Here you can find more details on how to configure the stream instances and what kind of events are available.

Constructor arguments

When creating a new instance of the stream these are the arguments you can pass as constructor arguments:

  • s3 (Object): An S3 client from the AWS SDK (or any object that implements a compatible listObjectsV2 method)
  • bucket (string): The name of the bucket to list
  • [bucketPrefix] (string): A prefix to list only files with the given prefix (optional)
  • [options] (S3ListBucketStreamOptions): Stream options (optional)
  • [listObjectsV2args] (ListObjectsV2args): Extra arguments to be passed to the listObjectsV2 call in the S3 client (optional)

S3ListBucketStreamOptions

S3ListBucketStreamOptions is an object that can contain arbitrary Readable stream options. You can also specify the following extra options:

  • fullMetadata (boolean): switches the stream to objectMode: true and emits objects containing the full metadata for a given bucket object. See the example above for more details (default value is false).

ListObjectsV2args

ListObjectsV2args is an object that can contain arbitrary s3.listObjectsV2 parameters like MaxKeys (set to 1000 by default), FetchOwner, RequestPayer or StartAfter.

These parameters will be propagated to every internal listObjectsV2 call to the S3 client provided at construction time.

Note: be careful not to specify values for Bucket, Prefix and ContinuationToken as these values will be managed by the internals according to the internal state and configuration of the given stream instance.

Events

The stream is a Readable stream instance, so it can fire all the common Readable stream events.

In addition to these events there some new events that will be fired by a given instance:

  • page: fired when a new page is fetched through the S3 client. If listening to this event, your callback will receive an object containing the properties params (original parameters for the listObjectsV2 call) and data (the raw response to the listObjectsV2 call).
  • stopped: fired when the readable buffer is full and the fetch from S3 is stopped
  • restarted: fired when the fetch from S3 is restarted after a pause

Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

You can also submit PRs as long as you adhere with the code standards and write tests for the proposed changes.

License

Licensed under MIT License. © Luciano Mammino.