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

multer-google-or-firebase-bucket

v2.0.2

Published

A multer storage engine for Google-Cloud/Firebase Storage.

Downloads

88

Readme

multer-google-or-firebase-bucket

multer-google-or-firebase-bucket is a custom storage engine for Multer that allows you to upload files directly to Google Cloud Storage or Firebase Storage.

Installation

To install the package, use the following command:

npm install multer-google-or-firebase-bucket

Usage

This package integrates Google Cloud Storage or Firebase Storage with Multer for file uploads. Here's how you can use it in your Express project.

1. Import the necessary modules and set up the Firebase Storage bucket

import multer from 'multer';
import { multerGoogleOrFirebaseStorage, Storage } from 'multer-google-or-firebase-bucket';

// Initialize Google Cloud Storage start

// @example 1
// Create a client with credentials passed by value as a JavaScript object
const storage = new Storage({
  projectId: process.env.Project_Id,
  credentials: {
    type: 'service_account',
    private_key: process.env.Private_key.replace(/\\n/g, '\n'),
    client_email: process.env.Client_Email,
    client_id: process.env.Client_Id,
    universe_domain: process.env.Universe_Domain,
  },
});

// @example 2
// Create a client with explicit credentials
const storage = new Storage({
  projectId: 'your-project-id',
  keyFilename: '/path/to/keyfile.json'
});

// Initialize Google Cloud Storage end

// Get the Firebase Storage bucket
const bucket = storage.bucket(process.env.Storage_Bucket || '');

2. Configure Multer to use multerGoogleOrFirebaseStorage

You can now configure Multer to use the custom storage engine for uploading files.

const storageEngin = multerGoogleOrFirebaseStorage({
  bucket,
  destination: (req, file, cb) => {
    cb(null, 'uploads'); // Store files in the 'uploads' folder in Firebase
  },
  filename: (req, file, cb) => {
    cb(null, `${Date.now()}_${file.originalname.replace(/ /g, '_')}`); // Customize the file name
  },
});

const upload = multer({
  storage: storageEngin,
  limits: { fileSize: 1000 * 1024 * 1024 }, // 1000 MB file size limit
  fileFilter: (_req, file, cb) => {
    const allowedTypes = [
      'image/jpeg',
      'image/png',
      'application/pdf',
      'video/mp4',
      'video/quicktime',
      'video/x-msvideo',
      'video/x-matroska',
    ];
    if (allowedTypes.includes(file.mimetype)) {
      cb(null, true);
    } else {
      cb(new Error('Invalid file type'), false);
    }
  },
}).single('file');

3. Use the upload middleware in your Express routes

You can now use the upload middleware in your Express routes to handle file uploads.

import express from 'express';
import { upload } from './path/to/your/uploadFile'; // Adjust the path

const router = express.Router();

// Middleware to handle Multer and Firebase or Google bucket upload
router.post('/', upload, (req, res) => {
  res.status(200).json({
    message: 'File uploaded successfully',
    file: req.file,
  });
});

export default router;

4. Environment Variables

Make sure you have the following environment variables set up in your .env file:

Project_Id=your-project-id
Private_key=your-private-key
Client_Email=your-client-email
Client_Id=your-client-id
Universe_Domain=your-universe-domain
Storage_Bucket=your-storage-bucket

5. Example .env file

Project_Id=your-project-id
Private_key="-----BEGIN PRIVATE KEY-----
YOUR_PRIVATE_KEY
-----END PRIVATE KEY-----
"
Client_Email=your-client-email@your-project.iam.gserviceaccount.com
Client_Id=your-client-id
Universe_Domain=your-universe-domain
Storage_Bucket=your-bucket-name.appspot.com

6. Example Request

To test the file upload, you can send a POST request to the /upload route using tools like Postman or cURL, with a file parameter containing the file to be uploaded.


Notes

  • The file is uploaded directly to your Firebase or Google Cloud Storage bucket, and the public URL to the file is returned in the response.
  • Ensure that the service account you are using has the necessary permissions to access Google Cloud Storage.