Skip to content

janis-commerce/s3

Repository files navigation

s3

Build Status Coverage Status npm version

A package to handle the S3 requests

Installation

npm install @janiscommerce/s3

Description

This is a wrapper for the AWS SDK for the management S3 request, that makes easier the use of it.

For more information read the AWS S3 SDK.

Since v2.0.0 you need Node 18

The possible methods are:

  • getObject
  • getObjectRaw
  • putObject
  • headObject
  • copyObject
  • deleteObject
  • deleteObjects
  • listObjects
  • listBuckets
  • createBucket
  • deleteBucket
  • getSignedUrl
  • createPresignedPost

getObject

Since the package updates to use SDK v3 and Node-18 compatibility, when use getObject, it returns the Body as an Readable type. This change is not compatible with previous uses.

So getObject method convert the Body into a Buffer type object, to keep the compatibility with previous uses.

If you wanted the original behavior, you can use getObjectRaw.

Usage

const S3 = require('@janiscommerce/s3');

try {
	const s3Response = await S3.putObject({
		Body: 'File content',
		Bucket: 'bucket-ame',
		Key: `path/to/file.txt`
	});
	console.log(s3Response);
} catch(err) {
	handleError(err);
}

Streams

For manage stream the package provide

GetObjectStream

The class to get, parse and process S3 streams.

Usage

const { GetObjectStream } = require('@janiscommerce/s3');

class MyGetObjectStream extends GetObjectStream {

	// Parse the incoming data before process rows
	get parsers() {
		return [
			/*
			* Your parsers here as array, where:
			* [function, ...params]
			*/
		]
	}

	// Manage the buffer rows size
	get bufferSize() {
		return 10;
	}

	// Process the buffered rows and return and array to continue.
	async processBuffer(buffer) {
		// ... Your logic here ...
	}

}

const myGetObjectStream = new MyGetObjectStream();

const myProcessedStream = myGetObjectStream.call({
	Bucket: 'bucket-ame',
	Key: `path/to/file.txt`
});

uploadStream

Method to manage streams upload

Usage

const { uploadStream } = require('@janiscommerce/s3');

try {
	const response = await uploadStream(someStream, {
		Bucket: 'bucket-ame',
		Key: `path/to/file.txt`
	});
} catch(e) {
	console.log(e)
}