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-image-handler

v1.0.1

Published

Simple javascript/node s3 helper to upload and get any type of image to s3

Downloads

4

Readme

S3-IMAGE-HANDLER

This package simplifies file management with Amazon S3, allowing users to easily upload and retrieve files without writing extensive code.

Installation

$ npm install s3-image-handler

Quick start

First you need to integrate s3-image-handler into your project by using the require function:

const s3ImageHandler = require('s3-image-handler');

Uploading a File

To upload a file to S3, use the uploadFile function:

const file = req.files?.filter((file) => file.fieldname === 'fileWithSomeName')[0];

// I've provided placeholder credentials below, so please refrain from attempting to upload files with this data. Instead, kindly use your own AWS credentials. You can obtain your credentials from AWS.
const config = {
  accessKeyId: 'AJDJKLSDJKLJSDKDKLSL', // Enter your access key here
  secretAccessKey: 'hY7uuu+dksdk+99as9dsjksdljskld', // Enter your secret key here
  region: 'ap-south-1', // Enter your region here example: ap-south-1
  bucket: 'bucket1', //Enter your bucket name here:
};
const response = await s3Module.uploadFile(config, file);
console.log('response', response); // { "key": "61b11f67-b9e7-432c-84e1-74801c1d30ef.png", "url": "https://<Unsigned URL path>/<Key Name>" }

// If we want the signed url then we have to send fetchedSignedUrl flag in the options parameter
const options = { fetchedSignedUrl: true };
const response = await s3Module.uploadFile(config, file, options);
console.log('response', response); // { "key": "61b11f67-b9e7-432c-84e1-74801c1d30ef.png", "url": "https://<Unsigned URL path>/<Key Name>", "signedUrl": "https://<Unsigned URL path>/<Key Name>?queryParams" }

Retrieving a File

const config = {
  accessKeyId: 'AJDJKLSDJKLJSDKDKLSL', // Enter your access key here
  secretAccessKey: 'hY7uuu+dksdk+99as9dsjksdljskld', // Enter your secret key here
  region: 'ap-south-1', // Enter your region here example: ap-south-1
  bucket: 'bucket1', //Enter your bucket name here:
};
const keyName = '61b11f67-b9e7-432c-84e1-74801c1d30ef.png';
const response = await s3Module.getSignedUrlFromKey(config, keyName);

console.log('response', response); // https://bucket1.s3.ap-south-1.amazonaws.com/61b11f67-b9e7-432c-84e1-74801c1d30ef.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X

Retrieving a File with Expiry Time for signed URL

We can pass additional options in getSignedUrlFromKey(). Here we have to pass expiresInSecond key and the value. By default the signed url will have an expiry of 15 minutes.

const config = {
  accessKeyId: 'AJDJKLSDJKLJSDKDKLSL', // Enter your access key here
  secretAccessKey: 'hY7uuu+dksdk+99as9dsjksdljskld', // Enter your secret key here
  region: 'ap-south-1', // Enter your region here example: ap-south-1
  bucket: 'bucket1', //Enter your bucket name here:
};
const keyName = '61b11f67-b9e7-432c-84e1-74801c1d30ef.png';
const options = { expiresInSecond: 60 };
const response = await s3Module.getSignedUrlFromKey(config, keyName, options);

console.log('response', response); // https://bucket1.s3.ap-south-1.amazonaws.com/61b11f67-b9e7-432c-84e1-74801c1d30ef.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X

Example to upload image in express js

Step 1: Setup multer: We will need multer so that we can access the files that are uploaded while calling our api

const multer = require('multer');
const dataCollectionStorage = multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, os.tmpdir());
  },
  filename: function (req, file, callback) {
    callback(null, `${req.params.userId}-${file.fieldname}-${file.originalname}`);
  },
});

Step 2: Create a route for and integrate multer with it

const os = require('os');
const router = require('express')();

const uploadMiddleware = multer({ storage: dataCollectionStorage });

router.route('/test').post(uploadMiddleware.any(), controller.test);

module.exports = router;

Step 3: Lets wtite the controller

exports.uploadFile = async (req, res, next) => {
  try {
    const file = req.files?.filter((file) => file.fieldname === 'fileWithSomeName')[0];

    const config = {
      accessKeyId: 'AJDJKLSDJKLJSDKDKLSL', // Enter your access key here
      secretAccessKey: 'hY7uuu+dksdk+99as9dsjksdljskld', // Enter your secret key here
      region: 'ap-south-1', // Enter your region here example: ap-south-1
      bucket: 'bucket1', //Enter your bucket name here:
    };
    const options = { fetchedSignedUrl: true };
    const response = await s3Module.uploadFile(config, file, options);

    return res.json({
      code: '200',
      message: 'Test API to upload file to s3 bucket successfully',
      data: response,
    });
  } catch (error) {
    return next(error);
  }
};